使两个索引数组成为一个索引数组PHP

时间:2019-02-13 06:22:32

标签: php arrays

我只想使两个索引数组成为一个数组。

这是我的代码:

$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
  $intdate[] =intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir);
  $intdate[$arr++];
}

结果类似:

array(2) { 
    [0]=> array(4) { 
            [0]=> string(10) "2018-11-12"
            [1]=> string(10) "2018-11-13"
            [2]=> string(10) "2018-11-14"
            [3]=> string(10) "2018-11-15" 
        } 
    [1]=> array(2) { 
            [0]=> string(10) "2018-10-31" 
            [1]=> string(10) "2018-11-01" 
        } 
}

我希望像:

array(5){ 
    [0]=> string(10) "2018-11-12"
    [1]=> string(10) "2018-11-13"
    [2]=> string(10) "2018-11-14"
    [3]=> string(10) "2018-11-15" 
    [4]=> string(10) "2018-10-31" 
    [5]=> string(10) "2018-11-01" 
}

谢谢.. !!

4 个答案:

答案 0 :(得分:1)

如果intervalDate函数的返回值是一个数组,则可以执行此操作

$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
    foreach (intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir) as $date) {
        $intdate[] = $date;
    }
}

答案 1 :(得分:0)

我认为您希望将两个数组合并为一个数组。如果是这样,那么array_reduce可能会为您提供帮助。

$arr = [
    [
        "2018-11-12",
        "2018-11-13",
        "2018-11-14",
        "2018-11-15" ,
    ],
    [
        "2018-10-31",
        "2018-11-01",
    ]
];

$intdate = array_reduce($arr, function($old, $new) {
    return array_merge($old, $new);
}, []);

echo '<pre>', print_r($intdate), '</pre>';

答案 2 :(得分:0)

您可以使用此功能

function flatten_array( array $array, array $flattened = array() ) {
    foreach ( $array as $item ) {
        if ( is_array($item) ) {
            $flattened = flatten_array( $item, $flattened );
            continue;
        }

        $flattened[] = $item;
    }

    return $flattened;
}

$arr = array(
    array(
        "2018-11-12",
        "2018-11-13",
        "2018-11-14",
        "2018-11-15" ,
    ),
    array(
        "2018-10-31",
        "2018-11-01",
    ),
);

flatten_array($arr);

答案 3 :(得分:0)

    $arr1 = array(1,2,3,4,5,11);
    $arr2 = array(6,7,8,9,10,11);
    echo'<pre>';print_r(array_merge($arr1,$arr2));
    echo'<pre>';print_r(array_unique(array_merge($arr1,$arr2)));die;

各自的输出:

array_merge :
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 11
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
)
array_unique + array_merge :
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 11
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)