0

I have this array:

Array ( [0] => Post Slider [1] => Post Slider Wide [2] => Post Slider ) 

And this second array:

Array ( [0] => Tools Sliders [1] => Tools Sliders [2] => modules-test ) 

When I use the PHP function array_combine, it does not include the duplicates, like this:

Array ( [Post Slider] => modules-test [Post Slider Wide] => Tool Sliders ) 

I'm confused how to get a desired result like this (without stripping the duplicates, complete one to one relationship):

Array ( [Post Slider] => Tools Sliders [Post Slider Wide] => Tools Sliders [Post Slider] => modules-test) 

I would appreciate any help and tips..

Regards, Codex

2
  • If you could do that, what would you expect echo $array["Post Slider"] to output? Commented Apr 19, 2013 at 12:53
  • Yeah agree that itself is a violation of the array but this comes in a rare circumstances from a data source somewhere that I cannot control. I only need to combine them the way I want and that's it.. Commented Apr 19, 2013 at 13:02

2 Answers 2

1

You will never have duplicated keys in your output array no matter what you will do. Keys are always unique.

The only solution is to assign to the key an array with, for example, two values.

$keys   = array ( 'Post Slider', 'Post Slider Wide', 'Post Slider' );
$values = array ( 'Tools Sliders', 'Tools Sliders', 'modules-test' );
$output = array();

$size = sizeof($keys);
for ( $i = 0; $i < $size; $i++ ) {
    if ( !isset($output[$keys[$i]]) ) {
        $output[$keys[$i]] = array();
    }
    $output[$keys[$i]][] = $values[$i];
}
Sign up to request clarification or add additional context in comments.

Comments

0
$count1 = count($array1);
$count2 = count($array2);
$array = array();

if($count1==$count2){
    foreach($array1 as $i=>$val){
        $array[]=array($val,$array2[$i]);
    }
}

You will get:

Array (
 [0] => Array(
         [0] => Post Slider
         [1] => Tools Sliders
       )
 ............
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.