-1

I have a array of string of stdClass objects but i am not able to parse the string correctly

I have tried various methods but all gives the same error of "get property of non-object"

foreach($response->Product->$x as $prod)
{
print $prod->Name;
print $prod->Price;
print $prod->Stock;
print $prod->SupplierName;
print $prod->CategoryName;
$x++;
}

//But my string coming in response is

stdClass Object ( 
[Product] => Array 
( [0] => stdClass Object
( [Id] => 2 [Name] => Green Shirt [Price] => 1800 [Stock] => 50 [SupplierName] => Ali Yar [CategoryName] => Jeans )
  [1] => stdClass Object
  ( [Id] => 7 [Name] => Red Shirt with Flowers [Price] => 2200 [Stock] => 40 [SupplierName] => Ali Yar [CategoryName] => Shirt )
  [2] => stdClass Object
  ( [Id] => 8 [Name] => Red Shirt with Flowers [Price] => 2200 [Stock] => 40 [SupplierName] => Ali Yar [CategoryName] => Shirt ) 
  [3] => stdClass Object
  ( [Id] => 9 [Name] => Check Shirt Yellow [Price] => 1100 [Stock] => 200 [SupplierName] => Ali Yar [CategoryName] => Shirt 
  )
 )
 ) 

I am getting these errors Trying to get property of non-object and Invalid argument supplied for foreach()

Ali Yar Khan
  • 1,231
  • 2
  • 11
  • 33

2 Answers2

1

Your code is not clear but i think you have to edit the foreach in this

foreach($response->Product as $prod)
{
print $prod->Name;
print $prod->Price;
print $prod->Stock;
print $prod->SupplierName;
print $prod->CategoryName;
$x++;
}
1

As Product property is array, then:

foreach($response->Product as $prod)
{

    print $prod->Name;
    print $prod->Price;
    print $prod->Stock;
    print $prod->SupplierName;
    print $prod->CategoryName;

    // this increment is useless
    //$x++;

}
u_mulder
  • 54,101
  • 5
  • 48
  • 64