Jump to content

Name of Array Within an Array


xProteuSx

Recommended Posts

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 by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.