Jump to content

Accessing the value of a Multidimensional array


sakilimran

Recommended Posts

I have assigned values for a multidimensional array as following (in opencart):

 

				    $this->data['price_list'][0]['115']['296']= "8.99";
				    $this->data['price_list'][1]['115']['396']= "8.99";
				    $this->data['price_list'][2]['115']['446']= "8.99";
				    $this->data['price_list'][3]['115']['496']= "8.99";
				    $this->data['price_list'][4]['115']['596']= "12.99";
				    $this->data['price_list'][5]['115']['796']= "";
				    $this->data['price_list'][6]['115']['996']= "";
				    $this->data['price_list'][7]['115']['1196']= "";
				    $this->data['price_list'][8]['140']['296']= "12.99";
				    $this->data['price_list'][9]['140']['396']= "12.99";
				    $this->data['price_list'][10]['140']['446']= "12.99";

 

 

I have two variables: $height and $width. I want to check in the above array as following:

 

let, $height= 140, $ width= 396. I want the value 12.99. How I'll check this?

Ok, Thanks for quick reply. Ive removed the 0-10's and doing the following in view page(.tpl file):

 

print_r($price_list); and getting following result:

Array
(
   [115] => Array
    (
	    [296] => 8.99
	    [396] => 8.99
	    [446] => 8.99
	    [496] => 8.99
	    [596] => 12.99
	    [796] =>
	    [996] =>
	    [1196] =>
    )
   [140] => Array
    (
	    [296] => 12.99
	    [396] => 12.99
	    [446] => 12.99
	    [496] => 12.99
	    [596] => 12.99
	    [796] =>
	    [996] =>
	    [1196] =>
    )
   [175] => Array
    (
	    [296] => 13.99
	    [396] => 13.99
	    [466] => 13.99
	    [496] => 13.99
	    [596] => 14.99
	    [796] => 26.99
	    [996] => 26.99
	    [1196] =>
    )
   [215] => Array
    (
	    [296] =>
	    [396] => 18.99
	    [446] =>
	    [496] => 18.99
	    [596] => 18.99
	    [796] => 26.99
	    [996] => 26.99
	    [1196] =>
    )
)
.
.
.
.

 

 

As I am a new php user I am not sure about the syntax how to access the values.

echo $price_list[$height][$width];

 

You use the $height variable as the first key value (140) and $width as the second key value (396).  You may get E_NOTICE messages if there is no match for your width/height combo, eg if you had $width=100, $height=100.  You could solve that with some isset checks prior to trying to access the value.

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.