dongshang5862 2016-09-12 21:36 采纳率: 0%
浏览 395
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dongwu5743 2016-09-12 21:52
    关注

    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;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部