简体   繁体   中英

php Multidimensional Array pushing

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.

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.

\n

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?

你可以做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM