0

I'm trying to insert a value key pair in an array in php. The code is given below but the issue is that it is giving error Illegal offset type on the line where I'm trying to push data. $request_url is the data coming from API.

$response = simplexml_load_file($request_url);
//make different array of images of small , medium and large size
$array_features = array();
$array_smallImages = array();
$array_mediumImage = array();
$array_largeImage = array();

foreach($response->Items->Item as $item){
    echo $item->ItemAttributes->Title.'<br>';   
    echo $item->ASIN.'<br>';
    $asin = $item->ASIN;
    echo $item->DetailPageURL.'<br>';
    echo $item->ItemAttributes->Manufacturer.'<br>';
    $small_img = $item->SmallImage->URL;
    $array_smallImages[$asin] = $small_img; 
    //$array_smallImages =  $item->SmallImage->URL;
    echo $item->MediumImage->URL.'<br>';
    echo $item->LargeImage->URL.'<br>';
    //echo $item->ItemAttributes->Manufacturer.'<br>';  
    echo 'Features:'.'<br>';
    foreach($item->ItemAttributes->Feature as $fea){
    //  $array_features[$item->ASIN] = $fea;
        echo $fea.'<br>';
    }

$array_smallImages[$asin] = $small_img; is the line on which error is coming Warning: Illegal offset type

smarttechy
  • 852
  • 1
  • 9
  • 23
  • Possible duplicate of [How to insert a new key value pair in array in php?](http://stackoverflow.com/questions/17524484/how-to-insert-a-new-key-value-pair-in-array-in-php) – LF00 Sep 13 '16 at 05:41
  • @KrisRoofe used that method and getting error. I'm here talking about the error. – smarttechy Sep 13 '16 at 05:47
  • your $asin should be string or num. not object. – LF00 Sep 13 '16 at 05:50
  • Possible duplicate of [php - How do I fix this illegal offset type error](http://stackoverflow.com/questions/2732451/php-how-do-i-fix-this-illegal-offset-type-error) – iliaz Sep 13 '16 at 05:59

1 Answers1

0

Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key

Use trim($asin) before $array_smallImages[$asin] = $small_img;

jophab
  • 5,356
  • 14
  • 41
  • 60