简体   繁体   中英

Values from Multidimensional Array

I have a function that return array values as JSON object:

function keywords(){

    $keywords = array ('29254' => array('JOIN', 'PIN', 'WITHDRWAL', 'BALANCE'),
                        '24254' => array('UPNIN', 'PEIN', 'BALANCE'),
                      );

    return json_encode($keywords);
 }

print_r(keywords());

The result:

{"29754":["JOIN","PIN","WITHDRWAL","BALANCE"],"24254":["UPNIN","PEIN","BALANCE"]}

I want to get the array with the key 29254 only.

I tried this:

$data = json_decode(keywords());

print_r($data)[29254];

...but I still get all of them.

Hope this helps

$data = json_decode(keywords(), true);

print_r($data['29254']);

or try this

$data = json_decode(keywords());
print_r($data->{29254});

json_decode will return values inside the object.

you can use this one:

   return json_encode($keywords[29254]);

output: ["JOIN","PIN","WITHDRWAL","BALANCE"]

function keywords($data=''){

    $keywords = array ('29254' => array('JOIN', 'PIN', 'WITHDRWAL', 'BALANCE'),
                        '24254' => array('UPNIN', 'PEIN', 'BALANCE'),
                      );

    return !empty($data) ? json_encode($keywords[$data]) : json_encode($keywords);
 }

print_r(keywords(29254));

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