多维数组中的值

时间:2018-08-27 14:16:33

标签: php arrays json

我有一个将数组值作为JSON对象返回的函数:

function keywords(){

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

    return json_encode($keywords);
 }

print_r(keywords());

结果:

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

我只想获取具有键29254的数组。

我尝试过:

$data = json_decode(keywords());

print_r($data)[29254];

...但是我还是全部。

3 个答案:

答案 0 :(得分:2)

希望这会有所帮助

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

print_r($data['29254']);

或尝试

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

json_decode将在对象内部返回值。

答案 1 :(得分:0)

您可以使用这个:

   return json_encode($keywords[29254]);

输出:     [“ JOIN”,“ PIN”,“ WITHDRWAL”,“ BALANCE”]

答案 2 :(得分:0)

SELECT p.person_id, 
       group_concat(pp.description order by pp.description) as concat_product_descriptions,
       group_concat(pp.price order by pp.description) as concat_product_prices
FROM person p
LEFT JOIN person_products pp on p.person_id = pp.person_id
GROUP BY p.person_id