note 120019 added to simplexmlelement.xpath
| From: | AlexisPeters | Date: | Tue, 11 Oct 2016 09:11:53 +0000 |
| Subject: | note 120019 added to simplexmlelement.xpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206831@lists.php.net to get a copy of this message | ||
If you want this behavior for an multidimensional array you can use this:
<?php
function apath(&$a, $path, $value = NULL) {
$pathes = explode('/',$path);
if(count($pathes) > 1) {
$firstPart = array_shift($pathes);
if(!isset($a[$firstPart])) {
$a[$firstPart] = array();
}
return apath($a[$firstPart], implode('/', $pathes), $value);
} else {
if($value){
$a[$path] = $value;
} else {
return $a[$path];
}
}
}
apath($a, 'user/name', array('prename' => 'foo',
'lastname' => 'bar'));
$a = array();
var_dump($a);
var_dump(apath($a, 'user/name/lastname'));
?>
Gives you this:
array(1) {
["user"]=>
array(1) {
["name"]=>
array(2) {
["prename"]=>
string(5) "foo"
["lastname"]=>
string(8) "bar"
}
}
}
string(8) "bar"
----
Server IP: 72.52.91.14
Probable Submitter: 80.228.63.221
----
Manual Page -- http://php.net/manual/en/simplexmlelement.xpath.php
Edit -- https://master.php.net/note/edit/120019
Del: integrated -- https://master.php.net/note/delete/120019/integrated
Del: useless -- https://master.php.net/note/delete/120019/useless
Del: bad code -- https://master.php.net/note/delete/120019/bad+code
Del: spam -- https://master.php.net/note/delete/120019/spam
Del: non-english -- https://master.php.net/note/delete/120019/non-english
Del: in docs -- https://master.php.net/note/delete/120019/in+docs
Del: other reasons-- https://master.php.net/note/delete/120019
Reject -- https://master.php.net/note/reject/120019
Search -- https://master.php.net/manage/user-notes.php