I can't find an exact duplicate of this question, as my nodes are numerical keys.
I need to sort this xml by date - newest first:
[article] => Array
(
[0] => SimpleXMLElement Object
(
[value] => some value
[date] => 2012-08-13 18:54:09
)
[1] => SimpleXMLElement Object
(
[id] => some more value
[date] => 2012-08-09 10:24:06
)
[2] => SimpleXMLElement Object
(
[id] => another value
[date] => 2012-08-11 20:45:44
)
)
How can I best do that - possibly WITHOUT turning the XML into a PHP array and sorting it then.
From your sample, it looks like you have an array of objects already, so usort would work fine.
The callback would look something like this:
function compare_node_dates($a, $b)
{
return
strtotime((string)$b->date)
-
strtotime((string)$a->date);
}
However, if your outermost variable isn't actually an array, there is no way I'm aware of doing this without creating an array, sorting that, and using that to construct brand new XML.
Related
I have a PHP standard class object converted from json_decode of a REST call on an API which looks like :
Array
(
[1437688713] => stdClass Object
(
[handle] => Keep it logically awesome.
[id] => 377748
[ping] => stdClass Object
(
[url] => https://api.me.com
[id] => 377748
[name] => web
[active] => 1
[events] => Array
(
[0] => data_new
[1] => data_old
)
So far i had no issues in parsing any of the PHP objects. However this one is failing because i can not access the nested object elements using a key since 1437688713 is not assigned to a key and accessing an object is failing if i try to do this:
$object->1437688713->handle
Is there a way to access these elements ?
Update: one more thing, i would never know this value (1437688713) in advance. Just like a key. All i get is a stdclass object which i have to parse.
The outer part of your data is an array, not an object. Try:
$array['1437688713']->handle;
or if you don't know the key, you can iterate over the array (handy if it may contain multiple objects too):
foreach ($array as $key => $object) {
echo $key; // outputs: 1437688713
echo $object->handle; // outputs: Keep it logically awesome.
}
Get the first item from $object array
$first_key = key($object);
Use it with your response array,
$object[$first_key]->handle;
Or, the first element of array
$first_pair = reset($object)->handle;
I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];
I have a SimpleXMLElement and would like to check if a particular element has a non-blank value before looping through it utilizing a foreach loop. Here is my SimpleXMLObject
Say this is contained in $myXMLElement
SimpleXMLElement Object ( [f] => Array ( [0] => Marcus [1] => Smith [2] => Brown University [3] => 1243123200000 [4] => Masters [5] => TestValue [6] => TestValue2 [7] => 4 [8] => SimpleXMLElement Object ( [#attributes] => Array ( [id] => 16 ) ) [9] => 0 ) [update_id] => 1325795135203 )
Within the array is a value of
[3] => 1243123200000
This is what I would like to check this parameter and determine that it is not NULL. If I wanted to grab it as a String what would be the way to do this.
I was hoping for something like (string)$myXMLElement[3] but that does not appear to be correct syntax for what I am trying.
You can use array notation, but since the array is part of the node f you have to access it like this:
$val = $xmlObj->f[3];
if (empty($val)) {
// its empty
}
On an unrelated note, since the 8th element in that array is itself a SimpleXML object, you would access its values like this:
$val = $xmlObj->f[8]->update_id;
In these cases, its just a mix of object access and array access notation.
To get the attributes from the 8th element, you can also use array notation:
$id = $xmlObj->f[8]['id']; // get "id" attribute
The page on Basic SimpleXML Usage has some very helpful examples showing how to access different elements from a SimpleXML object. Example #5 show how to access attributes.
I have a multidim array that I can view using print_r($users):
Array
(
[0] => stdClass Object
(
[username] => crustacea
[created_on] => 2010-08-07 19:54:32
[active] => 1
)
)
I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.
I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?
$user[0] is not an array but an object as indicated by [0] => stdClass Object.
echo $users[0]->username;
$user[0] is object and not an array. You can access your value by doing
$users[0]->username;
Or you could cast/convert your object into an array like
((array)$users[0])['username'];
I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(