正在合并具有特定输出格式的数组


Merging arrays with specific output format

我目前正在学习如何管理和使用数组。但我对组合两个数组有点不知所措。两个数组都有相同数量的值。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"
  }

我会在它们上循环:

foreach ($thumbnails as $k => $src) {
   if (isset($urls[$k]) {
      $urls[$k]['thumbnail'] = $src;
   }
}
函数array_merge在这里对您没有帮助,因为它是"数组中的数组"。但是您可以在它们上循环,所以您只有一个数组项。下面的函数返回一个您想要的数组:
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
        )
)
$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)));

你是这个意思吗?