【问题标题】:How to insert value key-value pair in array如何在数组中插入值键值对
【发布时间】:2017-01-20 15:07:00
【问题描述】:

我正在尝试在 php.ini 的数组中插入一个值键对。下面给出了代码,但问题是它在我尝试推送数据的行上给出了错误 Illegal offset type 。 $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; 是即将出现错误的行 警告:非法偏移类型

【问题讨论】:

标签: php arrays


【解决方案1】:

当您尝试使用对象或数组作为索引键访问数组索引时会发生非法偏移类型错误

$array_smallImages[$asin] = $small_img;之前使用trim($asin)

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2013-07-05
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多