What is the best php DOM 2 Array function?
You can use this (based on: http://php.net/manual/en/book.dom.php#93717);
function xml_to_array($root) {
$result = array();
if ($root->hasAttributes()) {
$attrs = $root->attributes;
foreach ($attrs as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
}
if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE) {
$result['_value'] = $child->nodeValue;
return count($result) == 1
? $result['_value']
: $result;
}
}
$groups = array();
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = xml_to_array($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = array($result[$child->nodeName]);
$groups[$child->nodeName] = 1;
}
$result[$child->nodeName][] = xml_to_array($child);
}
}
}
return $result;
}
Test;
$s = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<response>
<resData foo="1">
<contact:infData xmlns:contact="http://example.com/contact-1.0" bar="1">
<contact:status s="value1"/>
<contact:status s="value2"/>
<contact:status s="value3"/>
<contact:status s="value4"/>
</contact:infData>
</resData>
</response>';
$xml = new DOMDocument();
$xml->loadXML($s);
$xmlArray = xml_to_array($xml);
print_r($xmlArray);
// print_r($xmlArray['response']['resData']['contact:infData']['contact:status'][0]['@attributes']['s']);
// foreach ($xmlArray['response']['resData']['contact:infData']['contact:status'] as $status) {
// echo $status['@attributes']['s'] ."\n";
// }
What is the best way to move an associative array index to a different location
Not very elegant but it works:
$arr = array(
"foo" => "value1" ,
"bar" => "value2" ,
"baz" => "value3" ,
"qux" => "value4"
);
$tmp = array();
foreach($arr as $k=>$v) {
$tmp[] = array($k=>$v);;
}
array_splice($tmp, 1, 0, array_slice($tmp, 3));
array_splice($tmp, -1);
$arr = array();
foreach($tmp as $k => $v) {
foreach($v as $k2 => $v2) {
$arr[$k2] = $v2;
}
}
print_r($arr);
output:
Array
(
[foo] => value1
[qux] => value4
[bar] => value2
[baz] => value3
)
xml php DOM get each parent node from loop
<?php
$xml = '
<tag>
<item>
<id>106</id>
<title>DG</title>
</item>
<item>
<id>105</id>
<title>AC</title>
</item>
</tag>
';
$dom = new DomDocument();
$dom->loadXML($xml);
// Using SimpleXML
$root = simplexml_import_dom($dom);
foreach ($root->xpath('//item') as $item) {
$a = (array) $item;
var_dump($a);
}
// Using plain DOM
foreach ($dom->getElementsByTagName('item') as $item) {
$a = array();
for ($i = 0; $i < $item->childNodes->length; ++$i) {
$child = $item->childNodes->item($i);
if ($child->nodeType == XML_ELEMENT_NODE) {
$a[$child->tagName] = $child->nodeValue;
}
}
var_dump($a);
}
Also take a look at this answer.
Replace value in array based on other array
Those are some interesting arrays because usually numeric arrays always have a 0. I imagine you may have some different key combination so I think this is the best "future-proof" method:
foreach ($optB as $i => $optB2) {
foreach ($optB2 as $j => $val) {
if ($val) {
$opt[$i][$j] = '<strong>' . $opt[$i][$j] . '</strong>';
}
}
}
Related Topics
Mysqli Insert Error Incorrect Syntax
Pdo Last Insert Id Always the Right One
Is There a Call_User_Func() Equivalent to Create a New Class Instance
How Do We Implement Custom API-Only Authentication in Laravel
How to Keep All the Post Information While Redirecting in PHP
Jquery UI Saving Sortable List
Php: How to Convert Bigint from Int to String
Type Hinting for Properties in PHP 7
PHP Values of One Array to Key of Another Array
Cannot Initialize Mbstring with PHP 7
What Is the ASP.NET Equivalent to PHP's Echo
Regular Expressions: How to Express \W Without Underscore
Compare Two Version Strings in PHP
PHP Array, Are Array Indexes Case Sensitive
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
Cakephp Cake_Core_ Cache Was Unable to Write 'Cake_Dev_En-Us'