Jump to content

accessing array key


wds

Recommended Posts

Here is my array:

 

$cart = array(

$image => array(

"four" => array(

$four

),

"five" => array(

$five

),

"eight" => array(

$eight

)

)

);

 

Now, what I want to do is organize all data within this array onto a table so the customer may view what they are about to purchase. The columns will be the thumbnail image, and then the values for each: 4x6, 5x7, and 8x10. Denoting the number of each kind of photo they would like to receive. Now the problem is I don't understand how I go about accessing all of the $image keys, (i.e. 1314.jpg, 4852.jpg, 2148.jpg).

 

$cart[1314.jpg][0][0] <- i don't want the value stored in this array, i just want the 1314.jpg, as well as all other $image keys stored in the array.

 

what i want:

 

<tr>

<td>

print($image);

</td>

<td>

print($cart[$image][0][0]);

</td>

<td>

print($cart[$image][1][0]);

</td>

<td>

print($cart[$image][2][0]);

</td>

</tr>

 

 

I understand with a numerical array I simply have a while statement such as this to go about outputting all of my image values, but i want to access all of the keys in the $cart.

 

$i = 0;

while($image[$i] != null){

    print($image[$i]);

    $i++;

}

 

I hope that I have provided a good description of my objective, thanks for your help.

 

Link to comment
https://forums.phpfreaks.com/topic/61580-accessing-array-key/
Share on other sites

array_keys() function returns all the keys in an array, maybe that's what your looking for?

 

e.g.

$keys = array_keys($cart);
$count = count($keys);
for($i=0;$i<$count;$i++){
$key = $keys[$i];
print "
<tr>
<td>
print($key);
</td>
<td>
print($cart[$key][0][0]);
</td>
<td>
print($cart[$key][1][0]);
</td>
<td>
print($cart[$key][2][0]);
</td>
</tr>";
}

Link to comment
https://forums.phpfreaks.com/topic/61580-accessing-array-key/#findComment-306485
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.