2015-02-07 55 views
2

我目前正在學習如何管理和使用數組。但是我將兩個數組組合在一起時有點失落。兩個數組中都有相同數量的值。 array_merge($thumbnails, $urls);做它說什麼,但它不是我正在尋找。我如何合併如下所示的數組?合併具有特定輸出格式的陣列

$thumbnails = array( 
      array("thumbnail" => "https://example1.png") , 
      array("thumbnail" => "https://example2.png") , 
      array("thumbnail" => "https://example3.png") ,    
); 

    $urls = array( 
      array("url" => "http://www.example.com/1") , 
      array("url" => "http://www.example.com/2") , 
      array("url" => "http://www.example.com/3") ,   
); 

當前結果

[0]=> 
    array(1) { 
    ["thumbnail"]=> "https://example1.png" 
    } 
    [1]=> 
    array(1) { 
    ["thumbnail"]=> "https://example2.png" 
    } 
    [2]=> 
    array(1) { 
    ["thumbnail"]=> "https://example3.png" 
    } 
    [3]=> 
    array(1) { 
    ["url"]=> "http://www.example.com/1" 
    } 
    [4]=> 
    array(1) { 
    ["url"]=> "http://www.example.com/2" 
    } 
    [5]=> 
    array(1) { 
    ["url"]=> "http://www.example.com/3" 
    } 

所需的結果

[0]=> 
    array(2) { 
    ["thumbnail"]=> "https://example1.png" 
    ["url"]=> "http://www.example.com/1" 
    } 
    [1]=> 
    array(2) { 
    ["thumbnail"]=> "https://example2.png" 
    ["url"]=> "http://www.example.com/2" 
    } 
    [2]=> 
    array(2) { 
    ["thumbnail"]=> "https://example2.png" 
    ["url"]=> "http://www.example.com/2" 
    } 
    [3]=> 
    array(2) { 
    ["thumbnail"]=> "https://example3.png" 
    ["url"]=> "http://www.example.com/3" 
    } 

回答

1

我只想對他們的循環:

foreach ($thumbnails as $k => $src) { 
    if (isset($urls[$k]) { 
     $urls[$k]['thumbnail'] = $src; 
    } 
} 
+0

簡單而有效! – MaryCoding 2015-02-07 02:00:09

0

功能array_merge不會幫助你在這裏,因爲它是「Array in Array」 。但是你可以遍歷它們,所以你只有一個數組項。下面的函數返回一個數組,就像你的願望:

function array_merge_keys($a, $b) { 
    $retArr = array(); 
    // Lets use min(countA, countB) so there will be no errors on 
    // unbalanced array item counts. 
    for($i = 0; $i < min(count($a), count($b)); $i++) { 
     $retArr[$i] = array_merge($a[$i], $b[$i]); 
    } // for end 
    return $retArr; 
} 

$a = array(array("url" => "1.jpg"), array("url" => "2.jpg")); 
$b = array(array("thumb" => "1.thumb.jpg"), array("thumb" => "2.thumb.jpg")); 

print_r(array_merge_keys($a, $b)); 

這給了你:

Array 
(
    [0] => Array 
     (
      [url] => 1.jpg 
      [thumb] => 1.thumb.jpg 
     ) 

    [1] => Array 
     (
      [url] => 2.jpg 
      [thumb] => 2.thumb.jpg 
     ) 

) 
0
$thumbnails = array(
      array("thumbnail" => "https://example1.png") , 
      array("thumbnail" => "https://example2.png") , 
      array("thumbnail" => "https://example3.png") , 
); 

    $urls = array(
      array("url" => "http://www.example.com/1") , 
      array("url" => "http://www.example.com/2") , 
      array("url" => "http://www.example.com/3") , 
); 
$thumbnails = array_combine(array_map(function($key){return 'key'.$key;},array_keys($thumbnails)),$thumbnails); 
$urls = array_combine(array_map(function($key){return 'key'.$key;},array_keys($urls)),$urls); 
var_dump(array_values(array_merge_recursive($thumbnails,$urls))); 

你的意思呢?