Hi all,

I have a fairly limited understanding of how objects work exactly, but basically I have:

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

Which I would think stored the values as an array, but when I try to add something to that array with:
$response[] = array('quantity' => $itemsinfo['qty']);

I get the error "[] operator cannot be applied to string". This confused me since the $itemsinfo['qty'] is an array element, so I think this is because the array was created from an object?

Try dumping $itemsinfo to make sure is really an array

Yeah, it definitely is. I'm going to expand on this a bit more. I first start with an object when I go:

$itemsArray = $order->get_items();

This gives me something that looks like this:

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

)

Now I've tried for the life of me to find a way to grab those values. I was using:

foreach ($itemsArray as $name => $item)
        {
            $iteminfo[] = array(
                $name => $item->name,
                $name => $item->qty
                );
        }

But this gives me an output of null, yet everything I've looked at online says this is the correct way to go about it. Any idea why this is happening? (the code I posted first was a little further down the line, but I thought I'd go back to the start and work my way down)

Here is the result I get:

Array
(
    [0] => stdClass Object
        (
            [374] => 
        )

)
commented: piss off james +0
commented: +1 +13

Let focus on this line and its error message

  $response[] = array('quantity' => $itemsinfo['qty']);  
  // [] operator cannot be applied to string

First of all, there are two [] in this line. The $response[] and $itemsinfo['qty']. However, I don't think it is $itemsinfo because even if $itemsinfo is a string, the $itemsinfo['qty'] still is a valid syntax. Therefore, the problem is that $response is a string. Therefore, applying the $response[] = something; is not a valid syntax. In my computer, the error message will be "[] is not supported for string" which is very similiar to your error message.

commented: oi invisal, i dont like you +0
commented: +1 +13

Hi James,

$response[] = will create (if it doesn't exist) or append the right-hand value to the $response array. So if $html->data->identifier is a simple string, e.g. 'Price', then a new array would be ['Price'] or array(0=>'Price')

$order->get_items() appears to be a nested object not an array. I've created a copy and iterated through it below.

        $obj = (object) array(365 => (object) array('name'=>'steel wool 3', 'qty' => 1) );
        //print_r($obj);
        foreach($obj as $key => $obj2) {
            foreach($obj2 as $key => $value) {
                echo $value.'<br>';
            }
        }
commented: u cant code m8 +0
commented: +1 +13

Thanks, that seems to have helped. Just one more question though. I modifed the code a little to filter out the values I want:

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

And that leaves me with:

Array
(
    [0] => stdClass Object
        (
            [name] => steel wool 3
        )

    [1] => stdClass Object
        (
            [qty] => 1
        )

    [2] => stdClass Object
        (
            [line_total] => 666
        )

)

What I really want to know now is whether it's possible to extract the values from the object. I tried using get_object_vars, and the error I get is that I passed an array when the argument called for an object. As an example, I tried this:

$iteminfo = get_object_vars($iteminfo[0]);

Which according to the output above should be the object...

Ah, nvm. Simple change to $iteminfo[$i] = $value; fixed this.

Thanks!

Just noticed the inner $key should be $key2

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.