2011-11-08 32 views
-1

可能重複:
Changing a nested (multidimentional) array into key => value pairs in PHP我要如何改變一個PHP數組中的鍵值

我有這個陣列

[5] => Array 
     (
      [completed_system_products_id] => 1 
      [completed_systems_id] => 76 
      [step_number] => 1 
      [product_id] => 92 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:01 
     ) 

    [4] => Array 
     (
      [completed_system_products_id] => 2 
      [completed_systems_id] => 76 
      [step_number] => 2 
      [product_id] => 62 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:51 
     ) 

    [3] => Array 
     (
      [completed_system_products_id] => 3 
      [completed_systems_id] => 76 
      [step_number] => 3 
      [product_id] => 104 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:56 
     ) 

    [2] => Array 
     (
      [completed_system_products_id] => 4 
      [completed_systems_id] => 76 
      [step_number] => 4 
      [product_id] => 251 
      [category] => hardware 
      [date_added] => 2011-05-03 13:48:56 
     ) 

如何使該鍵的值與[step_number] =>

因此,例如,我想這個結果

[1] => Array 
     (
      [completed_system_products_id] => 1 
      [completed_systems_id] => 76 
      [step_number] => 1 
      [product_id] => 92 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:01 
     ) 

    [2] => Array 
     (
      [completed_system_products_id] => 2 
      [completed_systems_id] => 76 
      [step_number] => 2 
      [product_id] => 62 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:51 
     ) 

    [3] => Array 
     (
      [completed_system_products_id] => 3 
      [completed_systems_id] => 76 
      [step_number] => 3 
      [product_id] => 104 
      [category] => hardware 
      [date_added] => 2011-05-03 13:44:56 
     ) 

    [4] => Array 
     (
      [completed_system_products_id] => 4 
      [completed_systems_id] => 76 
      [step_number] => 4 
      [product_id] => 251 
      [category] => hardware 
      [date_added] => 2011-05-03 13:48:56 
     ) 

回答

1
$new = array(); 
foreach ($old as $value) { 
    $new[$value['step_number']] = $value; 
} 
$old = $new; 
0
$result = array(); // Create a new array to hold the result 
foreach ($array as $val) { // Loop the original array 
    $result[$val['step_number']] = $val; // Add the value to the new array with the correct key 
} 
ksort($result); // Sort the array by key 
print_r($result); // Display the result 
0
$results = array(); 
foreach ($array as $item) 
{ 
    $results[$item['step_number']] = $item; 
} 
相關問題