Jump to content

[SOLVED] Array Index problem


kevin7

Recommended Posts

hmmm, let say if i have the following array data

 

im building a shopping cart, and im having the following problem. the array in the bottom is the way i store customer's product and quantity. For example, refer to below, product 3's quantity is 2.

 

$item['product1'] = 1;

$item['product2'] = 4;

$item['product3'] = 2;

$item['product4'] = 1;

 

the problem im having is, how am i suppose to display the product? is it any function to list out the array index? i tried print_r, it did list out, but, it isnt the thing i want... i want an output like the following so that i can use the product id to retrieve the product information from database...

 

product1 : 1

product2 : 4

product3 : 2

product4 : 1

 

 

thank a lot!

Link to comment
Share on other sites

Hi..

        For that use the following code:

 

<?php

$item['product1'] = 1;
$item['product2'] = 4;
$item['product3'] = 2;
$item['product4'] = 1;
foreach($item as $key=>$val) 
{
        echo $key.": ".$val."<br/>";
}
?>

 

this will output like:

product1 : 1

product2 : 4

product3 : 2

product4 : 1

 

Regards,

Vijay

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.