tags:

views:

56

answers:

2

i m using array_combine() but it display error when in first array there is no value. how to get rid of this

EDIT:

First array  
Distance_Array
Array
(
    [0] => 
)

School_ID_Array
Array
(
    [0] => 
)
 and i m using
$Coverage_ Array=array_combine($School_ID_Array,$Distance_Array);   

which results

Coverage_ Array
Array
(
    [] => 
)

i want that in first array, if any value is empty , 
then  Coverage_ Array key accept any default key
A: 

array_combine()

Errors/Exceptions

Throws E_WARNING if keys and values are either empty or the number of elements does not match.

So you need to ensure there is something in the array or don't call array_combine(). For example:

if (count($keys) > 0 && count($values) > 0 && count($keys) == count($values)) {
  $combined = array_combine($keys, $values);
}
cletus
+3  A: 

Use conditions like this:

if (isset($some_var_or_array) && !empty($some_var_or_array)) {
    // some code which using $some_var_or_array value(s)
}

UDATED

Here the function ArrayCombine(), which get three parameters: two arrays and third - the default parameter. The default parameter value will be set to the empty or nulled first array values:

function ArrayCombine($array1, $array2, $default = 0)
{
    foreach ($array1 as $key => $value) {
        if (!isset($value) || empty($value)) {
            $array1[$key] = $default;
        }
    }
    return array_combine($array1, $array2);
}

Here the example:

$Distance_Array = array(
    1 => '',
);

$School_ID_Array = array(
    3 => 4,
);

$Coverage_Array = ArrayCombine($Distance_Array, $School_ID_Array);

var_dump($Coverage_Array);

/*
    var_dump output:
    array(1) {
        [24]=>
            int(4)
    }
*/
Sergey Kuznetsov
i used `var_dump(isset($storeArray)); // bool(true)` `var_dump(empty($storeArray)); //bool(false)`i edited quetion, please read that and reply
diEcho
@I Like PHP: Check the updated code
Sergey Kuznetsov
Thank You. It works
diEcho