I am trying to pull ImageUrl's, Lat and Long from a php array where I provide the HotelCode: $hotelCode
My XML file looks like the code below:
<Hotels>
<Hotel>
<HotelCode>Code<HotelCode>
<Latitude>Lat</Latitude>
<Longitude>Long</Longitude>
<HotelImages>
<ImageURL>file.jpg</ImageURL>
<ImageURL>file2.jpg</ImageURL>
....
</HotelImages>
</Hotel>
....
</Hotels>
My PHP code is :
$xmlstring = file_get_contents($xmlurl);
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$hotels = json_decode($json,TRUE);
print_r($hotels) is:
Array (
[Hotel] => Array (
[0] => Array (
[HotelCode] => ES002A
[comment] => Array (
[0] => Array ( ) )
[Latitude] => 37.396792
[Longitude] => -5.992054
[HotelImages] => Array (
[ImageURL] => Array (
[0] => http://image.metglobal.com/hotelimages/ES002A/9405329_0x0.jpg
[1] => http://image.metglobal.com/hotelimages/ES002A/9405330_0x0.jpg
[2] => http://image.metglobal.com/hotelimages/ES002A/9405331_0x0.jpg
)
)
)
print_r($hotelCodes) is
Array ( [0] => ESG56G [1] => ES0Z10 )
I have tried some diferent methods but none of them worked.
Well, first off you have an error in your xml.
<HotelCode>Code<HotelCode>
Should be:
<HotelCode>Code</HotelCode>
After that you can get the images with:
$hotels['Hotel']['HotelImages']['ImageURL'][0];
and
$hotels['Hotel']['HotelImages']['ImageURL'][0];
And the lat long respectivly:
$hotels['Hotel']['Latitude'];
$hotels['Hotel']['Longitude'];
You can loop around the [Hotel] array with a foreach and check if the [HotelCode] element is equal to the one you provided and return the lat and long properties after that.
foreach ($hotels as $hotel) {
if($hotel['HotelCode'] == 'YourCode'){
echo $hotel['Latitude'];
echo $hotel['Longtitude'];
}
}
Use array_keys
$hotelCodes = array_keys($hotels, "HotelCode");
This will return all values with key "HotelCode". Then you can loop the array with these values.
foreach ($hotelCodes as $code)
{
$iUrls = $hotels[$code]['HotelImages']['ImageURL'];
foreach ($iUrls as $iUrl)
{
echo $iUrl;
}
echo $hotels[$code]['Latitude'];
echo $hotels[$code]['Longitude'];
}
Related
So i have a function that checks for reference in my system which is like a pyramid, i want to display who you invited and who he or she invited too, im using this foreach loop to get all reference under a user id and save them in json. but im having problem listing or extrating the results using php.
here is my pup recursive function to find all children or an entered user.
<?php
include 'includes/connect.php';
$menu['Users'] =getChildren(2);
echo '<pre>'; print_r($menu); // check tree array
function getChildren($parent_id)
{
$db=new mysqli('localhost','root','P=3h?9)Do#R#w2NQ','tobo'); //db
$refs=$db->query("SELECT * FROM users WHERE ref=$parent_id");
$children = array();
$i = 0;
foreach ($refs as $key => $downline) {
$children[$i] = array();
$children[$i]['fname'] = $downline['fname'];
$children[$i]['downline'] =getChildren($downline['id']);
$i++;
}
return $children;
}
$myJson = json_encode($menu);
$newJson = json_decode($myJson, true); ?>
it works fine and i get the following result:
Array
(
[Users] => Array
(
[0] => Array
(
[fname] => Andrey
[downline] => Array
(
[0] => Array
(
[fname] => Alisa
[downline] => Array
(
[0] => Array
(
[fname] => Maxim
[downline] => Array
(
[0] => Array
(
[fname] => james
[downline] => Array
(
)
)
)
)
)
)
)
)
[1] => Array
(
[fname] => Satori
[downline] => Array
(
)
)
)
)
my question is how can i get this result in a list using php so i can display the to the user as a list.
i tried to decode but im new to json so its still not usable by me. i hope you will be patient with my question structure, thank u very much
I am parsing the xml like following:
$result = '
<sms>
<status>0</status>
<message>
<contact_lists>
<contact_list><cl_id>11111</cl_id><phone>999999999</phone><name>Neu</name></contact_list>
<contact_list><cl_id>222222</cl_id><phone>888888888</phone><name>Alt</name></contact_list>
</contact_lists>
</message>
</sms>';
$xml = simplexml_load_string($result, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,true);
$contact_lists = $array['contact_lists']['contact_list'];
A sometimes the array look like this, which is works.
Array ( [status] => 0 [message] => Array ( ) [contact_lists] => Array ( [contact_list] => Array ( [0] => Array ( [cl_id] => 11111 [phone] => 999999999 [name] => Neu ) [1] => Array ( [cl_id] => 222222 [phone] => 888888888 [name] => Alt ) ) ) )
B but sometime if the array has only one contact_list, it will look like following
Array ( [status] => 0 [message] => Array ( ) [contact_lists] => Array ( [contact_list] => Array ( [cl_id] => 11111 [phone] => 999999999 [name] => Neu ) ) )
when i use $contact_listsin foreach loop it works with A since there are multiple array keys like 0,1,2,etc... but with B it shows error Warning: Illegal string offset 'name' etc.. since there are no array key like 0,1,2,etc...
so parsing the xml is automatically removing the key numbering which causing the the issue.
1- is there a way to keep the key numbering if only one array?
2- tried to use if (count($contact_lists) >= 1) { , but its not working as well..
Any idea for a workaround to solve such issue ?
SOLUTION:
$contact_lists_found = isset($array['contact_lists']['contact_list']) ? $array['contact_lists']['contact_list'] : '';
if ($contact_lists_found !== '' ) {
if (array_key_exists('0', $contact_lists_found)) {
// more than contact list
$contact_lists = $array['contact_lists']['contact_list'];
} else {
// only one contact list
$contact_lists[0] = $array['contact_lists']['contact_list'];
}
} else {
$contact_lists = array();
}
You could just check, if the key 0 is set, and if not, then simply overwrite the element contact_list with itself wrapped into an array:
if(!isset($array['message']['contact_lists']['contact_list'][0])) {
$array['message']['contact_lists']['contact_list'] = [
$array['message']['contact_lists']['contact_list']
];
}
0 is not a valid tag name in XML, so you should never get that, unless there was more than one <contact_list> in the XML.
I start from xml file with following input:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<severa>
<mesaj id="6caca93f" tip="ATENTIONARE"cod="GALBEN"" />
<mesaj id="6caca93g" tip="ATENTIONARE" cod="GALBEN" />
</severa>
Using PHP with xml2Array function I obtain below array:
Array
(
[0] => Array
(
[#attributes] => Array
(
[id] => 6caca93f
[tip] => ATENTIONARE
[cod] => GALBEN
)
)
[1] => Array
(
[#attributes] => Array
(
[id] => 6caca93g
[tip] => ATENTIONARE
[cod] => GALBEN
)
)
)
I read this result with foreach and insert this 2 records into MySql.
Now the problem: this works only for multiple records in xml (>2). If I have only one record in xml the array look like below and no row is inserted. Could you please advice what I should do?
Seems that this array with a single entry have a different form. I hope this is not the reason
Thank you so much!
Array
(
[#attributes] => Array
(
[id] => 6caca93f
[tip] => ATENTIONARE
[cod] => GALBEN
)
)
Foreach is like this:
foreach ($a as $row) {
$atributes = $row['#attributes'];
$id = $atributes['id'];
$tip = $atributes['tip'];
$cod = $atributes['cod'];
mysqli_stmt_execute($st);
}
Your xml2Array function is treating a single record differently from a list of records, adding an extra dimension in the second case. You need to treat the first case specially.
if (isset($a['#attributes'])) { // Single record, turn it into an array
$a = array($a);
}
Then your loop will work for both cases.
Hi would love some help on how to perform this. Cause so far what I'm doing now is failing.
this is the sample output of json turned to array.
Array
(
[0] => Array
(
[0] => Array
(
[value] => lit-PR-00-Preparing-Precise-Polymer-Solutions.html
)
)
[1] => Array
(
[0] => Array
(
[value] => 90Plus
)
)
[2] => Array
(
[0] => Array
(
[value] => Particle Size Analyzer
)
)
)
So far this is what I got so far and It's still not outputting the value I need. Would appreciate some help on what I'm doing wrong thanks.
$decode = json_decode($row['elements'], true);
echo '<pre>';
//print_r($decode);
print_r(array_values($decode));
echo '</pre>';
echo ($value['0'][1][value]);
$decode = json_decode($row['elements'], true);
// iterate through array
foreach($decode as $array_row) {
echo $array_row[0]['value'];
}
// display specific row #2
echo $decode[2][0]['value'];
PHP Arrays
I'm having some problems getting the array in these objects. When I print_r(), the following code is printed. $message_object is the name of the object.
SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[responsetime] => 2012-12-22T14:10:09+00:00
)
[data] => SimpleXMLElement Object
(
[id] => Array
(
[0] => 65233
[1] => 65234
)
[account] => Array
(
[0] => 20992
[1] => 20992
)
[shortcode] => Array
(
[0] => 3255
[1] => 3255
)
[received] => Array
(
[0] => 2012-12-22T11:04:30+00:00
[1] => 2012-12-22T11:31:08+00:00
)
[from] => Array
(
[0] => 6121843347
[1] => 6121820166
)
[cnt] => Array
(
[0] => 24
[1] => 25
)
[message] => Array
(
[0] => Go tramping wellington 11-30
[1] => Go drinking Matakana 2pm
)
)
)
I'm trying to get the id arrays out of the objects with a foreach:
foreach($message_object->data->id AS $id) {
print_r($id);
}
The following reply is sent:
SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 )
How do I get the value of [0] or am I going about this wrong? and is there a way to loop though the results and get the object keys?
I have tried to echo $id[0] but it returns no result.
When you use print_r on a SimpleXMLElement there comes magic in between. So what you see is not actually what is there. It's informative, but just not the same as with normal objects or arrays.
To answer your question how to iterate:
foreach ($message_object->data->id as $id)
{
echo $id, "\n";
}
to answer how to convert those into an array:
$ids = iterator_to_array($message_object->data->id, 0);
As this would still give you the SimpleXMLElements but you might want to have the values you can either cast each of these elements to string on use, e.g.:
echo (string) $ids[1]; # output second id 65234
or convert the whole array into strings:
$ids = array_map('strval', iterator_to_array($message_object->data->id, 0));
or alternatively into integers:
$ids = array_map('intval', iterator_to_array($message_object->data->id, 0));
You can cast the SimpleXMLElement object like so:
foreach ($message_object->data->id AS $id) {
echo (string)$id, PHP_EOL;
echo (int)$id, PHP_EOL; // should work too
// hakre told me that this will work too ;-)
echo $id, PHP_EOL;
}
Or cast the whole thing:
$ids = array_map('intval', $message_object->data->id);
print_r($ids);
Update
Okay, the array_map code just above doesn't really work because it's not strictly an array, you should apply iterator_to_array($message_object->data_id, false) first:
$ids = array_map('intval', iterator_to_array$message_object->data->id, false));
See also: #hakre's answer.
You just need to update your foreach like this:
foreach($message_object->data->id as $key => $value) {
print_r($value);
}