0

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);

            }
}
5
  • 2
    Your shoes index wants to be an array surely. Then you can add to it e.g. $store['shoes'][] = new \stdClass; for example? Commented Feb 2, 2018 at 15:04
  • I tried assigning it as $store['shoes'][] = $shoe but get an error Cannot use object of type stdClass as array Commented Feb 2, 2018 at 15:11
  • 1
    Yeah, because 'shoes'=> new \stdClass should probably be 'shoes'=> [], which is what I meant by the first bit of my comment :) Commented Feb 2, 2018 at 15:21
  • I made it an array and tried it now it gives me Cannot use a scalar value as an array as error Commented Feb 2, 2018 at 15:36
  • There is no thing like an "array object" in PHP. Arrays and objects are different things. And there is the ArrayObject class. Its instances are objects (and not arrays) but can be used in expressions the same way the arrays are used. Commented Feb 2, 2018 at 15:44

1 Answer 1

1

Declare your array like this:

$store = array(
    'storeId'=> $shoe->storeId,
    'store'=> $shoe->store,
    'showAll'=> false,
    'shoes'=> [],
);

Now $store['shoes'] is an array instead of an stdObject.

This means that you can add to $store['shoes'] like this:

$store['shoes'][] = $shoe;

Edit: Your variable is currently a stdClass object. I recommend converting this to an array since the rest of your data is stored as an array. You can do this easily like below:

$store['shoes'][] = json_decode(json_encode($shoe), true);

This works by encoding your object to a json string, and then decoding it to an array.

You can learn more about json_encode() here and json_decode() here.

Sign up to request clarification or add additional context in comments.

10 Comments

I made it an array and tried it now it gives me Cannot use a scalar value as an array as error
@nash63 What does $shoe equal?
a complex object. {_id:177618672, title:Adidas, description: ...}
@nash63 I'm not sure what you mean by that. Can you show me the output of print_r($shoe)?
stdClass Object ( [_id] => 5a72d285247e116ebda43053 [title] => Adidas Prophere [description] => Testing the limits of street style, adidas unveil their Prophere - a new chunky soled sneaker inspired by global fashion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.