Simplexml xpath array result - php

I have an xpath query on a simplexml element like so:
$theSimpleXmlObject->xpath('//path/to/*/node');
it returns an array of results which is great but it is an array of simplexml objects and i just want a flat array.
Is this possible?
Ideally i would have a simple array returned with the matching node values instead of having to iterate over the results to prepare the array.

Try in this way to get Array
$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);
When you cast xml to array it will cast only "first level", so we can made it by convert it in JavaScript Object Notation and decode it again to array. Serialization will not help, becouse it keep data-structure.

Related

How to parse array in PHP.

I have the following array I want to get the element one by one.
"attachment":"["Apple","car","home","scooter"]"
If the array is in JSON form you do json_decode and the iterate over the array

How to retrieve an element from this PHP array?

So basically I have this array stored in SQL and I'm trying to retrieve the first element from it using PHP
I'm not sure if this is an associative or multidimensional array
a:3:{i:0;s:11:"Downpayment";i:1;s:28:"Variable 1 ";i:2;s:28:"Variable 2";}
How do I extract elements from this array ?
This is a PHP representation of serialized data. Use unserialize() on it.
If you not sure in this array you can use current() function or foreach to get first element of this array
For set inner pointer on first element use reset()

Getting value from string by index

I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}

interpreting JSON with PHP and putting it in array

I have only worked with JSON once before, but I don't recall how to use it with PHP.
If I have a script that returns JSON like this:
{
"bunisess":[
"business",
"bonuses",
"burnooses",
"boniness",
"burnoose's"
]
}
how can I take that and make each value a value in a PHP array. The keys just being numbers from 0 onwards?
Use json_decode, but pass true as the second parameter to get an associative array back:
$json='{"bunisess":["business","bonuses","burnooses","boniness","burnoose\'s"]}';
$data=json_decode($json, true);
Now, you can use array_values to get a numerically indexed array as you required:
$data=array_values($data);
A simple google search could have lead you to this page : http://php.net/manual/fr/function.json-decode.php
The subarray will be respecting exatcly what you're asking for.
Assuming that $data is the complete JSON string
$stdObject = json_decode($data, true);
print_r($array);
You should get an array with one key bunisess and value another numeric array with the other values.

Sort simplexml object - without casting to array

Is it possible to sort a simplexml object but keep the object as a simplexml object instead of casting to an array.
There doesnt appear to be a way to do this - and if casted to an array it seems very messy to transform the array(s) back to a simplexml object.
So -
Is it possible to sort a simplexml object by an attribute etc.
For example (sort by the position attribute node):
<test_nodes>
<node position="1"></node>
<node position="2"></node>
<node position="3"></node>
</test_nodes>
If not then what is the most painless and efficient way to cast an array to a simple xml object

Categories