开发者

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_p开发者_如何学Cush() 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.

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?


you could just do

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜