dsxd62219570 2012-06-13 00:05
浏览 79
已采纳

获取子数组键php

i have an array:

Array
( 
[47] => Array
    (
        [name] => 3543 good
        [price] => 100.0000
        [image] => data/hp_1.jpg
        [discount] => 
        [stock_status] => 
        [weight_class] => kg
    )

[28] => Array
    (
        [name] => HTC Touch HD
        [price] => 100.0000
        [image] => data/htc_touch_hd_1.jpg
        [discount] => 
        [stock_status] => 
        [weight_class] => g
    )

[41] => Array
    (
        [name] => iMac
        [price] => 100.0000
        [image] => data/imac_1.jpg
        [discount] => 
        [stock_status] => 
        [weight_class] => kg
    )

[40] => Array
    (
        [name] => iPhone
        [price] => 101.0000
        [image] => data/iphone_1.jpg
        [discount] => 
        [stock_status] => 
        [weight_class] => kg
    )
)

i need the sub array key (47 ,28 etc) as it is my product id

I'm running a foreach loop to get the details and assigning to a new array e.g. 'name' => $result['name'] but can't figure out how to target the product id.

  • 写回答

4条回答 默认 最新

  • douzi7711 2012-06-13 00:07
    关注

    You can assign the key to a variable in your foreach loop:

    foreach($array as $id => $result) {
        $item = array('name' => $result['name'], 'id' => $id);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥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 动力学代码报错,维度不匹配