dongshang5862 2016-09-13 05: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-13 05: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;

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号