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
Related
I got the following object in PHP:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => France
)
[0] => France
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => Nederland
)
[0] => Netherlands
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => Deutschland
)
[0] => Germany
)
)
How do I get only the values sitting right after [0]?
For the given example I would like to have: France Netherlands Germany
To get string value of SimpleXMLElement Object cast object to string explicitly:
$xmlObject = ...; // your object here
$str = (string) $xmlObject;
echo $str;
// also if you just
echo $xmlObject;
// $xmlObject will be casted to string implicitly
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 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);
}
I am parsing the array given below. Looping through td using foreach. Now i want to select the value other than [#attributes]. I cannot use a or p specifically as they change through out the objects.
How can i achieve this?
[0] => SimpleXMLElement Object
(
[th] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowspan] => 2
[scope] => row
)
[p] => Memory
)
[td] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => ttl
)
[a] => Card slot
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => nfo
)
[p] => No
)
)
)
Want the solution to work in php.
Try below one
<?php
foreach($td as $element)
{
foreach($element as $key => $value)
{
if(!preg_match("/#/", $key) && !is_array($value))
echo $element[$key];
}
}
?>
I have the following xml to be parsed.
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => xyz#gmail.com
[primary] => true
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => abc#gmail.com
[primary] => true
)
)
)
I have this above xml and I need to get only adress from this xml.
foreach ($result as $title) {
$email[$count++]=$title->attributes()->address->__toString;
}
debug($email);
The result is this. But I want only address . need some help.
Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
)
see : http://www.php.net/manual/en/simplexmlelement.attributes.php
Return Values
Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.
the solution is to cast the value into string,
for example :
$email[$count++]=(string)$title->attributes()->address;
Or iterate the return value will work as well
eg:
foreach($title->attributes() as $key => $val)
{
if ($key == 'address') $email[$count++] = $val;
}