Get output from XML array [duplicate] - php

This question already has answers here:
PHP get values from SimpleXMLElement array
(5 answers)
Closed 9 years ago.
I'm using XML for the use of different languages. First of all, I'm not used to work with XML so I want to ask if I'm doing it right. Here is my XML code:
<lang>
<one>
<ENG>Text1</ENG>
<NL>Text2</NL>
</one>
</lang>
When I load this in php I get this array:
SimpleXMLElement Object ( [one] => SimpleXMLElement Object ( [ENG] => Text1 [NL] => Text2 ) )
I'm now trying to get each single element out of the XML, I have this code:
$xml = simplexml_load_file($file);
$result = $xml['one']['ENG'];
But it doesn't return any result, please help.
Thanks.

Use object accessor syntax instead:
$result = $xml->one->ENG;

Related

Extract value from multi-dimension array in php [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 3 years ago.
I'm trying to slice a multi-array in php. I'm using google's geo-location api and I've managed to get it to return lat,lng and accuracy. I've used
$message = json_decode($result, true);
To convert the Json into an array.
The problem I have is that I have the results in this form:
Array ( [location] => Array ( [lat] => 10.1453652 [lng] => 0.2338764 ) [accuracy] => 33 )
and I can't figure out how to extract the lat and lon. Can anyone help?
Try the following code:
echo $message['location']['lat'];
It's simple, if not what you are expected, its straight forward
echo $message['location']['lat'].' '.$message['location']['lng'];
Working demo.

How can I convert JSON Array to array in PHP? [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
This is the value I would like to convert to array. I know it's quite a simple question, but please can anyone help me?
Use json_decode
json_decode($your_jsonString,true)
You should use json_decode.
Try this:
$data = json_decode($your_json_string, TRUE);
The second parameter will make decoded json string into an associative arrays.
Example :-
$data = '{"receiver_uid":["58a43a3e3fbbf3.61108490","58a43be07a3bc3.90311110","58da53ab5ce8d6.84754819"]}';
$array = json_decode($data, true);
echo "<pre>";
print_r($array);
Output would be like
Array
(
[receiver_uid] => Array
(
[0] => 58a43a3e3fbbf3.61108490
[1] => 58a43be07a3bc3.90311110
[2] => 58da53ab5ce8d6.84754819
)
)

array to get object's value failed in php [duplicate]

This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 8 years ago.
why is this wrong?
[enclosure] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
[length] =>
[type] => image/jpeg
)
)
I want to get the url to get the image file
I wrote print_r($eachItem->enclosure['#attributes']->url) it doesn't work. Why?
That is not the correct way of getting the attribute value. Use ->attributes() method:
echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)
Or
// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];
Right & Quick Format
$eachItem->enclosure->attributes()->{'url'};

Re-order XML results based on XML data [duplicate]

This question already has answers here:
PHP sorting issue with simpleXML
(3 answers)
Closed 9 years ago.
We are fetching data from a remote server via their API. Unfortunately, their API does not order the returned data by date.
I am trying, without much success, to figure out how to re-organize the data so that it is ordered by the next_bookable_date. We are using PHP and SimpleXMLElement to parse the data and create a string which is then inserted into a webpage. But the current result is in the same order as data appears in the returned XML.
The basic XML results are below. There is much more data, that I stripped out to save space.
SimpleXMLElement Object
(
[request] => GET search.xml?start_date=2013-05-03&end_date=2013-05-17
[error] => OK
[total_tour_count] => 4
[tour] => Array
(
[0] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-13
[tour_name] => Thailand Tour
)
[1] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-12
[tour_name] => Bali Tour
)
[2] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-05
[tour_name] => Hawaii Tour
)
[3] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-06
[tour_name] => Bhutan Tour
)
)
)
The PHP code we are using to generate the html string (again stripped of a bit of html code to save space):
foreach($result->tour as $tour) {
$tourname = $tour->tour_name;
$tourdate = $tour->next_bookable_date;
// create string for dpt-soon
$dpt_soon_list .= "<li> some html using the above values </li>\n";
}
Is there a way to re-order the XML data once we receive it from the remote server? Or is there a way to reorder the PHP output when running the foreach?
You can use usort() to sort multidimensional arrays or objects. I wrote this bit of code to explain how to use it with SimpleXML:
<?php
// Load the XML file
$xml = simplexml_load_file("xml.xml");
// Get all children into an array
$Tours = (array)$xml->children();
$Tours = $Tours["tour"];
// Call usort on the array
usort($Tours, "sorttours");
// Output results
echo "<pre>".print_r($Tours, true)."</pre>";
// The function that specifies when an entry is larger, equal or smaller than another
function sorttours($a, $b) {
// Parse strings into a date for comparison
$Date1 = strtotime($a->next_bookable_date);
$Date2 = strtotime($b->next_bookable_date);
// If equal, return 0
if ($Date1 == $Date2) {
return 0;
}
// If Date1 is larger, return 1, otherwise -1
return ($Date1 > $Date2) ? 1 : -1;
}
?>
This example assumes that the XML looks somewhat like this:
<?xml version="1.0"?>
<tours>
<tour>
<next_bookable_date>2013-05-13</next_bookable_date>
<tour_name>Thailand Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-12</next_bookable_date>
<tour_name>Bali Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-05</next_bookable_date>
<tour_name>Hawaii Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-06</next_bookable_date>
<tour_name>Bhutan Tour</tour_name>
</tour>
</tours>
If that is not the case, then you need to rewrite the sorttours function to use e.g. attributes to determine the order.

How to convert array in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP : Create array for JSON
I have an array like this :
Array ( [0] => tag1 [1] => tag2 [2] => tags17c [3] => tags20 [4] => tags21)
I want put that array on javascript, and the format must be like this :
var Tag = ['tag1', 'tag2', 'tags17c', 'tags20', 'tags21'];
How to convert it?
please, help me out..
var Tag = <?php echo json_encode(array_map("htmlspecialchars", $your_array)); ?>;
My advice would be to use json for that, as Charly suggested, using json_encode. Though there is no real reason for using array_values() as far as I know,
var Tag = <?php echo json_encode($array); ?>;

Categories