sakilimran Posted January 15, 2013 Share Posted January 15, 2013 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? Link to comment https://forums.phpfreaks.com/topic/273205-accessing-the-value-of-a-multidimensional-array/ Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 Can you remove the useless [0]-[10]s? If so do that and it'll be a lot easier. Otherwise loop through $this->data['price_list'] and check it there. Link to comment https://forums.phpfreaks.com/topic/273205-accessing-the-value-of-a-multidimensional-array/#findComment-1405938 Share on other sites More sharing options...
sakilimran Posted January 15, 2013 Author Share Posted January 15, 2013 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. Link to comment https://forums.phpfreaks.com/topic/273205-accessing-the-value-of-a-multidimensional-array/#findComment-1405955 Share on other sites More sharing options...
kicken Posted January 15, 2013 Share Posted January 15, 2013 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. Link to comment https://forums.phpfreaks.com/topic/273205-accessing-the-value-of-a-multidimensional-array/#findComment-1405962 Share on other sites More sharing options...
sakilimran Posted January 16, 2013 Author Share Posted January 16, 2013 Thanks Kicken, it worked. Link to comment https://forums.phpfreaks.com/topic/273205-accessing-the-value-of-a-multidimensional-array/#findComment-1406031 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.