I have an XML that looks like this...
<fields>
<field>
<id>86</id>
<source>system</source>
<type>integer</type>
<name>bounce_count</name>
<label>Bounce count</label>
<validation/>
<store-locally>true</store-locally>
<display-in-profile>false</display-in-profile>
<include-in-export>false</include-in-export>
<lma_display>false</lma_display>
<newsletters></newsletters>
</field>
</fields>
I need to loop in through the value of every tag inside <field>. I tried the following (result is the XML), first I got everything within the field tag...
$value = $result->field
$value = simplexml_load_string($value);
If I var_dump() this it looks as so...
object(SimpleXMLElement)#47 (11) { ["id"]=> string(3) "153" ["source"]=> string(6) "client" ["type"]=> string(6) "string" ["name"]=> string(4) "name" ["label"]=> string(4) "Name" ["validation"]=> object(SimpleXMLElement)#48 (0) { } ["store-locally"]=> string(4) "true" ["display-in-profile"]=> string(4) "true" ["include-in-export"]=> string(4) "true" ["lma_display"]=> string(4) "true" ["newsletters"]=> object(SimpleXMLElement)#46 (0) { } }
Next I try looping through the fields inside field
foreach ($value as $single_field) {
echo $single_field;
}
But I get no output. If I var_dump() $single_field I get the same result as above. I need to loop through the values within each field. So for the XML above I need to store the following values...
86
system
integer
bounce_count
Bounce Count
true
false
false
false
How can I do this?
If you are trying to iterate over children of the element, you need to use SimpleXMLElement::children in your foreach loop:
foreach ($value->children() as $single_field) {
echo $single_field . "\n";
}
Related
I have the following SimpleXMLElement:
object(SimpleXMLElement)#10 (3) {
["#attributes"]=> array(3) {
["id"]=> string(8) "18022352"
["name"]=> string(14) "The Salmon Man"
["slug"]=> string(14) "the-salmon-man"
}
["bids"]=> object(SimpleXMLElement)#11 (1) {
["price"]=> array(1) {
[0]=> object(SimpleXMLElement)#13 (1) {
["#attributes"]=> array(4) {
["decimal"]=> string(4) "9.60"
["percent"]=> string(5) "10.42"
["backers_stake"]=> string(5) "40.36"
["liability"]=> string(6) "347.00"
}
}
}
}
["offers"]=> object(SimpleXMLElement)#12 (1) {
["price"]=> array(1) {
[0]=> object(SimpleXMLElement)#15 (1) {
["#attributes"]=> array(4) {
["decimal"]=> string(4) "9.20"
["percent"]=> string(5) "10.87"
["backers_stake"]=> string(5) "85.35"
["liability"]=> string(5) "10.41"
}
}
}
}
}
Why does this work:
$horse[0]['name']
but this doesn't:
$horse[0]['bids'] // also tried $horse['bids'] and other ways
I can get the values like below but I was hoping to search the smaller object:
$xml->xpath("//odds/event[#id='$matchid']/market[#slug='to-win']/contract[#name='$pony']/bids"); // $pony == $horse[0]['name']
It's often easier to look at the XML itself, rather than a print_r of the SimpleXML object. In this case, you have something like this:
<horse id="18022352" name="The Salmon Man" slug="the-salmon-man">
<bids>
<price decimal="9.60" percent="10.42" backers_stake="40.36" liability="347.00" />
</bids>
<offers>
<price decimal="9.60" percent="10.42" backers_stake="40.36" liability="347.00" />
</offers>
</horse>
The bids item is an element, and as discussed in the SimpleXML Basic Usage guide, you access child elements with ->foo notation, whereas attributes use ['foo'] notation.
So if $horse is that <horse> element, you need:
$name = $horse['name'];
$bids = $horse->bids;
Note that this is equivalent to explicitly asking for the first child called bids; both forms will work regardless of whether there is actually more than one identically name element:
$bids = $horse->bids[0];
Now there are presumably one or more <price> elements in practice, so you might want to loop over, and then echo each decimal attribute. That would look like this:
foreach ( $horse->bids->price as $price ) {
echo $price['decimal'];
}
Again, this loop will work fine with only one <price>, it will just just loop once. If there is only ever one price, or you only care about the first one, you could just write:
echo $horse->bids->price['decimal'];
Which is equivalent to:
echo $horse->bids[0]->price[0]['decimal'];
Or of course either of these:
echo $horse->bids[0]->price['decimal'];
echo $horse->bids->price[0]['decimal'];
The number of different ways of getting to the same place is one reason I don't recommend relying too much on print_r output. Another is that it sometimes can't display everything that's in the XML, which doesn't mean that data isn't available if you ask for it.
I've got a codeigniter active record query that uses get('table_name')->result();
The output is below, what i'd like to do is unserialize(base_64_decode) the "venue_opening_hours" string and replace that string in the data structure with the result of the unserialized & base64_decoded data. I know i can array_walk to do this normally, but i don't see how to access that particular bit of data as an object within an array...
Thanks!
array(2) {
[0]=>
object(stdClass)#142 (4) {
["entry_id"]=>
string(2) "15"
["google-id"]=>
string(40) "552e7c08d3b86c14d130ebe43a0ba421d03a60ae"
["venue_opening_hours"]=>
string(148) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319fX0="
["title"]=>
string(18) "Place Name"
}
[1]=>
object(stdClass)#143 (4) {
["entry_id"]=>
string(2) "18"
["google-id"]=>
string(40) "71d9c8e1f64f330637c96d30a0ae15533836a85e"
["venue_opening_hours"]=>
string(972) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxMDp7aTowO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjE7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319aToyO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjM7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo0O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIyIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjU7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMiI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo2O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI0IjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjc7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNCI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo4O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI1IjtzOjQ6InRpbWUiO3M6NDoiMjM0NSI7fX1pOjk7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzNDUiO319fX19"
["title"]=>
string(24) "Other place name"
}
}
You'd access it using:
$array[0]->venue_opening_hours
in a for loop...
//foreach ($array as &$arrayItem)
//{
foreach ($arrayItem as &$object)
{
// extract and convert it...
//$openinghours = unserialize(base64_decode($object->venue_opening_hours));
// Update it...
$object->venue_opening_hours = $unserialize(base64_decode($object->venue_opening_hours));
}
//}
The &$object is a reference, so the assignment will change the value in the original result set...
Also I forgot to also loop the array ... I think! so added the outer loop :)
So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!
If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.
I have the following already decoded json stored in $response = $result->response;:
object(stdClass)#6 (5) {
["EmailAddress"]=> string(18) "email#gmail.com"
["Name"]=> string(0) ""
["Date"]=> string(19) "2011-10-09 19:32:00"
["State"]=> string(6) "Active"
["CustomFields"]=> array(1) {
[0]=>object(stdClass)#7 (2) {
["Key"]=>string(2) "id"
["Value"]=>string(6) "Dl9lIz"
}
}
I can already access the main attributes (EmailAddress, Name, etc) with:
$email = $response->{'EmailAddress'};
print $email;
But I need to access the "Value" portion in the CustomFields object. I don't know how to dig that deep. I'm attempting to do this in PHP..
Any suggestions?
It is contained in the first element ([0]) of array CustomFields, so you can access it with an object operator (->) after the array index.
print $response->CustomFields[0]->Value;
I use PHP and Simple XML.
I use a loop that does not work like expected:
foreach($item->Image->attributes()->source as $key => $value)
{
echo $value;
}
In the foreach I try to tell that I want to get the "source" of the image which is listed in the attributes.
$item above is created with a loop around my code above foreach($xml_content->Section->Item as $item {}, (if you need to know where it came from)
My object looks like this:
object(SimpleXMLElement)#36 (4) {
["Text"]=>
string(15) "Vinbergs socken"
["Description"]=>
string(73) "Vinbergs socken ingick i Faurås härad och ligger i Falkenbergs kommun.
"
["Url"]=>
string(44) "http://sv.wikipedia.org/wiki/Vinbergs_socken"
["Image"]=>
object(SimpleXMLElement)#38 (1) {
["#attributes"]=>
array(3) {
["source"]=>
string(113) "http://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Faur%C3%A5s_Vinberg.svg/50px-Faur%C3%A5s_Vinberg.svg.png"
["width"]=>
string(2) "50"
["height"]=>
string(2) "41"
}
}
}
What is wrong with my loop in the beginning of my post?
Your are trying to iterate a string, not an array
$item->Image->attributes()->source
To iterate all the attributes of the Image element, use
foreach ($item->Image->attributes() as $attributeName => $attributeValue) {
If you just want to output the value of the source attribute, do not iterate but use the shorthand
echo $item->Image['source']
See this demo and the SimpleXml Basic Usage Examples in the PHP Manual