php - Manually creating an associative array -
i'm trying create associative array dynamic data, , having trouble.
i'd produce array looks following while fetching rows mysql query.
array ( [0] = array ( [name] => first ) [1] = array ( [name] => second ) [2] = array ( [name] => third ) [3] = array ( [name] => fourth ) [4] = array ( [name] => fifth ) ) i've been trying use array_merge, it's not giving me result want. array_merge apparently doesn't operate same inside foreach outside (i ran same code , without loop, without worked way need).
basically, i'm doing (which doesn't work):
foreach($idlist $id) { $arr[] = array_merge(array(), array('name' => $id)); } this gives me output this:
array ( [0] = array ( [name] => first ) [1] = array ( [0] = array ( [name] => first ) [name] => second ) [2] = array ( [0] = array ( [name] => first ) [1] = array ( [0] = array ( [name] => first ) [name] => second ) [name] => third ) )
you've got few issues here.
mainly, can't have same index twice. 'name' can index once , once, you're 'desired' output impossible.
also, statement pretty problematic
foreach($idlist $id) { $arr[] = array_merge(array(), array('name' => $id)); } the use of $arr[] = $x push. adds new element of array, numerically indexed.
your use of array_merge unnecessary. array_merge returns second argument merged on first argument. trying add single new element. also, line used or did use array_merge($arr, array('name' => $id)); ???
try:
foreach($idlist $id) { $arr[] = array('name' => $id); } and get:
array ( [0] = array ( [name] => first ) [1] = array ( [name] => second } .... and on. i'm not sure if want, proposed in first place isn't possible.
Comments
Post a Comment