xProteuSx Posted September 10, 2016 Share Posted September 10, 2016 PHPFreaks folks, How in the heck do I name an array within an array (multidimensional array), so that I can bring it up by array name instead of array number? Please help me out here -- its over my head, I guess. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 10, 2016 Share Posted September 10, 2016 (edited) An array inside of an array doesn't have any 'name'. An array is one variable name. If you have an array inside an array it is simply an element/index of that array. $array1 = array('A'=>'valuea','B'=>'valueb','C'=>array('x','y','z'),'D'=>'valued'); You have a php var called $array1 here. In that var you have 4 elements - index 'A', index 'B', index 'C' & index 'D'. Within one of those elements you have an array buried containing 3 elements with numeric keys and the values of 'x', 'y', & 'z'. You reference the array as $array1['A'] $array1['B'] $array1['C'] $array1['D'] You can also reference the C element this way $array1['C'][0], $array1['C'][1] & $array1['C'][2] Referencing the A, B & D elements will give you the values you expect. Referencing $array1['C'] will give you an array. You will have to handle it differently by checking if the type of the element is in fact an Array and then loop thru that array's elements. A complete dump of the above: foreach($array1 as $k=>$v) { if (is_array($v)) { echo 'element $k contains '; foreach ($v as $k2=>$v2) echo "$k2 = $v2 "; } else echo "element $k is $v "; } Hope this gets you seeing it. Edited September 10, 2016 by ginerjm Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.