note 117468 added to function.array-combine
| From: | (at) | Date: | Sun, 14 Jun 2015 08:43:29 +0000 |
| Subject: | note 117468 added to function.array-combine | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202859@lists.php.net to get a copy of this message | ||
Nullable array, eg:
//$array1 = ['value1', 'value2', 'value3'];
//$array2 = ['foo', 'bar'];
function array_nullable(array $array1, array $array2)
{
$iterable = [$array1, $array2];
$cntArr1 = count($array1);
$cntArr2 = count($array2);
$index = $cntArr1 > $cntArr2 ? 0 : 1;
$tmp = [];
foreach($iterable[$index] as $key => $value)
{
$inversion = $iterable[ (int) ! $index];
if (isset($inversion[$key]))
$tmp[$value] = $inversion[$key];
else
$tmp[$value] = NULL;
}
return $tmp;
}
If $array1 more than $array2 then $array1 uses for keys, otherwise $array2.
/*
echo '1)';
var_dump(array_nullable(['value1', 'value2', 'value3'],
['foo', 'bar']));
echo '2)';
var_dump(array_nullable(['foo', 'bar'], ['value1',
'value2', 'value3']));
output:
1)
array (size=3)
'value1' => string 'foo' (length=3)
'value2' => string 'bar' (length=3)
'value3' => null
2)
array (size=3)
'value1' => string 'foo' (length=3)
'value2' => string 'bar' (length=3)
'value3' => null
*/
----
Server IP: 72.52.91.14
Probable Submitter: 46.188.125.207
----
Manual Page -- http://php.net/manual/en/function.array-combine.php
Edit -- https://master.php.net/note/edit/117468
Del: integrated -- https://master.php.net/note/delete/117468/integrated
Del: useless -- https://master.php.net/note/delete/117468/useless
Del: bad code -- https://master.php.net/note/delete/117468/bad+code
Del: spam -- https://master.php.net/note/delete/117468/spam
Del: non-english -- https://master.php.net/note/delete/117468/non-english
Del: in docs -- https://master.php.net/note/delete/117468/in+docs
Del: other reasons-- https://master.php.net/note/delete/117468
Reject -- https://master.php.net/note/reject/117468
Search -- https://master.php.net/manage/user-notes.php