简体   繁体   English

如何在数组中插入值键值对

[英]How to insert value key-value pair in array

I'm trying to insert a value key pair in an array in php. 我正在尝试在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. $ request_url是来自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; $ 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; $array_smallImages[$asin] = $small_img;之前使用trim($asin) $array_smallImages[$asin] = $small_img;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM