method toKeypair documentation in bbn\X
Converts a numeric array into an associative one, alternating key and value.
Example
X::toKeypair(['Test', 'TestFile', 'Example', 'ExampleFile']);
// string ['Test' => 'TestFile', 'Example' => 'ExampleFile']
function(array $arr, bool $protected = true)
{
$num = \count($arr);
$res = [];
if (($num % 2) === 0) {
$i = 0;
while (isset($arr[$i])) {
if (!\is_string($arr[$i]) || ($protected && preg_match('/[^0-9A-Za-z\-_]/', Str::cast($arr[$i])))) {
return false;
}
$res[$arr[$i]] = $arr[$i + 1];
$i += 2;
}
}
return $res;
}
Converts a numeric array into an associative one, alternating key and value. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions