Insert multidimensional associative array into multidimensional associative PHP -
hi have following (this example of attempting have allot larger):
$array= array(); array_push($array,$var['bookshopname']); $array[$var['bookshopname']]=array('opentime'=>$var1,'closetime'=>$var2); foreach($array $var) { print_r($var); }
i get:
storename1 array ( [opentime] => 12 [closetime] => 17 ) storename2 array ( [opentime] => 13 [closetime] => 19 )
so if count array there 4 elements if attempted following
foreach($array $var) { print_r($var['opentime']); }
it breaks on first result (storename1).
i want following
array( storename1 => array(opentime => ...) storename2 => array(opentime => ...) )
and getting this:
array( [0] => storename1[storename1] => array ( [opentime ] =>.... [1] => storename2[storename2] => array ( [opentime ] =>.... )
i cant quite figure out why creates these 2 results names
in response oriol
when try update further down line
for example:
$array[$var['bookshopname']] = array('opentime'=>$array[$var['bookshopname']]['opentime']+1, 'closetime'=>$array[$var['bookshopname']]['closetime']-2);
then not update values rather replaces values trying add or subtract
just use
$array= array(); $array[$var['bookshopname']]=array('opentime'=>$var1,'closetime'=>$var2);
your code creates entries because of
array_push($array,$var['bookshopname']);
maybe should read http://php.net/manual/en/function.array-push.php understand better array_push
does
edit:
if want modify values, try this
$array[$var['bookshopname']]['opentime'] += 1; $array[$var['bookshopname']]['closetime'] -= 2;
Comments
Post a Comment