开发者

Php cannot use object stdClass as array [closed]

开发者 https://www.devze.com 2023-02-22 00:05 出处:网络
It's difficult to tell what is b开发者_StackOverflow社区eing asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its curr
It's difficult to tell what is b开发者_StackOverflow社区eing asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Derp. Getting 'Fatal error: Cannot use object of type stdClass as array'

I dunno what I'm doing wrong here:

foreach ($json->result->items[$key]->attributes->attribute as $attrib => $val) 
{
    if($json->result->items[$key]->attributes->attribute[$attrib]->name == 'cannot_trade')
    {
        $notrade=1;
        echo 'Item ' . $key . ' is not tradeable' . $br;
    }
}

And here's the data:

[attributes] => stdClass Object
(
  [attribute] => Array
  (
    [0] => stdClass Object
    (
      [name] => custom employee number
      [class] => set_employee_number
      [value] => 0
    )
    [1] => stdClass Object
    (
      [name] => cannot trade
      [class] => cannot_trade
      [value] => 1
    )
  )
)

Essentially, I'm trying to test to see if the 'attribute' array has the cannot_trade thingy. Sometimes, the parent object doesn't have an 'attributes' object


You can parse your JSON as an array if you want:

// These both give you an array:
$json = json_decode($whatever, true);
$json = (array) json_decode($whatever);


You could try

if($val->name == 'cannot_trade')
{
    $notrade=1;
    echo 'Item ' . $key . ' is not tradeable' . $br;
}

If it still doesn't works, try adding

var_dump($val);

in the loop, to check what it really contains.

0

精彩评论

暂无评论...
验证码 换一张
取 消