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