2017-02-15 85 views
1

我在一次提交陣列,請參閱下面的詳細信息:PHP數組操作需要提示

array(5) { 
    ["ambition_id"]=> 
     array(2) { 
     [55]=> string(2) "55" 
     [60]=> string(2) "60" 
     } 
     ["target"]=> 
     array(1) { 
     [0]=> string(8) "target 1" 
     [1]=> string(8) "target 2" 
     } 
     ["strides"]=> 
     array(1) { 
     [0]=> string(1) "1" 
     [1]=> string(1) "1" 
     } 
     ["date"]=> 
     array(1) { 
     [0]=> string(10) "2017-02-08" 
     [1]=> string(10) "2017-03-08" 
     } 
     ["frequency"]=> 
     array(1) { 
     [0]=> string(1) "1" 
     [1]=> string(1) "2" 
     } 
} 

其實,我在mysql中,「野心」和「目標」兩個表。野心是一組目標('ambition_id'是'目標'表中的外鍵)。該數組將存儲在'目標'表中。這就是爲什麼有一個「ambition_id」

我試過很多次,但失敗了(使用的foreach),現在我需要有人誰可以給我一個幫助。

蠻力,這很容易!我已經解決了它,但我需要「更高級」的數組操作。

我怎麼能想出這個?

array(2) { 
    [0] => array('ambition_id' => 55, 
       'target' => 'target 1', 
       'strides' => 1, 
       'date' => '2017-02-08', 
       'frequency' => 1 
       ), 
    [1] => array('ambition_id' => 60, 
       'target' => 'target 2', 
       'strides' => 2, 
       'date' => '2017-03-08', 
       'frequency' => 2) 
} 

請幫忙,非常感謝!

+1

$ final_array =陣列(array_column($陣列,0),array_column($陣列,1)); –

+1

@Anant第一個子數組沒有'0','1'索引。需要先將其轉換。 –

+0

@SougataBose是 –

回答

0

你必須轉動你的數據:

$data = array (
    "ambition_id" => 
     array (
     55 => "55", 
     60 => "60" 
    ), 
    "target" => 
    array (
     0 => "target 1", 
     1 => "target 2" 
    ), 
    "strides" => 
    array (
     0 => "1", 
     1 => "1" 
    ), 
    "date" => 
    array (
     0 => "2017-02-08", 
     1 => "2017-03-08" 
    ), 
    "frequency" => 
    array (
     0 => "1", 
     1 => "2" 
    ) 
); 

// pivot data 
$pivot = array(); 
foreach ($data as $datum => $values) { 
    $value_index = 0; 
    foreach ($values as $value) { 
     $pivot[$value_index][$datum] = $value; 
     $value_index++; 
    } 
} 

print_r($pivot); 

這是假設你只有兩個數據水平,該數據表現良好。

0

沒有最佳答案,但它可以解決你的問題

<?php 
$array = [ 
    "ambition_id" => 
    [ 
     55 => "55", 
     60 => "60" 
    ], 
    "target" => 
    [ 
     0 => "target 1", 
     1 => "target 2" 
    ], 
    "strides" => 
    [ 
     0 => "1", 
     1 => "1" 
    ], 
    "date" => 
    [ 
     0 => "2017-02-08", 
     1 => "2017-03-08" 
    ], 
    "frequency" => 
    [ 
     0 => "1", 
     1 => "2" 
    ], 
]; 
$result = array(); 
foreach ($array as $k => $v) { 
    foreach ($v as $kk => $vv) { 
     if ($k == "ambition_id") { 
      $result[] = array($k => $vv); 
     } else { 
      $result[$kk][$k] = $vv; 
     } 
    } 
} 

下面是測試https://3v4l.org/UdHH8

0

只需使用循環數組和用戶array_values重新指數環內陣列和存儲它進入如下所示的新陣列。

<?php 

    $new_array =array(); 

    foreach($array as $key1=>$row1) 
    { 
     $ss =array_values($row1); 
     foreach($ss as $key2=>$row2) 
     { 

      $new_array[$key2][$key1]=$row2; 

     } 

    } 

    echo "<pre>"; 
    print_r($new_array); 

    ?> 

輸出:

Array 
    (
     [0] => Array 
      (
       [ambition_id] => 55 
       [target] => target 1 
       [strides] => 1 
       [date] => 2017-02-08 
       [frequency] => 1 
      ) 

     [1] => Array 
      (
       [ambition_id] => 60 
       [target] => target 2 
       [strides] => 1 
       [date] => 2017-03-08 
       [frequency] => 2 
      ) 

    )