简体   繁体   中英

With $result I would be getting JSON value and with $arr I will get multidimensional array. So how can I print single value of JSON or of PHP?

<?php
$url="https://api.airbnb.com/v2/search_results?client_id=3092nxybyb0otqw18e8nh5nty&search=kolkata";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$arr=json_decode($result, true);
echo $arr;
?>

With $result I would be getting JSON value and with $arr I will get multidimensional array. So how can I print single value of JSON or of PHP?

Use foreach() loop of php.

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (array_expression as $value)
{
    // your code here
}

Note: You can further use foreach inside another foreach.

In our example curl Response contain two main index First one is : metadata Second one is : search_results

Example : May be this code is helpful to you :

    - Access index value in PHP look like :

        echo $arr['metadata']['pagination']['result_count'];

    - Access index value in Json look like (usin .(dot) sign ): 

        arr.metadata.pagination.result_count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM