0

I am working on a project to get the names of an array. The arrays seem to be multidimensional, with the added bonus of being a stdclass Object. I am trying to select a key from the provided array but seem to have no luck selecting them.

echo($response->array[shoecompany]->array[1]->name);

from the information here

    stdClass Object
(
    [shoe] => shoemaker
    [shoecompany] => Array
        (
            [0] => stdClass Object
                (
                    [shoenumber] => 1
                    [name] => Blank

            [1] => stdClass Object
                (
                    [shoenumber] => 2
                    [name] => demo
                )

            [2] => stdClass Object
                (
                    [shoenumber] => certificate
                    [name] => certofsale

                )

        )

)

Nothing i do seems to pull the information i need out of this. Any ways to go about pulling, said information.

4
  • $response= json_decode(json_encode($response), true); echo($response['shoecompany'][1]['name']); Commented Aug 23, 2017 at 10:54
  • ksjohn answer should fix your problem. Are you aware there is a typo in your json ? shoecomapny instead of shoecompany. Commented Aug 23, 2017 at 11:05
  • yes i am aware lol thank you both Commented Aug 23, 2017 at 11:43
  • $response->array[shoecompany] -- there is no array key in $response. Read about arrays and objects in PHP. Commented Aug 23, 2017 at 12:17

2 Answers 2

2

The arrays seem to be multidimensional, with the added bonus of being a stdclass Object.

Arrays and objects aren't the same things.

I let you learn more about the specifics of both if you are curious.

Regarding access, yous use brackets - '[]' - when you want to access something in an array and an arrow - '->' - when you want to access an object's property :

$array['key'];

$object->property;

In your case, since only $response and the entries in the entry showcompany - I assume it's a typo - are objects, what you should write is :

$response->shoecompany[1]->name;

Which gives you in practical use :

foreach ($response->shoecompany as $val) {
    echo $val->shoenumber, ' : ', $val->name, '<br>'; // Or whatever you want to print, that's for the sake of providing an example
}

If it is more convenient for you to handle exclusively arrays, you can also use get_object_vars() to convert an object properties to an array :

$response = get_object_vars($response);
Sign up to request clarification or add additional context in comments.

6 Comments

this helped a lot. would it be easy to attach this to a foreach command?
To attach what ? For what purpose ?
foreach shoenumber run a script till dead
That would be quite simple, have you tried it yet ?
in the practical use you provided it throws an error i had to change it to foreach ($response->shoecompany[0] as $val) it since i had to designate the shoe company would i be able to search for more than one at a time or have to make separate foreach calls for each shoecompany?
|
1

Code should be like:

echo $response->shoecomapny[1]->name;

In short, to select key inside an object you need to use "->" operator and to select key inside array use "[]".

3 Comments

its useful to explain why this works if you can, rather than just put corrected code up.
this was definitely helpful
@iDaRkkO, If this answer useful to you and it's correct then you should mark it as the correct answer. It will be helpful for others.

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.