PHP Foreach() with Arrays Within Arrays

Foreach for arrays inside of an array

You need to iterate the elements from the inside array, like this:

foreach($resultArray as $row => $innerArray){
foreach($innerArray as $innerRow => $value){
echo $value . "<br/>";
}
}

PHP loop through arrays inside array

First of all, $slides => array( is invalid syntax. I will assume you actually meant 'slides' => array( - if that's true, the following code will list all of the properties from each array using implode().

foreach($gallery as $key => $slides){
echo implode(', ', $slides['slides']) . "<br>";
}

If you want to do additional processing for each slide, loop through like this:

foreach($gallery as $key => $slides){
foreach($slides['slides'] as $sub_key => $slide) {
echo "<div>Key: $sub_key<br>Slide: $slide</div>";
}
}

PHP foreach() with arrays within arrays?

That's a perfect job for Iterators:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $key => $value) {
echo "$key => $value\n";
}

See Introduction to SPL Iterators and Live Demo on codepad

EDIT: the alternative would be array_walk_recursive as show in Finbarr's answer below

PHP foreach with Nested Array?

If you know the number of levels in nested arrays you can simply do nested loops. Like so:

//  Scan through outer loop
foreach ($tmpArray as $innerArray) {
// Check type
if (is_array($innerArray)){
// Scan through inner loop
foreach ($innerArray as $value) {
echo $value;
}
}else{
// one, two, three
echo $innerArray;
}
}

if you do not know the depth of array you need to use recursion. See example below:

//  Multi-dementional Source Array
$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(
7,
8,
array("four", 9, 10)
))
);

// Output array
displayArrayRecursively($tmpArray);

/**
* Recursive function to display members of array with indentation
*
* @param array $arr Array to process
* @param string $indent indentation string
*/
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $value) {
if (is_array($value)) {
//
displayArrayRecursively($value, $indent . '--');
} else {
// Output
echo "$indent $value \n";
}
}
}
}

The code below with display only nested array with values for your specific case (3rd level only)

$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(7, 8, 9))
);

// Scan through outer loop
foreach ($tmpArray as $inner) {

// Check type
if (is_array($inner)) {
// Scan through inner loop
foreach ($inner[1] as $value) {
echo "$value \n";
}
}
}

How to make array inside in foreach loop - php?

I suppose you're looking for this:

$input = array("teamA","teamB","teamC");
$data = [];
foreach($input as $value){
$assign = "50"; /* The data just temp */
$data[$value] = $assign;
}

echo $data["teamA"];

If $assign is same for all keys:

$data = array_fill_keys($input, 50);

Foreach loop inside array

That's invalid syntax. You'd have to build the "parent" portions of the array first. THEN add in the sub-array stuff with the foreach loop:

$foo = array(
'label' => 'Assign to user',
'desc' => 'Choose a user',
'id' => $prefix.'client',
'type' => 'radio',
'options' => array()
);

foreach ($clients as $user) {
$foo['options'][] = array (
'label' => $user->user_login,
'value' => $user->user_login,
);
}

How to use foreach inside array of foreach?

try this code:

public function dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
$options = [];
foreach($request['drop-option-label'] as $key => $data) {
$options[] = [
'label' => ($request['drop-option-label'][$key]) ?($request['drop-option-label'][$key]) : null,
'value' => ($request['drop-option-value'][$key]) ? ($request['drop-option-value'][$key]) : null,
];
}

$value[] = [
"type" => ($request['drop-type'][$key]) ? (($request['drop-type'][$key])) : null,
"label" => ($request['drop-label'][$key]) ? ($request['drop-label'][$key]) : null,
"name" => ($request['drop-name'][$key]) ? ($request['drop-name'][$key]) : null,
"placeholder" => ($request['drop-placeholder'][$key]) ? ($request['drop-placeholder'][$key]) : null,
"options" => $options
];
}
}
return $value;
}

Return all arrays inside foreach loop

You are currently actually just overwriting $array, you need to push the new data to it instead.

$array = array();
foreach ($stmt->fetchAll() as $value) {

//custom value
$customval = 1;

$array[] = [
"ID" => $value['ID'],
"name" => $value['name'],
"staus" => $value['status'],
"customval" => $customval,
];
}

return $myArray;


Related Topics



Leave a reply



Submit