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
https://forums.phpfreaks.com/topic/65923-solved-array-index-problem/
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

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.