note 93528 deleted from function.array-filter by cmb
| From: | cmb@php.net | Date: | Sun, 06 Oct 2019 08:10:33 +0000 |
| Subject: | note 93528 deleted from function.array-filter by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212493@lists.php.net to get a copy of this message | ||
Note Submitter: mewsterus at yahoo dot com
----
Here's an easy way to get a combination of keys and values, such that if you don't specify
a key you will get the value, and if you do specify a key you will get the key:
<?php
$array = array('One' => 'First', 'Second', 'Third',
'Four' => 'Fourth', 'Fifth');
var_dump(array_keys($array));
$names = array_filter(array_keys($array), 'is_string') + array_values($array);
ksort($array);
var_dump($names);
?>
Outputs:
array(5) {
[0]=>string(3) "One"
[1]=>int(0)
[2]=>int(1)
[3]=>string(4) "Four"
[4]=>int(2)
}
array(5) {
[0]=>string(3) "One"
[1]=>string(6) "Second"
[2]=>string(5) "Third"
[3]=>string(4) "Four"
[4]=>string(5) "Fifth"
}
Without using ksort, the keys appear before the fallback values, instead of inline and appearing
like they replace them, however the keys are intact (which is why ksort works) so it's only
execution order.