合并多维数组并在PHP中添加默认值

时间:2014-01-12 18:10:14

标签: php arrays loops multidimensional-array

我有一个这样的多维数组,我想从中创建一个新数组并添加默认值。第一个数组的键是固定的(00,01,02,03),但次要键中的键是动态值

Array
(  
[00] => Array
    (
        [KEY 1] => 100
        [KEY 2] => 50
        [KEY 3] => 30
    )

[01] => Array
    (
        [KEY 1] => 40
        [KEY 2] => 100
        [KEY 4] => 200
    )

[02] => Array
    (
        [KEY 1] => 30
        [KEY 2] => 40
        [KEY 3] => 30
    )

[03] => Array
    (
        [KEY 5] => 30
    )

 )

所以我的问题是如何将上面的数组变成这样的?

Array
(  
[00] => Array
    (
        [KEY 1] => 10
        [KEY 2] => 50
        [KEY 3] => 30
        [KEY 4] => 0
        [KEY 5] => 0
    )

[01] => Array
    (
        [KEY 1] => 40
        [KEY 2] => 100
        [KEY 3] => 0
        [KEY 4] => 200
        [KEY 5] => 0
    )

[02] => Array
    (
        [KEY 1] => 30
        [KEY 2] => 40
        [KEY 3] => 30
        [KEY 4] => 0
        [KEY 5] => 0
    )

[03] => Array
    (
       [KEY 1] => 0
       [KEY 2] => 0
       [KEY 3] => 0
       [KEY 4] => 0
       [KEY 5] => 30
    )

我一整天都在挣扎,我的头已经累了。有人可以帮忙吗?

//这是我正在使用的代码

CSV文件的内容

"00","KEY 1",100
"00","KEY 2",50
"00","KEY 3",30
"01","KEY 1",40
"01","KEY 2",100
"01","KEY 4",200
"02","KEY 1",30
"02","KEY 2",40
"02","KEY 3",30
"03","KEY 5",30

这是代码

$csvFile='export (82).csv';


$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle)) {
    $line_of_text[] = fgetcsv($file_handle, 1024);
 }
 foreach ($arr as $key => $val) {
    foreach ($line_of_text as $v) {
        if (!isset($val[$v]))
            $arr[$key][$v] = 0;
    }
 }
echo"<pre>";
 print_r($arr);
 echo"</pre>";

//创建多维数组的代码

$fp = fopen($csvFile, 'r');
$master = array();
while( $line = fgetcsv( $fp ) ) {

if( !isset( $master[$line[0]] ) )
    $master[$line[0]] = array();

if( !isset( $master[$line[0]][$line[1]] ) )
    $master[$line[0]][$line[1]] = 0;

$n = filter_var($line[2], FILTER_SANITIZE_NUMBER_INT);
$master[$line[0]][$line[1]] += $n;
}

4 个答案:

答案 0 :(得分:2)

如果您的输入数组是$array

// merge inner arrays to get an array that has a value for every key
$merged = call_user_func_array('array_merge', $array);

// extract the keys from that array
$keys = array_keys($merged);

// build array that has value `0` for each key
$defaults = array_fill_keys($keys, 0);

// loop over the input array, adding values for missing keys
foreach (array_keys($array) as $key) {
    $array[$key] += $defaults;
}

请注意,要使array_merge得到所需的结果,内部数组中的键必须是字符串。

答案 1 :(得分:1)

我认为这就是你要找的东西。

$defaults = [
    'KEY 1' => 0,
    'KEY 2' => 0,
    'KEY 3' => 0,
    'KEY 4' => 0,
    'KEY 5' => 0,
];

foreach ($values as &$v) {
    $v += $defaults;
}
unset($v);

答案 2 :(得分:1)

我认为你需要这个:

$keys = Array("A", "B", "C", "D", "E");

$x = Array(
    Array( "A" => 10, "C" => 10),
    Array( "A" => 10, "D" => 20)
);

// Loop through $x
foreach($x as &$value){
    // Loop through the array 
    foreach($keys as $key){

        if(!in_array($key, array_keys($value))) {
            $value[ $key ] = 0;
        }
    }
}
var_dump($x);

输出:

array(2) { 
      [0]=> array(5) { 
          ["A"]=> int(10) 
          ["C"]=> int(10) 
          ["B"]=> int(0) 
          ["D"]=> int(0) 
          ["E"]=> int(0) 
      } 
      [1]=> &array(5) { 
          ["A"]=> int(10) 
          ["D"]=> int(20) 
          ["B"]=> int(0) 
          ["C"]=> int(0) 
          ["E"]=> int(0) 
      } 
}

答案 3 :(得分:0)

$arr = array(
            00 => array
                (
                'KEY1' => 100,
                'KEY2' => 50,
                'KEY3' => 30,
            ),
            01 => array
                (
                'KEY1' => 40,
                'KEY2' => 100,
                'KEY4' => 200,
            ),
            02 => array
                (
                'KEY1' => 30,
                'KEY2' => 40,
                'KEY3' => 30,
            )
            ,
            03 => array
                (
                'KEY5' => 30,
            ),
        );
        $csv = array_unique(explode(",", str_replace("\n", ",", file_get_contents("csv.csv"))));
        foreach ($csv as $key => $value) {
            $csv[$key] = str_replace("\"","", $value);
        }

        foreach ($arr as $key => $val) {
            foreach ($csv as $v) {
                if (!isset($val[$v]))
                    $arr[$key][$v] = 0;
            }
        }
        print_r($arr);