I have the following array in php and I would like to get the lat and lng.The array is
[results] => Array
(
[0] => stdClass Object
(
[geometry] => stdClass Object
(
[location] => stdClass Object
(
[lat] => 52.222306
[lng] => 0.093831
)
)
)
)
Now there is only one element but it can be multiple.So I need to use foreach loop.The desired array structure should look like
array('lat=>'52.222306', 'lng'=>'0.093831', 'lat=>'xxx', 'lng'=>'yyy');
Can you please suggest me the best possible way.Thank you in advance.
The results in the array that you requested is not possible but this will give you an array of arrays.
$coords = array();
foreach ($results as $result){
$coords[] = array("lat"=>$result->geometry->location->lat,
"lng"=>$result->geometry->location->lng);
}
Related
I have this array (decoded from JSON, output with print_r):
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[item] => te
[date] => 13.10
)
[1] => stdClass Object
(
[item] => te
[date] => 13.10
)
[2] => stdClass Object
(
[item] => tr
[date] => 13.10
)
)
)
But now I have to remove all the duplicates.
If I try $result = array_unique($array, SORT_REGULAR);
$result is null.
Can someone spot my mistake?
This is a stdClass object, not an array. When you decode by using the function json_decode, you need to pass the parameter "true" to have an array:
$array = json_decode($json, true);
Edit: As people noticed in the comments, the actual array exists in $array['data'], so the array_unique must be applied on $array['data'] instead of $array.
I have an array generated out of json_decode().
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
The output array looks like below:
Array
(
[#attributes] => Array
(
[version] => 1.0
)
[response] => Array
(
[operation] => Array
(
[#attributes] => Array
(
[name] => ADD_REQUEST
)
[result] => Array
(
[statuscode] => 200
[status] => Success
[message] => Request added successfully
)
[Details] => Array
(
[0] => Array
(
[workorderid] => 291885
)
[1] => Array
(
[parameter] => Array
(
[name] => workorderid
[value] => 291885
)
)
)
)
)
)
I need to save the value of the key 'workorderid' in another php varaible,so I can use it further in my code. the value is dynamic.
I have been struggling a lot now and looking for some guidance.
Could anyone please help with out in getting this done? Thanks a lot in advance!
Regards,
Pooja
If you know for sure the first array under Details will contain the workorderid key, you can just access it directly:
$workorderid = $array_data['response']['operation']['Details'][0]['workorderid'];
var_dump($workorderid);
Output:
string(6) "291885"
If you don't know in which array under Details it will be, you'll have to loop over it and find it:
$workorderid = null;
foreach ($array_data['response']['operation']['Details'] as $detail) {
if (isset($detail['workorderid'])) {
$workorderid = $detail['workorderid'];
break;
}
}
if (null !== $workorderid) {
var_dump($workorderid);
}
Output:
string(6) "291885"
This is a viable solution if you only need to fetch 1 key from the response. If you'd need more keys I'd suggest mapping the response data into a more readable structure.
I have a PHP array object that can contain zero or more values like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
)
)
I want to loop through each value in this array and use the id in a query that returns an object that looks like this:
Array
(
[0] => stdClass Object
(
[id] => taWPlKGXHR5Y03cc
[title] => Test Document Title
[filename] => test.docx
)
)
On each iteration of the loop the query returns with one result in the form of an array object. I want to add the object to an array object that in this case would look something like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
[title] => Test Document Title 0
[filename] => test0.docx
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
[title] => Test Document Title 1
[filename] => test1.docx
)
)
The query is written and working and I know I need to use a foreach loop to iterate over the array of IDs, but I don't quite get how to set it up so that the end result is an array object as listed just above. I'm using PHP & Codeigniter to do all of this.
The code of the foreach I have so far is something like this:
$child = array();
foreach ($id as $row) {
$child = $this->users_model->get_docnfo($id);
}
Thanks for reading!
You should try it with
$child = array();
foreach ($id as $row) {
$child[] = $this->users_model->get_docnfo($row->id);
}
Note the $row->id instead of $id and also the brackets after $child.
I have this object returned:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Amount should be numeric.
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Please enter your Reference Number.
)
)
)
How can I get the desc values? I need to get both Desc Values('Amount should be numeric.' and 'Please enter your Reference Number. ')
I have tried:
$res = $str[0];
it returned:
SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Amount should be numeric.
)
)
Your object is a SimpleXML object.
You can learn how to use it here:
http://php.net/manual/fr/book.simplexml.php
To solve your issue, you can use this:
$res0 = $str[0]->attributes()->Desc;
$res1 = $str[1]->attributes()->Desc;
Call attributes() and then access them as properties.
$node->attributes()->Desc
If I have an array like the one below what would be the best way to get all of the ListingCategory items out of the array so I can use them in an if statement within a foreach?
Array:
Array (
[0] => Array (
[ListingId] => SimpleXMLElement Object ([0] => 532712629)
[ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- )
) [1] =>
This will cycle through all array items, and all of the children within each ListingCategory.
foreach($array as $item)
{
foreach($item["ListingCategory"]->children() as $val)
{
doSomething($val);
}
}