组合两个数组并保持重复

时间:2013-10-24 10:04:54

标签: php arrays key-value

大家好,

我有一个带键的数组:

$keys
: array = 
  0: string = Author
  1: string = Description
  2: string = Title
  3: string = Description

另一个值为:

的数组
$values
: array = 
  0: string = Margaret Atwood
  1: string = A wonderful Canadian writer
  2: string = The handmaids tale
  3: string = One of the most wonderful books of the year 

为了打印:

Author: Margaret Atwood 

Description: A wonderful Canadian writer

Title: The handmaids tale

Description: One of the most wonderful books of the year

我做:

$ary= array_combine($keys, $values);

但这会打印出来:

Author: Margaret Atwood 
Description: One of the most wonderful books of the year
Title: The handmaids tale

我该怎么做才能获得所需的印刷效果?

我担心我无法更改密钥数组中的重复说明:(

非常感谢!!!

4 个答案:

答案 0 :(得分:1)

重命名你的密钥,以摆脱重复:

$keys
: array = 
  0: string = Author
  1: string = AuthorDescription
  2: string = Title
  3: string = TitleDescription

$values
: array = 
  0: string = Margaret Atwood
  1: string = A wonderful Canadian writer
  2: string = The handmaids tale
  3: string = One of the most wonderful books of the year 

在这种情况下,$ary= array_combine($keys, $values);将保留所有信息,因为现在没有任何重复的密钥。

答案 1 :(得分:0)

使用foreach循环迭代$keys数组的索引和元素。在每个迭代步骤中,打印元素并使用索引访问$values数组中的相应值:

foreach ($keys as $index => $element) {
    printf("%s: %s\n", $element, $values[$index]);
}

或者,您可以存储密钥和值以供以后使用:

$combined = array();
foreach ($keys as $index => $element) {
    $combined[$index] = array('key' => $element, 'value' => $values[$index]);
}

答案 2 :(得分:0)

为什么我们不能复制?是的,我们不能复制数组键,但我们可以制作2个暗淡的数组。例如:

<?php
$keys = array( 
    'Author',
    'Description',
    'Title',
    'Description'
);

$values = array(
    'Margaret Atwood',
    'A wonderful Canadian writer',
    'The handmaids tale',
    'One of the most wonderful books of the year'
);

$combinedArray = array();

foreach ($keys as $index=>$key){
    if( isset($combinedArray[$key]) ){
        if(!is_array($combinedArray[$key])){
            $combinedArray[$key] = array($combinedArray[$key]);
        }
        array_push($combinedArray[$key],$values[ $index ]);
    }else{
        $combinedArray[$key] = $values[ $index ];
    }
}


var_dump( $combinedArray );

输出:

array(3) {
  ["Author"]=>
  string(15) "Margaret Atwood"
  ["Description"]=>
  array(2) {
    [0]=>
    string(27) "A wonderful Canadian writer"
    [1]=>
    string(43) "One of the most wonderful books of the year"
  }
  ["Title"]=>
  string(18) "The handmaids tale"
}

实例here

答案 3 :(得分:0)

一般情况下,如果使用二维数组会更好。 但是,如果你已经拥有2个阵列并且只想破解它,那就去寻找像这个函数更难看的东西:

function combineStringArrayWithDuplicates ($keys, $values) {
  $iter = 0; 
  foreach ($keys as $key) 
      { $combined[$iter] = $key .": ". $values[$iter]; $iter++;}
return $combined;
}

..你可以这样使用:

$keys = array("Author","Description", "Title", "Description");
$values = array ("Margaret Atw"," wonderful Canadian writer","The handmaids tale", "One of the most wonderful books of the year"); 

$combined = combineStringArrayWithDuplicates($keys, $values);

的print_r($组合);