PHP:如何使所有数组具有相同的键

时间:2017-08-30 07:06:39

标签: php arrays

目前,我尝试在包含不同形状信息的网站上进行报废。

我需要成为CSV文件的最终结果。问题是具有不同页面形状的页面需要不同的字段。

让我们说,在报废后,我有以下数组:

Array
(
    [0] => Array(
        'key-1' => 'value #1',
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    [2] => Array(
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    [3] => Array(
        'key-1' => 'value #1',
        'key-3' => 'value #3',
    ),
    [4] => Array(
        'key-2' => 'value #2',
    ),
)

那么,我怎样才能让这个数组看起来像下一个呢?

Array
(
    [0] => Array(
        'key-1' => 'value #1',
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    [2] => Array(
        'key-1' =>
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    [3] => Array(
        'key-1' => 'value #1',
        'key-2' =>
        'key-3' => 'value #3',
    ),
    [4] => Array(
        'key-1' =>
        'key-2' => 'value #2'
        'key-3' =>
    ),
)

有没有办法?请记住,每个表都有20个键,最终的数组将有大约2000个项目。

实际上,我正在寻找最佳性能解决方案,以避免计算机在尝试处理数据时崩溃。

更新#1: 从一开始就不知道丢失的密钥。几乎每种产品都有不同的领域,每种产品只有少数相同。

更新#2: 这是CSV的可能示例输出

key-1,key-2,key-3
value #1,value #2,value #3
,value #2,value #3
value #1,,value #3
,value #2,

3 个答案:

答案 0 :(得分:2)

完整的解决方案是:

// your source array
$a = Array
(
    Array(
        'key-1' => 'value #1',
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    Array(
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    Array(
        'key-1' => 'value #1',
        'key-3' => 'value #3',
    ),
    Array(
        'key-2' => 'value #2',
    ),
);

// storage for keys
$keys = [];

foreach ($a as $row) {
    // keys of current row
    $current_keys = array_keys($row);
    // find keys that are not already in a `$keys`
    $diff = array_diff($current_keys, $keys);

    // if keys found - add them to `$keys`
    if ($diff) {
        $keys = array_merge($keys, $diff);
    }

}

echo'<pre>',print_r($keys),'</pre>';    // check values of keys

foreach ($a as $row) {
    $rowForCsv = [];
    // check what keys exist in current `$row`
    foreach ($keys as $key) {
        $rowForCsv[$key] = !empty($row[$key]) ? $row[$key] : '';
    }
    // add `$rowForCsv` to CSV file
}

答案 1 :(得分:1)

[akshay@localhost tmp]$ cat test.php
<?php


$array = array
(
    array(
        'key-1' => 'value #1',
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    array(
        'key-2' => 'value #2',
        'key-3' => 'value #3',
    ),
    array(
        'key-1' => 'value #1',
        'key-3' => 'value #3',
    ),
    array(
        'key-2' => 'value #2',
    ),
);

   $first_element = array_keys( $array[0] );
   foreach($array as &$item)
   {
            foreach($first_element as $key)
            {
                     if(!array_key_exists($key, $item))
                     {
                              $item[$key]='';
                     }
            }
        ksort($item);
   }
   unset($item);
   print_r($array);

?>

测试结果

[akshay@localhost tmp]$ php test.php

Array
(
    [0] => Array
        (
            [key-1] => value #1
            [key-2] => value #2
            [key-3] => value #3
        )

    [1] => Array
        (
            [key-1] => 
            [key-2] => value #2
            [key-3] => value #3
        )

    [2] => Array
        (
            [key-1] => value #1
            [key-2] => 
            [key-3] => value #3
        )

    [3] => Array
        (
            [key-1] => 
            [key-2] => value #2
            [key-3] => 
        )

)

<强> 输出

{{1}}

答案 2 :(得分:1)

希望我最简单的帖子也会有所帮助。我们在这里使用array_maparray_keysarray_merge_recursivearray_diffsplat运算符。

Try this code snippet here

$allKeys=  array_map('array_keys', $array);//getting all keys of complete array
$result=array_unique(array_merge_recursive(...$allKeys));//getting unique keys
foreach ($array as $key => &$value){
    $data=array_diff($result, array_keys($value));//remaining keys
    if(count($data)!=0)
    {
        $newData=array();
        foreach($result as $key => $resultValue)
        {
            $newData[$resultValue]=isset($value[$resultValue]) ? $value[$resultValue] : "";
        }
        $value=$newData;//filled new data with absent keys
    }
}
print_r($array);