Hi all,

Been really trying to get a grips with objects in PHP. I think my lack of understanding isn't helping here! Basically, with the help of another user here I managed to filter some object values and add them a nice array. I used the following code:

        $i = 0;
        foreach ($itemsArray as $key => $obj2)
        {
            foreach($obj2 as $key2 => $value)
            {
                if($key2 == 'name' || $key2 == 'qty' || $key2 == 'line_total')
                {
                    $iteminfo[$i][] = $value;
                }
            }
            $i++;
        }

However, later on in my code, I want to add a different object to an array. Here I use this code:

$response[] = $html->data->identifier;

I want to know how I would use that approach in the first example, because that notatation makes more sense. The original object format looked like:

stdClass Object
(
    [356] => stdClass Object
        (
            [name] => steel wool 3
            [qty] => 1
            [other values]....
        )
)

But there is a more pressing issue. I want to the values from $iteminfo and $response into one. To do that, I am using the following under a foreach:

$response[$i][] = $qty;

I figured that this would add the $qty value underneath the value from $responses. However, I get the error: '[] operator not supported for strings'.

When I get rid of the $i and just have $response[] it works fine. Really confused what is happening here, is the $qty value not a real value at this point or is it still an object?

James I can't understand what you are trying to do , explain it in more details. It seems that for some reason you have anonymous objects stdClass what is that reason and how did you got those stdClass “objects” in first place ?

"[] operator not supported for strings" , message means what is says that [] operator is not supported for strings , that means of course that $response[$i] is a string

Hi jkson, thanks for getting back to me.

The first stdObject I get is from a wordpress plugin. $order->get_items returns the first standard object array. The first foreach code I wrote is supposed to extract the values I get from that object.

$request is an object returned from an API call, originally in JSON, I use json_decode to return an object array: $response[] = $html->data->identifier;

Therefore, $response when print_r'd looks something like this:

Array
(
    [0] => 15D49BF5826A9A
    [1] => EB99EABF5B391
)

How can $request possible be a string when it looks like that?!

What I don't understand is when the object I get from $order->get_items looks like:

stdClass Object
(
    [356] => stdClass Object
        (
            [name] => steel wool 3
            [qty] => 1
            [other values]....
        )
)

Why I can't do something like $itemsinfo->356->name like I do with $html->data->identifier. Doing the former returns null. Though like I mentioned, I'm really after trying to figure out why $response things it's a string.

Well, without really understanding what happened, I managed to resolve the issue I had. To make things simpler, I used json_decode(value, true) to turn all the objects into arrays, and then found it all easier to manipulate.

Hello James , there are some points that may help you so I will share them one by one.

Paying a bit more attention to error messages or even answers here would save you a lot of time.
You wrote :

How can $request possible be a string when it looks like that?!

I wrote:

"[] operator not supported for strings" , message means what is says that [] operator is not supported for strings , that means of course that $response[$i] is a string

As you can see the error message from PHP and also I are talking about the $response[$i] is a string not the $response.

I have no idea why any plugin would return an anonymous object stdClass , it doesn't make any sense , but you wrote that this plugin that you use does it so I will take it as fact.
You wrote:

Why I can't do something like $itemsinfo->356->name like I do with $html->data->identifier

e.g.

<?php
$arrIn = array("name"=>"steel wool","qty"=>1);
$stdObjIn = (object) $arrIn;
$arr = array(356=>$stdObjIn);
$stdObj = (object) $arr;
var_dump($stdObj);
echo "<br/>";

// This wouldn't work because 356 is not a valid property name 
// echo $stdObj->356->name; 

// typecasting that anonymous object to array
$array = (array) $stdObj;
// This will work because 365 is a valid arary key 
echo $array[365]; 
?>
commented: Thanks so much for clarifying :) +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.