如何将三个以[0]为键的数组合并为一个

时间:2015-09-04 07:08:04

标签: php arrays

我该怎么转这个数组:

Array ( [0] => 80 ) Array ( [0] => 20 ) Array ( [0] => 90 )

进入这样的数组:

Array (
[0] => 80,
[1] => 20,
[2] => 90
);

代码:

$percentage_result = $percentage_query->result_array(); //output below:

输出:

    Array
(
    [0] => Array
        (
            [id] => 62
            [list_id] => 55
            [start_date] => 1459987200
            [end_date] => 1459987200
            [percentage] => 80
        )

    [1] => Array
        (
            [id] => 64
            [list_id] => 55
            [start_date] => 1459814400
            [end_date] => 1459814400
            [percentage] => 20
        )

    [2] => Array
        (
            [id] => 63
            [list_id] => 55
            [start_date] => 1459900800
            [end_date] => 1459900800
            [percentage] => 90
        )

我想保存所有[百分比]并获得最高的一个。

这样做:

                $null = array();
                foreach ($percentage_result as $ptime) {            

                //Days between start date and end date -> seasonal price
                $start_time = $ptime['start_date'];
                $end_time = $ptime['end_date'];

                $percentage_sm = explode(',', $ptime['percentage']);


                $mrg = array_merge($null, $percentage_sm);

                print_r($mrg);

$ msg告诉我:

    Array
(
    [0] => 80
)
Array
(
    [0] => 20
)
Array
(
    [0] => 90
)

3 个答案:

答案 0 :(得分:2)

你可以用非常简单的方式做到这一点

$percentage_sm = array(); //define blank array
foreach ($percentage_result as $ptime) {            

    //Days between start date and end date -> seasonal price
    $start_time = $ptime['start_date'];
    $end_time = $ptime['end_date'];

    $percentage_sm[] = $ptime['percentage'];  //assign every value to array
}

print_r($percentage_sm);

答案 1 :(得分:0)

使用array_merge()

$result = array_merge($arr1, $arr2, $arr3);
print_r($result);

答案 2 :(得分:0)

如果你想从$ percentage_result数组中获得最高百分比值,那么最简单的方法就是

$maxPercentage = max(array_column($percentage_result, 'percentage'));

而不是尝试使用array_merge

做一些奇怪的事情

(PHP> = 5.5.0)

如果您正在运行较低版本的PHP,那么您可以使用

执行类似操作
$maxPercentage = max(
    array_map(
        $percentage_result, 
            function ($value) { return $value['percentage']; }
    )
);