PHP:将多维数组转换为单维数组

时间:2022-11-16 21:31:36

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

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

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

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.

这不是问题,但问题是服务不断更改这些项的索引,因此在下一个数组中,Price可以是1而不是0。

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']?

如何有效地将这样的数组转换为单维数组,以便通过$ var ['Size']而不是$ var [1] ['Size']来访问变量?

Appreciate your help

感谢你的帮助

7 个解决方案

#1


9  

Like this:

喜欢这个:

$result = array();

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

The $result array would now look like this:

$ result数组现在看起来像这样:

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

#2


30  

$result = call_user_func_array('array_merge', $array);

#3


7  

I am using laravel's helper: http://laravel.com/api/source-function-array_flatten.html#179-192

我正在使用laravel的助手:http://laravel.com/api/source-function-array_flatten.html#179-192

function array_flatten($array)
    {
        $return = array();

        array_walk_recursive($array, function($x) use (&$return) { $return[] = $x; });

        return $return;
    }

#4


4  

function flattenArray($input, $maxdepth = NULL, $depth = 0)
{
    if(!is_array($input)){ 
      return $input;
    }

    $depth++;
    $array = array(); 
    foreach($input as $key=>$value){
      if(($depth <= $maxdepth or is_null($maxdepth)) && is_array($value)){
        $array = array_merge($array, flattenArray($value, $maxdepth, $depth));
      } else {
        array_push($array, $value);
        // or $array[$key] = $value;
      }
    }
    return $array;
}

#5


0  

Consider $mArray as multidimensional array and $sArray as single dimensional array this code will ignore the parent array

将$ mArray视为多维数组,将$ sArray视为单维数组,此代码将忽略父数组

function flatten_array($mArray) {
    $sArray = array();

    foreach ($mArray as $row) {
        if ( !(is_array($row)) ) {
            if($sArray[] = $row){
            }
        } else {
            $sArray = array_merge($sArray,flatten_array($row));
        }
    }
    return $sArray;
}

#6


0  

I think i found best solution to this :

我想我找到了最好的解决方案:

array_walk_recursive($yourOLDmultidimarray, function ($item, $key) {
    //echo "$key holds $item\n";
$yourNEWarray[]=$item;
});

#7


0  

If you use php >= 5.6, you may use array unpacking (it's much faster):

如果你使用php> = 5.6,你可以使用数组解包(它更快):

$result = array_merge(...$array);

See wiki.php.net on unpacking

在解压缩时请参阅wiki.php.net

#1


9  

Like this:

喜欢这个:

$result = array();

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

The $result array would now look like this:

$ result数组现在看起来像这样:

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

#2


30  

$result = call_user_func_array('array_merge', $array);

#3


7  

I am using laravel's helper: http://laravel.com/api/source-function-array_flatten.html#179-192

我正在使用laravel的助手:http://laravel.com/api/source-function-array_flatten.html#179-192

function array_flatten($array)
    {
        $return = array();

        array_walk_recursive($array, function($x) use (&$return) { $return[] = $x; });

        return $return;
    }

#4


4  

function flattenArray($input, $maxdepth = NULL, $depth = 0)
{
    if(!is_array($input)){ 
      return $input;
    }

    $depth++;
    $array = array(); 
    foreach($input as $key=>$value){
      if(($depth <= $maxdepth or is_null($maxdepth)) && is_array($value)){
        $array = array_merge($array, flattenArray($value, $maxdepth, $depth));
      } else {
        array_push($array, $value);
        // or $array[$key] = $value;
      }
    }
    return $array;
}

#5


0  

Consider $mArray as multidimensional array and $sArray as single dimensional array this code will ignore the parent array

将$ mArray视为多维数组,将$ sArray视为单维数组,此代码将忽略父数组

function flatten_array($mArray) {
    $sArray = array();

    foreach ($mArray as $row) {
        if ( !(is_array($row)) ) {
            if($sArray[] = $row){
            }
        } else {
            $sArray = array_merge($sArray,flatten_array($row));
        }
    }
    return $sArray;
}

#6


0  

I think i found best solution to this :

我想我找到了最好的解决方案:

array_walk_recursive($yourOLDmultidimarray, function ($item, $key) {
    //echo "$key holds $item\n";
$yourNEWarray[]=$item;
});

#7


0  

If you use php >= 5.6, you may use array unpacking (it's much faster):

如果你使用php> = 5.6,你可以使用数组解包(它更快):

$result = array_merge(...$array);

See wiki.php.net on unpacking

在解压缩时请参阅wiki.php.net