2015-09-04 55 views
1

我怎樣才能把這種陣列相結合:如何三個陣列與[0]作爲鑰匙插入一個

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); 

$味精顯示我:

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

我們e array_merge –

+0

[array_merge()](http://www.php.net/manual/en/function.array-merge.php)也許....嘗試看看PHP文檔....他們可以是非常有幫助 –

+0

@SunilPachlangia它不起作用。 –

回答

0

使用array_merge()

$result = array_merge($arr1, $arr2, $arr3); 
print_r($result); 
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); 
+0

它似乎工作,但是,如果我print_r以外的foreach它不。是什麼原因? –

+0

您是否定義了數組$ percentage_sm = array();在foreach循環之前 –

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']; } 
    ) 
);