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

时间:2015-02-07 01:24:29

标签: php

我目前正在学习如何管理和使用数组。但是我在组合两个阵列时有点失落。两个数组都具有相同数量的值。 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"
  }

3 个答案:

答案 0 :(得分:1)

我会绕过它们:

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

答案 1 :(得分:0)

函数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
        )

)

答案 2 :(得分: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)));
你是说这个吗?