Iterate through stdClass - php

How on Earth do I get the ids, and names from this array (possibly by using a ForEach construct)?
stdClass Object (
[data] => Array (
[0] => stdClass Object ( [name] => Corinne Culbard [id] => 5005561918 )
[1] => stdClass Object ( [name] => Tom West [id] => 5034297044 )
[2] => stdClass Object ( [name] => Sabri Tasci [id] => 5057373639 )

Is this not working?
foreach ($object->data as $item) {
$name = $item->name;
$id = $item->id;
}

Related

How to use a variable string with array in a foreach loop

I try to get the following code working.
foreach ($uniqueItems as $key => $value) {
$output = "{$value->properties->property[10]->value}";
echo $output;
}
In the browser I see 24.99
Above code gives me the correct output. But I need the $output from outside this foreach. I try the following
$output = "{\$value->properties->property[10]->value}";
foreach ($uniqueItems as $key => $value) {
echo $output;
}
In the browser I see {$value->properties->property[10]->value}
This is print_r($uniqueItems[$key]);
SimpleXMLElement Object
(
[name] => Donnay joggingbroek zwart unisex
[properties] => SimpleXMLElement Object
(
[property] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryTime
)
[value] => Voor 16.00 uur besteld, morgen in huis!
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => brand
)
[value] => Donnay
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => size
)
[value] => SimpleXMLElement Object
(
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => color
)
[value] => Zwart
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => EAN
)
[value] => SimpleXMLElement Object
(
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => categoryPath
)
[value] => Tenniskleding/Tenniskleding dames
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => deliveryCosts
)
[value] => 4.95
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => discount
)
[value] => 5.00
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => subcategories
)
[value] => Tenniskleding dames
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SKU
)
[value] => 489000-TL-020
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => fromPrice
)
[value] => 24.99
)
))
How to get the right output?
The second snippet won't work as php does not know that $value is to be interpolated until $value gets defined which is happening when the loop actually starts. Till then, it will just be a string. That's why it's just printing out it as a string

Weird save to array in foreach in php

Something strange happens during saving into an array inside a foreach.
I'm getting unexpected results so I make this part of code for debuging.
$search_case = new stdClass();
$same_case = array ();
print_r($families);
foreach ($families as $key => $family) {
echo "<br/><br/>";
$same_case['aaaaa'.$key] = $this->search_benef->search_case($family, $search_case);
print_r($same_case);
}
The problem is that when I get function's results for $key = 1 it also change the data in table for $key = 0. To chek that I created aaaaa0 and aaaaa1 names for my table.
Bellow is print_r results:
First print_r I have 2 objects, IBRAHIM and MOHAMMAD
stdClass Object ( [0] => stdClass Object ( [focal] => stdClass Object ( [name] => RENAS [surname] => IBRAHIM [case_number] => 36343 ) [case] => 36343 ) [1] => stdClass Object ( [focal] => stdClass Object ( [name] => AZAD [surname] => MOHAMMAD [case_number] => 54944 ) [case] => 54944 ) )
Second print_r inside foreach loop, in the first loop I get results for IBRAHIM and in the second loop Ι get results for MOHAMMAD, but it removes results for IBRAHIM in aaaaa0.
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 26 [name] => RENAS [surname] => IBRAHIM ) ) ) ) )
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) [aaaaa1] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) )
Can you explain why this happens? Which is the error?
-- EDIT --
I change my code and I remove 'search_case' function, but I face the same problem. The content changes. Below I paste my new code without the function.
$aaaaa = array ();
foreach ($families as $key => $family) {
echo "<br/><br/>Family->Case Number: " . $family->case;
$db_lists = array('athens' => 'athensDB', 'hotel' => 'hotelDB');
foreach ($db_lists as $key => $db_list) {
$remoteDB = $this->ci->load->database($key, TRUE);
$query = 'SELECT tb.id as id, name, surname FROM beneficiaries tb
left join _map_family_status tmfs ON tmfs.id=tb.family_status
where asylum_case = ' . $family->case . ' AND family_status = 1 GROUP BY asylum_case';
$focal = $remoteDB->query($query)->row();
if ($focal) {
$search_case->old->$key->focal = $focal;
}
}
echo '<br/> '.$family->case.' <br/> ';
$aaaaa[$family->case] = $search_case;
echo "<pre>";
print_r($aaaaa);
echo "</pre>";
}
return $aaaaa;
But the results are dissapointing. Array[36343] content changes with the second loop's content. Below I paste "print_r" results for the two loops.
Family->Case Number: 36343
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 26
[name] => RENAS
[surname] => IBRAHIM
)
)
)
)
)
Family->Case Number: 54944
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
[54944] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
)

How can I merge this array or remove a key

Below is an example of the array I have compiled so far. I am generating the array from a facebook graph api call and want to remove the Array wrapping each object so I just have one list under the data Array. Preferably I need a dynamic solution as their could be more than one [0] => Array, [1] => Array and so on.... in each API request.
stdClass Object
(
[data] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
)
I would like the array to look like this ultimately. What is the best approach here?
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
[1] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
Assuming your data is stored in a variable $data, you can do this:
foreach($data->data as &$el) {
$el = $el[0];
}
Now the wrapping arrays have been removed.
See it run on eval.in
You can use this:
if ($data && $data->data){ $new = array_values($data->data); }

Access array returned from xpath in PHP

How can I access (in order to display) the elements returned by xpath?
My xpath call is
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
and what I got is
Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Tomato and Cheese ) [type] =>
Regular [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Small ) [price] => 5.50 ) [1] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Large ) [price] => 9.75 ) ) ) ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Pepperoni ) [type] => Regular [available] => true [size] => Array ( [0]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small ) [price] => 6.85 ) [1]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large ) [price] => 10.85 ) ) )
) [2] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Meatball ) [type] => Regular
[available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Small ) [price] => 6.85 ) [1] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Large ) [price] => 10.85 ) ) ) ) [3] => SimpleXMLElement Object ( [#attributes] =>
Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes] => Array ( [name]
=> Hawaiian ) [type] => Regular [available] => true [size] => Array ( [0] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Small ) [price] => 7.95 ) [1] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Large ) [price] => 11.80 ) ) ) ) [4] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Three Aces Special ) [type] =>
Speciality [available] => true [size] => Array ( [0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Small ) [price] => 9.80 ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Large ) [price] => 15.80 ) ) ) ) [5] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Mediterranean ) [type] => Speciality [available] => true
[size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small )
[price] => 9.80 ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large )
[price] => 15.80 ) ) ) ) )
What I want to do is to display this information in a sort of a list, so I'm wondering how can I access the elements of SimpleXMLElement Object.
Thanks
Cleaning up output:
[0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza )
[item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Tomato and Cheese )
[type] => Regular
[available] => true
[size] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Small )
[price] => 5.50 )
[1] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Large )
[price] => 9.75 )
)
)
)
Something like this will work, though a lot of your data is wrapped up as attributes, which will make it a little more verbose to get to, like the sizes attribute. You may need to cast items in order to get the value.
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
foreach ($products as $product) {
// accessing SimpleXML values is tricky
// $name is a SimpleXMLElement Object
$name = $product['title'];
// $name2 has the string value of the title attribute
$name2 = (string) $product['title'];
// following works because __toString is implemented
// by SimpleXMLElement class
echo "Title: " . $product['title'];
// alternative syntax, unless you cast to a
// string, you have a SimpleXML Object
$name3 = $product->attributes()->title;
}
See Access #attributes data in SimpleXMLElement in PHP

How can I parse this result?

Array (
[0] => stdClass Object (
[name] => query1
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 226 ) ) )
[1] => stdClass Object (
[name] => query2
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 247 ) ) )
[2] => stdClass Object (
[name] => query3
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 580 ) ) )
[3] => stdClass Object (
[name] => query4
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 10 ) ) )
[4] => stdClass Object (
[name] => query5
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 508 ) ) )
)
How I can parse this result ?
I searched, but I didn't found out how I could do it.
Assuming your array variable is called $data:
foreach ($data as $object) {
$name = $object->name;
$result_set = $object->fql_result_set;
$fan_count = $result_set[0]->fan_count;
// Do what you need to with the data here
}
Hope that helps

Categories