9

I have a problem here in array_combine()

Warning: array_combine(): Both parameters should have an equal number of elements in PATH on line X

This error gets display on the following line:

foreach(array_combine($images, $word) as $imgs => $w)
{
    //do something
}

How can I fix it?

4
  • 2
    Have you tried putting the same number of elements in both arrays? Commented Oct 16, 2013 at 3:59
  • 1
    the number of elements for each array should be equal for array_combine() to work Commented Oct 16, 2013 at 4:00
  • what I want to do is, for example the user mistakenly added a null value, how can I avoid having this error? Commented Oct 16, 2013 at 5:44
  • I mean error handling for array_combine() when the elements are not equal Commented Oct 16, 2013 at 5:52

2 Answers 2

26

This error appears when you try to combine two arrays with unequal length. As an example:

Array 1: Array (A, B, C)     //3 elements
Array 2: Array (1, 2, 3, 4)  //4 elements

array_combine() can't combine those two arrays and will throw a warning.


There are different ways to approach this error.

You can check if both arrays have the same amount of elements and only combine them if they do:

<?php

    $arrayOne = Array("A", "B", "C");
    $arrayTwo = Array(1, 2, 3);

    if(count($arrayOne) == count($arrayTwo)){
        $result = array_combine($arrayOne, $arrayTwo);
    } else{
        echo "The arrays have unequal length";
    }

?>

You can combine the two arrays and only use as many elements as the smaller one has:

<?php

    $arrayOne = Array("A", "B", "C");
    $arrayTwo = Array(1, 2, 3);

    $min = min(count($arrayOne), count($arrayTwo));
    $result = array_combine(array_slice($arrayOne, 0, $min), array_slice($arrayTwo, 0, $min));

?>

Or you can also just fill the missing elements up:

<?php

    $arrayOne = Array("A", "B", "C");
    $arrayTwo = Array(1, 2, 3);

    $result = [];
    $counter = 0;

    array_map(function($v1, $v2)use(&$result, &$counter){
        $result[!is_null($v1) ? $v1 : "filler" . $counter++] = !is_null($v2) ? $v2 : "filler";     
    }, $arrayOne, $arrayTwo);

?>

Note: That in all examples you always want to make sure the keys array has only unique elements! Because otherwise PHP will just overwrite the elements with the same key and you will only keep the last one.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please provide a sample?
Your solution proved to be the one which worked for me - array_map did the job!
-2

You can use array_merge instead of array_combine

Example:

array_merge($myFirstArray, $mySecondArray);

1 Comment

You do understand what array_combine does, and how it's different from array_merge? php.net/array_combine

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.