Array
(
[result] => Array
(
[0] => stdClass Object
(
[uniq_id] => 00fdc23c151ad0044d60
)
[1] => stdClass Object
(
[uniq_id] => 590dde424e
)
[2] => stdClass Object
(
[uniq_id] => 6f0eb3bb34
)
[3] => stdClass Object
(
[uniq_id] => eeb6c63929
)
[4] => stdClass Object
(
[uniq_id] => a72034387e
)
}
}
But I want only multiple uniq_id one by one in foreach loop
like
00fdc23c151ad0044d60
590dde424e
6f0eb3bb34
eeb6c63929
a72034387e
Its a stdClass Object array, so try this:
foreach($yourArray['result'] as $data)
{
echo $date->uniq_id.'<br>';
}
Note: stdClass Object properties can be accessed using ->
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 6 years ago.
How to sort/arrange associative array by key,I have an array like this
Array
(
[0] => Array
(
[SF] => stdClass Object
(
['name']=>Miller
['age']=>25
['gender']=>M
)
[PF] => stdClass Object
(
['name']=>Dirk
['age']=>30
['gender']=>M
)
[SG] => stdClass Object
(
['name']=>Marcin
['age']=>24
['gender']=>M
)
[PG] => stdClass Object
(
['name']=>Julit
['age']=>23
['gender']=>F
)
[F] => stdClass Object
(
['name']=>Alex
['age']=>21
['gender']=>M
)
[G] => stdClass Object
(
['name']=>Andrew
['age']=>22
['gender']=>M
)
[C] => stdClass Object
(
['name']=>kevin
['age']=>26
['gender']=>M
)
[UTIL] => stdClass Object
(
['name']=>John
['age']=>24
['gender']=>M
)
)
)
I want to sort/arrange the above array like this PG,SG,SF,PF,C,G,F,UTIL
This is how my array should looks like after sort:
Array
(
[0] => Array
(
[PG] => stdClass Object
(
['name']=>Julit
['age']=>23
['gender']=>F
)
[SG] => stdClass Object
(
['name']=>Marcin
['age']=>24
['gender']=>M
)
[SF] => stdClass Object
(
['name']=>Miller
['age']=>25
['gender']=>M
)
[PF] => stdClass Object
(
['name']=>Dirk
['age']=>30
['gender']=>M
)
[C] => stdClass Object
(
['name']=>kevin
['age']=>26
['gender']=>M
)
[G] => stdClass Object
(
['name']=>Andrew
['age']=>22
['gender']=>M
)
[F] => stdClass Object
(
['name']=>Alex
['age']=>21
['gender']=>M
)
[UTIL] => stdClass Object
(
['name']=>John
['age']=>24
['gender']=>M
)
)
)
Any idea how can I achieve the same?
Being a custom order you can't use any built in PHP sort function. A simple way of doing it would be:
$sort = array('PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'UTIL');
$result = array();
foreach ($sort as $key) {
$result[0][$key] = $myArray[0][$key];
}
var_dump($result);
First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);
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];
}
}
?>
This question already has answers here:
stdClass object and foreach loops
(5 answers)
Closed 4 months ago.
I have an object like this:
stdClass Object
(
[_count] => 10
[_start] => 0
[_total] => 37
[values] => Array
(
[0] => stdClass Object
(
[_key] => 50180
[group] => stdClass Object
(
[id] => 50180
[name] => CriticalChain
)
)
[1] => stdClass Object
(
[_key] => 2357895
[group] => stdClass Object
(
[id] => 2357895
[name] => Data Modeling
)
)
[2] => stdClass Object
(
[_key] => 1992105
[group] => stdClass Object
(
[id] => 1992105
[name] => SQL Server Users in Israel
)
)
[3] => stdClass Object
(
[_key] => 37988
[group] => stdClass Object
(
[id] => 37988
[name] => CDO/CIO/CTO Leadership Council
)
)
[4] => stdClass Object
(
[_key] => 4024801
[group] => stdClass Object
(
[id] => 4024801
[name] => BiT-HR, BI & IT Placement Agency
)
)
[5] => stdClass Object
(
[_key] => 37845
[group] => stdClass Object
(
[id] => 37845
[name] => Israel Technology Group
)
)
[6] => stdClass Object
(
[_key] => 51464
[group] => stdClass Object
(
[id] => 51464
[name] => Israel DBA's
)
)
[7] => stdClass Object
(
[_key] => 66097
[group] => stdClass Object
(
[id] => 66097
[name] => SQLDBA
)
)
[8] => stdClass Object
(
[_key] => 4462353
[group] => stdClass Object
(
[id] => 4462353
[name] => Israel High-Tech Group
)
)
[9] => stdClass Object
(
[_key] => 4203807
[group] => stdClass Object
(
[id] => 4203807
[name] => Microsoft Team Foundation Server
)
)
)
)
I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.
For example I tried this:
foreach ($res as $values) { print "\n"; print_r ($values); }
It iterates trough the object, but it also gives me useless
10 0 37
echo "<table>"
foreach ($object->values as $arr) {
foreach ($arr as $obj) {
$id = $obj->group->id;
$name = $obj->group->name;
$html = "<tr>";
$html .= "<td>Name : $name</td>";
$html .= "<td>Id : $id</td>";
$html .= "</tr>";
}
}
echo "</table>";
Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:
You can iterate overa stdclass simply by using foreach:
$user = new \stdClass();
$user->flag = 'red';
foreach ($user as $key => $value) {
// $key is `flag`
// $value is `red`
}
function objectToArray( $data )
{
if ( is_object( $data ) )
$d = get_object_vars( $data );
}
Convert the Object to array first like:
$results = objectToArray( $results );
and use
foreach( $results as result ){... ...}
I know it's an old post , but for sake of others:
when working with stdClass you should use Reflections:
$obj = new ReflectionObject($object);
$propeties = $obj->getProperties();
foreach($properties as $property) {
$name = $property->getName(); <-- this is the reflection class
$value = $object->$name; <--- $object is your original $object
here you need to handle the result (store in array etc)
}
foreach($res->values as $value) {
print_r($value);
}
i have array in this way
Array
(
[0] => stdClass Object
(
[qa_verified] => 0
)
[1] => stdClass Object
(
[qa_verified] => 1
)
[2] => stdClass Object
(
[qa_verified] => 2
)
)
i need to change into
Array
(
[0] => stdClass Object
(
[qa_verified] => invalidate
)
[1] => stdClass Object
(
[qa_verified] => approve
)
[2] => stdClass Object
(
[qa_verified] => reject
)
)
i have to change the value of qa_verified key depending on the status
0 = invalidate, 1= approve, 2=reject
i tried on array_walk, but unable to get result
any one help me on this
$lookup = array('invalidate', 'approve', 'reject');
array_walk(
$myArray,
function(&$entry) use ($lookup) {
$entry->qa_verified = $lookup[$entry->qa_verified];
}
);
var_dump($myArray);