doudu9652 2013-07-30 11:13
浏览 70
已采纳

php - 将数组推入数组 - (同时推送键和数组)

I am trying to add an array to an existing array. I am able to add the array using the array_push . The only problem is that when trying to add array that contains an array keys, it adds an extra array within the existing array.

It might be best if I show to you

foreach ($fields as $f)
{  
    if ($f == 'Thumbnail')
    {
        $thumnail = array('Thumbnail' => Assets::getProductThumbnail($row['id'] );
        array_push($newrow, $thumnail);
    }
    else
    { 
        $newrow[$f] = $row[$f];
    }
}

The fields array above is part of an array that has been dynamically fed from an SQl query it is then fed into a new array called $newrow. However, to this $newrow array, I need to add the thumbnail array fields .

Below is the output ( using var_dump) from the above code. The only problem with the code is that I don't want to create a seperate array within the arrays. I just need it to be added to the array.

 array(4) { ["Product ID"]=> string(7) "1007520"
           ["SKU"]=> string(5) "G1505"
           ["Name"]=> string(22) "150mm Oval Scale Ruler"            
           array(1) { ["Thumbnail"]=> string(77) "thumbnails/products/5036228.jpg" } }

I would really appreciate any advice.

  • 写回答

3条回答 默认 最新

  • duan1443 2013-07-30 12:22
    关注

    All you really want is:

    $newrow['Thumbnail'] = Assets::getProductThumbnail($row['id']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配