Click
Click

Reputation: 5

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.

Upvotes: 0

Views: 51

Answers (3)

jvk
jvk

Reputation: 2201

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));

Upvotes: 0

Vamsi
Vamsi

Reputation: 421

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.

Upvotes: 2

NETFLOX
NETFLOX

Reputation: 119

you can use this one:

   return json_encode($keywords[29254]);

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

Upvotes: 0

Related Questions