嗨,我有以下代码:
$shops = array();
foreach($json as $var)
{
if (in_array($var['shop'], $shops)) {}
else
{
array_push($shops,$var['shop']);
array_push($shops->$var['shop'],array('revenue' => 0, 'numberofbooks' => 0));
}
}
我想要如下:
Shops
=> shop1
=> revenue
=> numberofbooks
=>shop2
=> revenue
=> numberofbooks
但是插入第三个数组不起作用,有人能帮我吗?
最佳答案
试试这个:
$shops = array();
foreach($json as $var){
if (!isset($shops[$var['shop']])){ // use ! as "not", and use isset()
$shops[$var['shop']] = array('revenue' => 0, 'numberofbooks' => 0);
}
}
php的
in_array()
检查数组中是否存在值,而不是键是否存在。关于php - 如何在PHP中将数组插入数组中的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18188110/