PHP:打开多维数组一维数组 [英] PHP: Turning multidimensional arrays to single dimension arrays

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

问题描述

基本上我的应用程序与发送回一个奇怪的多维数组,如Web服务交互

Basically my app is interacting with a web service that sends back a weird multidimensional array such as:

Array
(
    [0] => Array
        (
            [Price] => 1
        )
    [1] => Array
        (
            [Size] => 7
        )
    [2] => Array
        (
            [Type] => 2
        )
)

这不是一个问题,但问题是,服务不断改变的项目的索引,所以下阵列中的价格可以是在1,而不是0。

That's not a problem, but the problem is that the service keeps changing the index of those items, so in the next array the Price could be at 1 instead of 0.

我如何effeciently变换数组像这样成一维数组,所以我可以通过$ VAR而不是$ VAR ['大小']访问的变量[1] ['大小']

How do I effeciently transform arrays like this into a single dimension array so I can access the variables through $var['Size'] instead of $var[1]['Size']?

鸭preciate你的帮助

Appreciate your help

推荐答案

这样的:

$result = array();

foreach($array as $inner) {
    $result[key($inner)] = current($inner);        
}

$结果阵列现在应该是这样的:

The $result array would now look like this:

Array
(
    [Price] => 1
    [Size] => 7
    [Type] => 2
)

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

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