note 111693 deleted from function.array-combine by aharvey
| From: | aharvey@php.net | Date: | Mon, 18 Mar 2013 16:23:54 +0000 |
| Subject: | note 111693 deleted from function.array-combine by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194112@lists.php.net to get a copy of this message | ||
Note Submitter: Daniel Loureiro
----
a little (and blazing-fast) code to convert tags to array (xml, html, etc):
<?php
function array_combine_($keys, $values)
{
$result = array();
foreach ($keys as $i => $k) {
$result[$k][] = $values[$i];
}
array_walk($result, create_function('&$v', '$v = (count($v) == 1)?
array_pop($v): $v;'));
return $result;
}
function extract_data($str)
{
return
(is_array($str))?
array_map(__NAMESPACE__. '\extract_data', $str):
((!preg_match_all('#<([A-Za-z0-9]*)(?:\s+[^>]+)?>(.*?)</\1>#s', $str,
$matches))?
$str:
array_map((__NAMESPACE__. '\extract_data'), array_combine_($matches[1], $matches[2])));
}
print_r(extract_data("<a><b>1</b><c>2</c><c>3</c></a>"));
?>
Results:
[a] => Array
(
[b] => 1
[c] => Array
(
[0] => 2
[1] => 3
)
)