2014-10-06 155 views
-2

大家好我想使從陣列2維新1個陣列尺寸和這樣的代碼:PHP數組二維數組轉換成一維

[0] => Array(
    [photo_data_id] => 0 
    [photo_image] => Array 
     (
      [0] => 37a.jpg 
     ) 

    [photo_status] => 1 
) 

[1] => Array(
    [photo_data_id] => 1 
    [photo_image] => Array 
     (
      [0] => 6.jpg 
      [1] => 6a.jpg 
      [2] => 20141001_073231.jpg 
     ) 

    [photo_status] => 1 
) 
[2] => Array(
     [photo_data_id] => 2 
     [photo_image] => Array 
      (
       [0] => 20.jpg 
       [1] => 21.jpg 
       [2] => 22.jpg 
       [3] => 23.jpg 
       [4] => 24.png 
      ) 

     [photo_status] => 1 
) 

,我希望像下面,比專賣店到數據庫中的結果:

[0] => Array(
     [photo_data_id] => 0 
     [photo_image] => 37a.jpg 
     [photo_status] => 1 
) 

[1] => Array(
     [photo_data_id] => 1 
     [photo_image] => 6.jpg 
     [photo_status] => 1 
) 
[2] => Array(
     [photo_data_id] => 1 
     [photo_image] => 6a.jpg 
     [photo_status] => 1 
) 
[3] => Array(
     [photo_data_id] => 1 
     [photo_image] => 20141001_073231.jpg 
     [photo_status] => 1 
) 

[4] => Array(
     [photo_data_id] => 2 
     [photo_image] => 20.jpg 
     [photo_status] => 1 
) 

[5] => Array(
     [photo_data_id] => 2 
     [photo_image] => 21.jpg 
     [photo_status] => 1 
) 
[6] => Array(
     [photo_data_id] => 2 
     [photo_image] => 22.jpg 
     [photo_status] => 1 
) 
[7] => Array(
     [photo_data_id] => 2 
     [photo_image] => 23.jpg 
     [photo_status] => 1 
) 
[8] => Array(
     [photo_data_id] => 2 
     [photo_image] => 24.jpg 
     [photo_status] => 1 
) 

請幫幫我,我是新手,謝謝的前:)

回答

1
$result = array(); 

foreach ($array1 as $item) { 
    foreach ($item['photo_image'] as $image) { 
     $result[] = array(
      'photo_data_id' => $item['photo_data_id'], 
      'photo_image' => $image, 
      'photo_status' => $item['photo_status'],  
     ); 
    } 
} 

print_r($result); 
+0

驚人,完美地工作(Y) – user3562918 2014-10-06 11:10:55