2014-12-04 69 views
0

我試着解釋一下:PHP - 如何從其他2個數組構建數組?

我有2個陣列,其中之一是:

Array(
[0] => 01/09/2014 
[1] => 02/09/2014 
[2] => 03/09/2014 
[3] => 04/09/2014 
[4] => 05/09/2014 
[5] => 06/09/2014 
[6] => 07/09/2014 
[7] => 08/09/2014 
[8] => 09/09/2014 
[9] => 10/09/2014 
[10] => 11/09/2014 
[11] => 12/09/2014 
[12] => 13/09/2014 
[13] => 14/09/2014 
[14] => 15/09/2014 
[15] => 16/09/2014 
[16] => 17/09/2014 
[17] => 18/09/2014 
[18] => 19/09/2014 
[19] => 20/09/2014 
[20] => 21/09/2014 
[21] => 22/09/2014 
[22] => 23/09/2014 
[23] => 24/09/2014 
[24] => 25/09/2014 
[25] => 26/09/2014 
[26] => 27/09/2014 
[27] => 28/09/2014 
[28] => 29/09/2014 
[29] => 30/09/2014 
) 

,另一個是:

Array(
[12/09/2014] => Array 
    (
     [0] => 17:41:54 
     [1] => 17:42:21 
     [2] => 17:42:47 
     [3] => 17:43:21 
     [4] => 17:47:24 
     [5] => 17:47:40 
     [6] => 17:48:09 
     [7] => 17:48:27 
     [8] => 18:09:21 
     [9] => 18:55:22 
     [10] => 20:22:02 
     [11] => 23:36:22 
     [12] => 23:42:39 
     [13] => 17:59:25 
    ) 

[15/09/2014] => Array 
    (
     [0] => 13:02:29 
    ) 

[23/09/2014] => Array 
    (
     [0] => 9:55:56 
     [1] => 10:25:01 
    ) 

[26/09/2014] => Array 
    (
     [0] => 9:09:24 
     [1] => 9:15:30 
     [2] => 10:39:49 
     [3] => 19:53:40 
    ) 

[29/09/2014] => Array 
    (
     [0] => 8:28:46 
    ) 

[30/09/2014] => Array 
    (
     [0] => 8:16:54 
     [1] => 10:06:02 
     [2] => 10:11:32 
     [3] => 14:16:07 
     [4] => 16:25:02 
    ) 

[01/10/2014] => Array 
    (
     [0] => 8:28:23 
     [1] => 9:12:22 
     [2] => 9:15:38 
     [3] => 10:25:23 
    ) 

我需要,如果當建立其他第二個數組索引與一些第一個數組元素相匹配,放置子元素(次),如果沒有,則放置「沒有結果」。

明白嗎?

例子:

Array 
(
[01/09/2014] => Array 
    (
     [0] => "There aren't results" 

    ) 

[02/09/2014] => Array 
    (
     [0] => "There aren't results" 
    ) 

[03/09/2014] => Array 
    (
     [0] => "There aren't results" 
    ) 

. 
. 
. 
[12/09/2014] => Array 
    (
     [0] => 17:41:54 
     [1] => 17:42:21 
     [2] => 17:42:47 
     [3] => 17:43:21 
     [4] => 17:47:24 
     [5] => 17:47:40 
     [6] => 17:48:09 
     [7] => 17:48:27 
     [8] => 18:09:21 
     [9] => 18:55:22 
     [10] => 20:22:02 
     [11] => 23:36:22 
     [12] => 23:42:39 
     [13] => 17:59:25 
    ) 

[13/09/2014] => Array 
    (
      [0] => "There aren't results" 
    ) 

回答

1

一個可能的答案可能是以下幾點:

<?php 

$dates = array(); 
$information = array(); 
$result = array(); 

foreach($dates as $date) { 
    if(array_key_exists($date, $information)) { 
     $result[$date] = $information[$date]; 
    } else { 
     $result[$date] = array('There aren\'t any results'); 
    } 
} 

Ofcourse,你必須改變$dates你的第一個數組和$information到您的第二個陣列...

+0

這只是什麼我想......謝謝......這很容易,我看不到它哈哈。 – 2014-12-04 11:10:39

+0

也請看@hsz提供的解決方案,雖然它對於初學者來說可讀性較差,但它是針對您的問題的更優雅的解決方案=) – RichardBernards 2014-12-04 11:13:27

+0

是的,我做到了。但我認爲你的解決方案對我來說已經足夠了:) – 2014-12-04 11:35:15

1

試試這個代碼

$first_arr = array(); 
$second_arr = array(); 
$new_array = array(); 

foreach($first_arr as $key => $value){ 
    if(isset($second_arr[$value])){ 
    $new_array[$key] = $second_arr[$value]; 
    }else{ 
    $new_array[$key] = array("There aren't results"); 
    } 
} 
1
$newA=array(); 
foreach ($firstA as $fa) 
{ 
    if(array_key_exists($fa,$secoundA)) 
    { 

     $newA[$fa]=$secoundA[$fa]; 
    } 
    else 
    { 
     $newA[$fa]="There aren't results"; 
    } 

} 
print_r($newA); 
2

array_fill_keys剛剛嘗試用默認值和數據陣列進行合併:

$dates = array(
    '01/09/2014', 
    '02/09/2014', 
    '03/09/2014', 
    '04/09/2014', 
); 

$times = array(
    '02/09/2014' => array(
     '14:41:54', 
     '17:41:54', 
    ), 
    '04/09/2014' => array(
     '11:41:54', 
    ), 
); 

$output = $times + array_fill_keys($dates, array("There aren't results")); 
ksort($output); 

var_dump($output); 

輸出:

array (size=4) 
    '01/09/2014' => 
    array (size=1) 
     0 => string 'There aren't results' (length=20) 
    '02/09/2014' => 
    array (size=2) 
     0 => string '14:41:54' (length=8) 
     1 => string '17:41:54' (length=8) 
    '03/09/2014' => 
    array (size=1) 
     0 => string 'There aren't results' (length=20) 
    '04/09/2014' => 
    array (size=1) 
     0 => string '11:41:54' (length=8) 
+0

不錯而優雅的解決方案! – RichardBernards 2014-12-04 11:01:01

+0

我喜歡RichardBernards的解決方案,現在我很難理解這個解決方案,但他說這比他的解決方案好,所以我認爲說實話就是了。感謝所有的答案,它的答案很快就令人驚歎。我留下了深刻的印象 – 2014-12-04 11:32:27

1

您可以使用array_map

$combined = array_map(function($value) use ($secondArray) { 
    if (array_key_exists($value, $secondArray)) { 
     return array(
      $value => $secondArray[$value], 
     ); 
    } else { 
     return array(
      $value => "There aren't results", 
     ); 
    } 
}, $firstArray);