3

大家好,我有以下代码:

$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

但是插入第三个数组不起作用,任何人都可以帮助我吗?

4

2 回答 2

1

尝试这个:

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

PHPin_array()检查数组中是否存在值而不是键是否存在..

于 2013-08-12T13:25:08.393 回答
-1

试试这个(未测试)

$shop = array();
foreach($json as $var)
{
    if(!in_array($var['shop'], $shop)){
        $shops[$var['shop']] = array('revenue' => 0, 'numberofbooks' => 0);
    }
}
于 2013-08-12T13:26:38.450 回答