up vote 0 down vote favorite

Is it possible to array_push to a multidimensional array?

Creating the array using:

$ObjectArray = array();
$ShiftArray = array($ObjectArray);
$WeekShiftArray = array($ShiftArray);
$MasterShiftArray = array($WeekShiftArray);

And trying to push to the array using

array_push($MasterShiftArray[$last_monday_from_date][$CurrentShift->Offset][$CurrentShift->Shift], $CurrentShift);

But I'm getting:

Warning: array_push() expects parameter 1 to be array, object given in /opt/lampp/htdocs/sandboxj/blog/wp-content/plugins/Shifty/AddShift.php on line 94

Any help would be appreciated.

link|flag

2 Answers

up vote 0 down vote accepted

As the name implies, $ObjectArray is probably an array of objects, not an array of arrays. So you are trying to push values into an object.

EDIT
I'm assuming you're not showing the actual contents of $ObjectArray here BTW. Is this correct?

EDIT 2
Hmm, I've tested it now, and the levels you are addressing work fine with the example you're giving, even if the $objectArray holds objects. So there is probably something else going on here which we're not seeing.

Are you sure the $ObjectArray is an array at the time of addressing it?

EDIT 3
What do you get when you do a var_dump( $ObjectArray ) right before the array_push call?

link|flag
up vote 0 down vote

you could just do

$MasterShiftArray[$last_monday_from_date][$CurrentShift->Offset][$CurrentShift->Shift][] = $CurrentShift;
link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.