简体   繁体   中英

How to extract data from JSON array using PHP?

This is my JSON :

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [uid] => 1
                    [email] => 1@gmail.com
                )

            [1] => Array
                (
                    [uid] => 2
                    [email] => 2@gmail.com
                )
 )

)

This is my code (PHP):

header('Content-Type: application/json; charset=UTF-8');
$dulieu=$_GET['token'];
$url = "HTTPS://MYJSONURL.COM?url=$dulieu";
$content = file_get_contents($url);
$json = json_decode($content, true);
echo print_r($json, true);

How to get result like:

1|1@gmail.com <br>
2|2@gmail.com

Try something like this:

$array = array(); //You can also use shorthand: $array = [];
foreach($json['data'] as $v){
    array[] = "{$v['uid']}|{$v['email']}";
}

var_dump($array);

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