0

大家好,

我有一个带键的数组:

$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

4 回答 4

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);将保留所有信息,因为现在没有任何重复的键。

于 2013-10-24T10:08:33.050 回答
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]);
}
于 2013-10-24T10:10:04.913 回答
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"
}

现场示例在这里

于 2013-10-24T10:25:09.110 回答
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($combined);

于 2013-10-24T11:00:31.360 回答