smarttechy smarttechy - 1 month ago 5x
PHP Question

How to insert value key-value pair in array

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


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;