Page 1 of 1

Looping over a multi dimensional arrays

Posted: Wed Oct 13, 2004 9:13 am
by dubs
Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

<?php  
session_start();
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);

if(!isset($_SESSION_VARS['cart']))
	{					
		$_SESSION_VARS['cart']= array();
		
		$_SESSION_VARS['cart'][] = array('Test1',0,0); 
		$_SESSION_VARS['cart'][] = array('Test2',0,0); 
		$_SESSION_VARS['cart'][] = array('Test3',0,0); 
		$_SESSION_VARS['items'] = 1;
		$_SESSION_VARS['Total Price']= 1.00;	 		
	
	foreach($_SESSION_VARS['cart'] as $current){
		foreach($current as $attribute){
			echo  . $attribute . '<br>';
		}
	}
	//echo count($_SESSION_VARS['cart']);
			
	}
At the moment my loop returns all my product attributes but it doesn't give me the row number or colum number. How do I access these variables / keys.

Re: Looping over a multi dimensional arrays

Posted: Wed Oct 13, 2004 10:00 am
by nigma
Try changing your second foreach loop to match the second one in the following example.

Code: Select all

foreach($_SESSION_VARS['cart'] as $current){
		foreach($current as $key=>$val){
			echo  "$key,$val<br />";		}
	}

Re: Looping over a multi dimensional arrays

Posted: Wed Oct 13, 2004 10:16 am
by dubs
Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

[quote="nigma"]Try changing your second foreach loop to match the second one in the following example.

Code: Select all

foreach($_SESSION_VARS['cart'] as $current){
		foreach($current as $key=>$val){
			echo  "$key,$val<br />";		}
	}
[/quote]

Cheers for the reply, that did work...however, I've just realised that I'm accessing the wrong array. What I'm trying to to is loop over my array and compare a specific value with a variable I'm going to pass it. If the comparison matches then I going to need to change its value...basically I'm building a shopping cart.

Code: Select all

if(!isset($_SESSION_VARS['cart']))
	{					
		$_SESSION_VARS['cart']= array();
		
		$_SESSION_VARS['cart'][] = array('Test1',0,0); 
		$_SESSION_VARS['cart'][] = array('Test2',0,0); 
		$_SESSION_VARS['cart'][] = array('Test3',0,0); 
		$_SESSION_VARS['items'] = 1;
		$_SESSION_VARS['Total Price']= 1.00;	 		
	
	
	foreach($_SESSION_VARS['cart'] as $key => $val){
		if ($val[0] == 'Test2'){
			
		}
If $val[0] is equal to Test2, I want to change the third column in my array to 5. I think I need the row number to achieve this..but how do i get it.

$HTTP_SESSION_VARS['cart'][???][2] = 5

Cheers

Richard

Posted: Wed Oct 13, 2004 10:24 am
by nigma

Code: Select all

foreach($_SESSION_VARS['cart'] as $key => $val)  { 
  if ($val[0] == "test2") {
    $_SESSION_VARS['cart'][$key][2] = "newval";
  }
}