• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Help Accessing my mulitdimensional array

This code is kind of choppy, let me know if you need more detail.

I have a multidimensional array that I'm having trouble getting the data from.

There is a global array that gets assigned data either by editing or appending.  This code was existing and functioning:

$oID = 12345;
foreach($GLOBALS['order']->totals as $key => $total) {
	if($total['class'] == $order_total['code']) {
	// Update the information in the order
		$GLOBALS['order']->totals[$key]['title'] = $order_total['title'];
		$GLOBALS['order']->totals[$key]['value'] = $order_total['value'];
		$GLOBALS['order']->totals[$key]['text'] = $order_total['text'];

		$found = true;

if(!$found) {
	$GLOBALS['order']->totals[] = array(
	'class' => $order_total['code'],
	'title' => $order_total['title'],
	'value' => $order_total['value'],
	'text' => $order_total['text'],
	'sort_order' => $order_total['sort_order']

Open in new window

I passed the array to a function.  (I realize that is not required for a global, but I'm doing it in case I'm able to turn the $GLOBALS['order'] into a local variable);

update_order_total($oID, $GLOBALS['order'])

Open in new window

Here's the function declaration:
function update_order_total($oID, $order_total)

Open in new window

And here's my sad failed attempt inside the function to get the values from that array:

foreach ($order_total as $current_total) {
	//START test	
		$sql_data_array = array(
				'orders_id' => (int)$oID,					
			'title' => $current_total['class'],
				'text' => $current_total['text'],
				'value' => (is_numeric($current_total['value'])) ? $current_total['value'] : '0',
				'class' => 'ot_custom_' . $i,
				'sort_order' => $current_total['sort_order']
//then in insert this into a row in the database.  
//The class field and order_id field have values, and are okay. 
//The $current_total fields are blank.

Open in new window

Can you see what I'm doing wrong?

  • 3
1 Solution
Ray PaseurCommented:
Any chance you can reduce this to the SSCCE?  You only need to show us the data you have and the data you want to get from the data you have.  That will be much more effective than looking at code that we all know does not work!
christamccAuthor Commented:
It's only the last chuck of code that doesn't work, I need to specifically use the first chuck of code above, which I mentioned is working.  

The first chuck of code fills a multidimensional array. (needs to stay as is)
ie: $GLOBALS['order']->totals[] = array('class' => $order_total['code'])

Solution: I need to pass that $GLOBALS['orders'] array to a variable and then loop through that new array variable to get the values.
christamccAuthor Commented:
I figured it out.  My function call should be:

update_order_total($oID, $GLOBALS['order']->total)

rather than

update_order_total($oID, $GLOBALS['order'])

Everything else was fine.
christamccAuthor Commented:
There were no other solutions posted and this fixes my problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now