note 93528 added to function.array-filter
| From: | mewsterus at yahoo dot com | Date: | Mon, 14 Sep 2009 17:13:30 +0000 |
| Subject: | note 93528 added to function.array-filter | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160560@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 208.69.120.35
Probable Submitter: 129.186.252.140
----
Manual Page -- http://www.php.net/manual/en/function.array-filter.php
Edit -- https://master.php.net/note/edit/93528
Del: integrated -- https://master.php.net/note/delete/93528/integrated
Del: useless -- https://master.php.net/note/delete/93528/useless
Del: bad code -- https://master.php.net/note/delete/93528/bad+code
Del: spam -- https://master.php.net/note/delete/93528/spam
Del: non-english -- https://master.php.net/note/delete/93528/non-english
Del: in docs -- https://master.php.net/note/delete/93528/in+docs
Del: other reasons-- https://master.php.net/note/delete/93528
Reject -- https://master.php.net/note/reject/93528
Search -- https://master.php.net/manage/user-notes.php