开发者

array_combine stuck when there is no value in first array

开发者 https://www.devze.com 2022-12-18 15:37 出处:网络
i m using array_combine() but it display error when in first array there is no value. how to get rid of this

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 , 
开发者_如何学Gothen  Coverage_ Array key accept any default key


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)
    }
*/


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消