I'm new to PHP. I've created an array $store
$store = array(
'storeId'=> $shoe->storeId,
'store'=> $shoe->store,
'showAll'=> false,
'shoes'=> new \stdClass
);
I'm trying to add an object $shoe to 'shoes'. I did this
$store["shoes"]= $shoe;// This successfully adds $shoe object into $store
But i'm unable to append new objects to it. I want a result like this.
$store = array(
'storeId'=> $shoe->storeId,
'store'=> $shoe->store,
'showAll'=> false,
'shoes'=> {{$shoe1}, {$shoe2}...}
);
EDIT:
foreach($product->shoes as $shoe) {
$store = searchForId($shoe->storeId, $stores);// returns True if key present
if($store === null){
$store = array(
'storeId'=> $shoe->storeId,
'store'=> $shoe->store,
'showAll'=> false,
'shoes'=> []
);
$store['shoes'][] = json_decode(json_encode($shoe), true);
array_push($stores, $store);
}
else{
$store['shoes'][] = json_decode(json_encode($shoe), true);
}
}
shoesindex wants to be an array surely. Then you can add to it e.g.$store['shoes'][] = new \stdClass;for example?$store['shoes'][] = $shoebut get an errorCannot use object of type stdClass as array'shoes'=> new \stdClassshould probably be'shoes'=> [], which is what I meant by the first bit of my comment :)Cannot use a scalar value as an arrayas errorArrayObjectclass. Its instances are objects (and not arrays) but can be used in expressions the same way the arrays are used.