PHP - 如何从其他2个数组构建数组?

时间:2014-12-04 10:53:19

标签: php arrays

我试着解释一下自己:

我有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"
    )

5 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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");
  }
}

答案 2 :(得分: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');
    }
}

当然,您必须将$dates更改为第一个数组,将$information更改为第二个数组......

答案 3 :(得分: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);

答案 4 :(得分: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);