How can I loop through this array in php? - php

I'm trying to loop through this multidimensional array however it doesn't seem to be working correctly.
Array
(
[*backingData] => Array
(
[data] => Array
(
[0] => stdClass Object
(
[name] => bob
[id] => 1
)
[1] => stdClass Object
(
[name] => bob
[id] => 2
)
[2] => stdClass Object
(
[name] => bob
[id] => 3
)
)
)
)
This is what I have right now:
foreach ($array as $key => $value) {
for ($i=0;$i<=count($value);$i++) {
echo $value[$i]['id'];
}
}

Here a way to do that :
function recursiveArrayLoop ($array) {
foreach ($array as $a) {
if (is_array ($a)) {
recursiveArrayLoop ($a);
} else {
var_dump ($a);
}
}
}

you have to convert your object data to array. use get_object_vars().
foreach ($array as $key=>$value) {
foreach ($value as $key2=>$data) {
$data = get_object_vars($data);
echo $data['id'];
}
}

Related

How can I retrieve data from array

Am a beginner in yii2. In my program there is an array named Place. It contains the values like:
Array
(
[0] => Array
(
[0] => Aluva
[1] => Paravoor
)
[1] => Array
(
[0] => Ernakulam
[1] => Paravoor
)
[2] => Array
(
[0] => Aluva
[1] => Ernakulam
)
[3] => Array
(
[0] => Kottuvally
[1] => Paravoor
)
)
How can I retrieve each element from this array?
You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});
using foreach ?
foreach($array as $row){
foreach($row as $key => $val){
echo $val.'<br>';
}
}
Sample code -
<?php
$data = array(
0 => array(0=>'Aluva', 1=>'Paravoor'),
1 => array(0=>'Ernakulam', 1=>'Paravoor'),
2 => array(0=>'Aluva', 1=>'Ernakulam'),
3 => array(0=>'Kottuvally', 1=>'Paravoor')
);
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value1) {
echo '<br>'.$value1;
}
}
$var = $Place[0][1];
Where is $var is now "Paravoor"

How can I use foreach to process a multidimensional php array?

I parsed a JSON file with json_decode, and the result is a long multidimensional array like the following:
Array
(
[Basic] => Array
(
[0] => Array
(
[text] => Taunt.
[playerClass] => Shaman
[locale] => enUS
[mechanics] => Array
(
[0] => Array
(
[name] => Taunt
)
)
)
)
[Classic] => Array
(
[0] => Array
(
[cardId] => CS2_188o
[name] => 'Inspired'
[mechanics] => Array
(
[0] => Array
(
[name] => OneTurnEffect
)
)
)
)
)
I want to use a foreach to insert the data into a datatable but I can't make it work with this multidimensional array. How would I do that?
You must use recursive array to do it like this
function build($fullArray)
{
foreach ($fullArray as $item) {
if (is_array($item)){
build($item);
}
else{
echo $item["cardId"];
echo $item["name"];
....
}
}
}
Use this:
foreach ($items as $item) {
if (is_array($item)){
foreach ($item as $it) {
echo $it["name"];
}
}
echo $item["cardId"];
echo $item["name"];
....
}

Returning values from a multidimensional array php

I'm hoping there is a better way of returning the values from each of cy_GB['value] and en_GB['value] from the array below:
MultilingualSelectAttributeTypeOptionList Object (
[options:MultilingualSelectAttributeTypeOptionList:private] => Array
(
[0] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 7
[values] => Array
(
[cy_GB] => Array
(
[id] => 13
[value] => Audio described
)
[en_GB] => Array
(
[id] => 14
[value] => Audio described
)
)
[th] => TextHelper Object
(
)
)
[1] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 3
[values] => Array
(
[cy_GB] => Array
(
[id] => 5
[value] => BSL signed
)
[en_GB] => Array
(
[id] => 6
[value] => BSL signed
)
)
[th] => TextHelper Object
(
)
)
)
[error] =>
)
This is what I've tried. I should also use more meaningful names.:
foreach ($selectedOptions as $row) {
foreach ($row as $key) {
foreach ($key as $k => $v) {
if($k == 'cy_GB') {
echo $v['value'];
}
if($k == 'en_GB') {
echo $v['value'];
}
}
}
}
I know this kind of thing has been asked many times before so I apologise for that. any help would be most appreciated.
Something like this could work:
function findKeyRec($obj, $search) {
if( !is_array( $obj ) && !$obj instanceof Traversable ) return;
foreach($obj as $key => $value) {
if($key == $search) {
echo $value['value'];
} else {
findKeyRec($value, $search);
}
}
}
findKeyRec($ar, 'cy_GB');
findKeyRec($ar, 'en_GB');
It's not shorter, but in my opinion more elegant, and it should work with any object/array structure.
Untested.

Parsing an array in PHP after serializeArray() in JS

I receive this array in my PHP page after form serializeArray() done on a form in Javascript:
Array
(
[datas] => Array
(
[0] => Array
(
[name] => room_1
[value] => a
)
[1] => Array
(
[name] => room_2
[value] => b
)
[2] => Array
(
[name] => room_3
[value] => c
)
)
)
How can I parse it after during a foreach loop ?
Thanks.
I tried:
foreach ($datas as $key => $item) {
echo $item;
}
foreach ($array['datas'] as $item) {
echo $item['name'];
echo $item['value'];
}
If the name of the array is $array,
$arr = $array['datas'];
foreach($arr as $key => $val){
$name = $val['name'];
$value = $val['value'];
}
}

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

Categories