How do I loop through the array to get the "converted_amount" values?
stdClass Object
(
[rows] => Array
(
[0] => stdClass Object
(
[components] => Array
(
[0] => stdClass Object
(
[amount] => 5033298.132349431
[count] => 1337
[rate] => 3.1398800
[converted_amount] => 1603021.9952863243
)
[1] => stdClass Object
(
[amount] => 458673.0026585825
[count] => 325
[rate] => 0.45260800
[converted_amount] => 1013400.4157520011
)
I have tried a foreach like this but it doesn't work. I think there should be something in-between components and converted_amount - maybe another foreach? I'm not sure.
foreach ($getexvolume as $vol) {
echo $vol['rows'][0]['components']['converted_amount'];}
You have an object instead if array. You must work with data as an object...
foreach ($getexvolume->rows as $row) {
foreach ($row->components as $component) {
echo $component->converted_amount;
}
}
echo $vol->rows[0]->components[0]->converted_amount;
You are mixing array and object. Your output is an object so you have to access it like one otherwise if you want to treat it like an array you have to convert it to an array. As for now you can use the above code.
A better solution which i think fits your problem is that you loop around your nested array like:
foreach($vol->rows[0]->components as $data){
echo $data->converted_amount;
}
Try this:
foreach ($getexvolume->rows[0]->components as $vol) {
echo $vol->converted_amount;
}
The object you have is a mix of Arrays and Objects.
Arrays can be addressed as $array['value'] but Objects must be addressed as $object->value.
echo $vol->rows[0]->components[0]->converted_amount;
However since you have multiple components, you will need a nested loop:
foreach ($getexvolume as $vol)
{
foreach($vol->rows as $row)
{
foreach($row->component as $component)
{
echo $component->converted_amount;
}
}
}
(pseudocode - not tested).
Ideally the variable would be normalised as a multidimensional array or nested object first so you don't have to worry about syntax.
Related
I'm getting data from database and putting it in a variable $data. If I'll print_r($data), I'll get something like that:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Bob
)
[1] => stdClass Object
(
[id] => 2
[name] => Mike
)
)
It has more [key] => value in each of them, and obviously it doesn't end on [1].
I'm using foreach() like that:
foreach ($data as $item) {
foreach ($item as $key => $value) {
//code
}
}
I have two question regards all the above:
1) Is there less complex technique to get the $key and $value?
2) If, for example, I want to output only the [name], how would I access it?
The $key[placeholder] while placeholder is a number is outputting the letter number value of the $key.
You have an array with object, so if you want only the name:
$name_list = array();
foreach ($data as $obj) {
$name_list[] = $obj->name;
}
This way you will have an array with only the name of each object.
So to summarize :
You loop through an array of obj, so to access the properties of each object just do :
$obj->/* the properties */;
I've been hours trying to figure this, i know i'm missing something obvious. This is the problem:
I have an array that some of its elements are objects, others are arrays and others are of other types. What i need is:
Loop through the array and convert the object elements into array elements.
Loop recursively (or whatever you want to call it) through these new array elements (the ones that were converted from objects) and the elements that already are array and perform the previous task (that is: convert the object elements into array elements).
Here's the gotcha: every time an object element is converted into array, the class name of the object has to be added as the first element of the array that is generated by converting the object.
Here is a simplified example:
Array:
Array
(
[0] => PhpParser\Node\Expr\Assign Object
(
[var] => PhpParser\Node\Expr\Variable Object
(
[name] => bar
)
[expr] => PhpParser\Node\Scalar\LNumber Object
(
[value] => 22
)
)
)
I need a function like this:
//$arr is the array previously posted
$arr = cool_object_to_array($arr);
var_dump($arr );
outputs
Array
(
[0] => Array
(
[0] => PhpParser\Node\Expr\Assign
[var] => Array
(
[0] => PhpParser\Node\Expr\Variable
[name] => bar
)
[expr] =>Array
(
[0] => PhpParser\Node\Scalar\LNumber
[value] => 22
)
)
)
The nesting level is unknown. It can be many arrays nested on objects nested on other objects or arrays, etc. The example is just very simplified. I need a solution that handles that too.
Thanks in advance for all your answers!
This should do it:
function cool_object_to_array($array) {
foreach ($array as $key => &$value) {
if (is_object($value)) {
$type = get_class($value);
$value = (array) $value;
array_unshift($value, $type);
}
if (is_array($value)) {
$value = cool_object_to_array($value);
}
}
return $array;
}
I have a PHP standard class object converted from json_decode of a REST call on an API which looks like :
Array
(
[1437688713] => stdClass Object
(
[handle] => Keep it logically awesome.
[id] => 377748
[ping] => stdClass Object
(
[url] => https://api.me.com
[id] => 377748
[name] => web
[active] => 1
[events] => Array
(
[0] => data_new
[1] => data_old
)
So far i had no issues in parsing any of the PHP objects. However this one is failing because i can not access the nested object elements using a key since 1437688713 is not assigned to a key and accessing an object is failing if i try to do this:
$object->1437688713->handle
Is there a way to access these elements ?
Update: one more thing, i would never know this value (1437688713) in advance. Just like a key. All i get is a stdclass object which i have to parse.
The outer part of your data is an array, not an object. Try:
$array['1437688713']->handle;
or if you don't know the key, you can iterate over the array (handy if it may contain multiple objects too):
foreach ($array as $key => $object) {
echo $key; // outputs: 1437688713
echo $object->handle; // outputs: Keep it logically awesome.
}
Get the first item from $object array
$first_key = key($object);
Use it with your response array,
$object[$first_key]->handle;
Or, the first element of array
$first_pair = reset($object)->handle;
I am trying to extract an array embedded in another array using the standard foreach loop, the problem is it keep returning unwanted data.
Array
Array
(
[id] => 2035443879
[status] => Unshipped
[sku] => 0340024275-UsedGood
[isbn] => 0340024275
[condition] => Used
[number_of_items] => 1
[title] => Linnets and Valerians (Knight Books)
[purchase_date] => 1361536149
[0] => Array
(
[status] => Shipped
[title] => Linnets and Valerians (Knight Books)
[date] => 1361491200
)
)
Print function
function mapStatus($orders){
foreach($orders as $order){
echo "<pre>";
print_r($order);
echo "</pre>";
foreach(array_unique($order) as $item){
echo "-".$item["status"]."-";
}
}
}
Outcome
-2--U--0--0--U--1--W--L--H--1--Shipped-
As you can see from my outcome what was printed is not exactly what I expected, it seems that I am printing the first character of every index in the array and not just the actual array I want.
I'm aware that I can use a is_array() function to determine if what I am printing is coming from an array object but I would like to know if there is proper way to do what I want?
for ($i = 0; $i < $order['number_of_items']; $i++) {
echo "-".$order[$i]["status"]."-";
}
However, I recommend a different data structure. Instead of having the items as indexed elements within the order array, you should have $order['items'], which points to an array. Then you can use:
foreach ($order['items'] as $item)
Then you don't need $order['number_of_items'], you can just use count($order['items']).
I can't for the life of me work out a nice way of getting an array of objects like this (would actually be a much larger array from a db):
Array
(
[0] => stdClass Object
(
[name] => Canterbury
)
[1] => stdClass Object
(
[name] => West Coast
)
)
Into a single level array like this:
Array
(
[0] => Canterbury
[1] => West Coast
)
So that when I do a json_encode, it looks like this:
{"0":"Canterbury","1":"West Coast"}
Rather than this:
[{"name":"Canterbury"},{"name":"West Coast"}]
I've tried a variety of things, like using php's array combine and merge functions, but no luck.
Pretty simple:
$output = array();
foreach($objectArray as $oneObject)
$output[] = $oneObject->name;
echo json_encode($output);
Well, I think you just need to put the object's name attribute in place of the array's value:
foreach($array as $key => $value){
$value = $value->name
}