Miguel Mas
Miguel Mas

Reputation: 545

Combining two arrays and keeping duplicates

Hi all,

I have an array with keys:

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

And another array with values:

$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 

In order to print:

Author: Margaret Atwood 

Description: A wonderful Canadian writer

Title: The handmaids tale

Description: One of the most wonderful books of the year

I do:

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

But this prints:

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

What can I do to get the desired printing???

I'm afraid I cannot change the duplicate Description in keys array :(

Thanks a lot!!!

Upvotes: 0

Views: 1574

Answers (4)

user1885574
user1885574

Reputation: 11

In general it would be better if you use a two dimensional array. But if you already have the 2 arrays and just want to hack it just go for something more ugly like this function:

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

.. which you can then use like this:

$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);

Upvotes: 0

dododo
dododo

Reputation: 266

Why not we cann't duplicate? Yes, we can not duplicate array key, but we can make 2 dim array. Example:

<?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 );

Output:

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

Live example here

Upvotes: 0

Oswald
Oswald

Reputation: 31647

Use a foreach loop to iteratare over the indices and elements of the $keys array. In each iteration step, print the element and use the index to acces the respective value in the $values array:

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

Alternatively, you can store the key and value for later use:

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

Upvotes: 0

Ashalynd
Ashalynd

Reputation: 12563

Rename your keys, to get rid of the duplication:

$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 

In this case, $ary= array_combine($keys, $values); will keep all the information because now there aren't any duplicated keys.

Upvotes: 1

Related Questions