簡體   English   中英

合並兩個數組並保留重復項

[英]Combining two arrays and keeping duplicates

大家好,

我有一個鍵數組:

$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

我該怎么做才能獲得所需的打印效果???

恐怕我不能更改鍵數組中的重復描述:(

非常感謝!!!

重命名密鑰,以消除重復:

$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); 將保留所有信息,因為現在沒有任何重復的密鑰。

使用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個昏暗的數組。 例:

<?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"
}

這里的例子

通常,如果使用二維數組會更好。 但是,如果您已經擁有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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM