2017-08-30 155 views
0

當前,我嘗試在網站中執行不同形狀的信息。PHP:如何使所有陣列具有相同的密鑰

最後的結果我需要是一個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, 
+2

你不需要值添加到陣列。寫入csv時只需添加空值。 –

+0

沒有魔法。只需使用for循環。 'for($ x = 1; $ x <= 20; $ x ++)' –

+0

@u_mulder事情是我從一開始就不知道丟失的鍵。幾乎每個我試圖報廢的產品都有不同的密鑰。每種產品只有少數是相同的。 –

回答

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
<?php 
    $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); 
?> 

測試結果

[[email protected] 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); 

?> 

輸出

[[email protected] 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

希望我簡單的職位也將有所幫助。這裏我們使用array_map,array_keys,array_merge_recursive,array_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); 
相關問題