获取子数组键的PHP [英] get sub array key php

查看:75
本文介绍了获取子数组键的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

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
    )
)

我需要子阵列键(47、28等),因为它是我的产品ID

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

我正在运行一个foreach循环以获取详细信息并分配给新数组,例如'name' => $result['name'],但无法确定如何定位产品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.

推荐答案

您可以在foreach循环中将键分配给变量:

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

foreach($array as $id => $result) {
    $item = array('name' => $result['name'], 'id' => $id);
}

这篇关于获取子数组键的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆