Loop Multidimensional PHP arrays - php

Hello I am having hard time looping through the PHP multidimensional array, I want to know the best possible way of looping an array. This is the current array that I am trying to loop through.
Array
(
[bathroom] => Array
(
[name] => Bathroom
[things] => Array
(
[0] => Array
(
[name] => Cheval Mirrow
[cubic] => .14
[quantity] => 1
)
[1] => Array
(
[name] => Carton/Wine
[cubic] => .07
[quantity] => 1
)
[2] => Array
(
[name] => Carton/picture
[cubic] => .07
[quantity] => 1
)
)
)
)
I have tried this code
$keys = array_keys($array);
for($i = 0; $i < count($array); $i++) {
echo $keys[$i] . "<br>";
foreach($array[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
foreach($array[$value[$i]] as $key1 => $value1){
echo $key1.":". $value1."<br>";
}
}
echo "<br>";
}
I am able to get the first value now the issues is that I am not able to get the values of things array, I am getting error on this, can someone tell me where I am getting wrong on this.

Here's an example of how you can process your array:
foreach ($array as $key => $value) {
echo "$key:<br>\n";
echo " name: {$value['name']}<br>\n";
foreach ($value['things'] as $t => $thing) {
echo "\tthing $t:<br>\n";
foreach ($thing as $name => $val) {
echo "\t $name: $val<br>\n";
}
}
}
Output:
bathroom:<br>
name: Bathroom<br>
thing 0:<br>
name: ChevalMirrow<br>
cubic: 0.14<br>
quantity: 1<br>
thing 1:<br>
name: Carton/Wine<br>
cubic: 0.07<br>
quantity: 1<br>
thing 2:<br>
name: Carton/picture<br>
cubic: 0.07<br>
quantity: 1<br>
Demo on 3v4l.org

foreach ($orginalarray as $key1 => $value1){
foreach ($value1 as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
foreach ($value3 as $key3 => $value3) {
}
}
}
}

Related

Nested 'foreach' for multidimensional array

<?php
$p_23 = array("Name"=>"XYZ","Age"=>"12");
$a_23 = array("Class"=>"5","Sec"=>"A");
$r_23 = array("Personal"=>$p_23,"Academic"=>$a_23);
$p_24 = array("Name"=>"ABC","Age"=>"14");
$a_24 = array("Class"=>"6","Sec"=>"B");
$r_24 = array("Personal"=>$p_24,"Academic"=>$a_24);
$stud = array("23"=>$r_23,"24"=>$r_24);
foreach ($stud as $key => $value) {
echo $value;
}
?>
Using echo is giving error,
previous issue resolved, expanding my question now.
now i want this multi dimensional array to print like below using html tags
#Roll 23#
##Academic##
-Class=>5
-sec=>B
Personal
-Name=>YXZ
-Age=>12
#Roll 24#
##Academic##
-Class=>6
-sec=>A
Personal
-Name=>ABC
-Age=>12
--Nested foreach part with HTML tags--
foreach ($stud as $key => $value) {
echo "<h1>Roll $key</h1>";
echo "<ol>";
foreach ($r_23 as $key => $value) {
echo "<h2>$key</h2>";
echo "<ul>";
foreach ($p_23 as $key => $value){
echo "<li>$key => $value</li>";}
echo "</ul>";}
echo "</ol>";
but it is showing the same value for both academic and personal keys, which i dont exactly want. Thank u!!
$value is an array, echo will only print strings, you need to either JSON encode your $value and echo it or use var_dump. If your intended output was more complex than this then you would need to expand on your question.
Well, its a multi-dimensional array, so you need more foreach()
Here is the updated code:
<?php
$p_23 = array("Name"=>"XYZ","Age"=>"12");
$a_23 = array("Class"=>"5","Sec"=>"A");
$r_23 = array("Personal"=>$p_23,"Academic"=>$a_23);
$p_24 = array("Name"=>"ABC","Age"=>"14");
$a_24 = array("Class"=>"6","Sec"=>"B");
$r_24 = array("Personal"=>$p_24,"Academic"=>$a_24);
$stud = array("23"=>$r_23,"24"=>$r_24);
foreach ($stud as $key => $value) {
foreach($value as $k => $v){
foreach($v as $kk => $vv) {
echo $vv;
}
}
}
?>
And this is your multi-dimensional array:
Array
(
[23] => Array
(
[Personal] => Array
(
[Name] => XYZ
[Age] => 12
)
[Academic] => Array
(
[Class] => 5
[Sec] => A
)
)
[24] => Array
(
[Personal] => Array
(
[Name] => ABC
[Age] => 14
)
[Academic] => Array
(
[Class] => 6
[Sec] => B
)
)
)

Loop through php multi level associative array

I am struggling to return values from all layers of a multidimensional array, e.g. value then all child elements. I've tried lots of loops but can't quite get it right. Please can someone help - I've been stuck for ages! Thank you.
The array is:
Array
(
[SiteRep] => Array
( [DV] => Array
(
[Location] => Array
(
[Period] => Array
(
[0] => Array
(
[value] => 2016-12-19Z
[Rep] => Array
(
[0] => Array
(
[D] => W
[F] => 1
[G] => 9
)
[1] => Array
(
[D] => W
[F] => 0
[G] => 7
)
)
)
[1] => Array
(
[value] => 2016-12-20Z
[Rep] => Array
(
[0] => Array
(
[D] => ENE
[F] => 4
[G] => 7
)
[1] => Array
(
[D] => E
[F] => 3
[G] => 9
)
so far my code is:
$i=0;
foreach ($json_decoded['SiteRep']['DV']['Location']['Period'][$i] as $key => $value) {
if (is_array($value)){
foreach ($value as $key2 => $value2){
if (is_array($value2)){
foreach ($value2 as $key3 => $value3) {
echo $key3 . " 3: " . $value3 . "<br>";
}
} else {
echo $key2 . " 2: " . $value2 . "<br>";
}
};
} else {
echo $key . " 1: " . $value . "<br>";
}
$i++;
};
I believe you are looking for recursion. A function that calls itself. It can be tricky but it is by far the most efficient way to navigate an array of indeterminate depth.
function printStuff($stuff)
{
echo $stuff['value']
if (isset($stuff['rep']))
{
printStuff($stuff['rep']);
}
}
If you want the values to pass down simply change echo to a return value:
function printStuff($stuff)
{
$temp = $stuff['value']
if (isset($stuff['rep']))
{
$temp .= printStuff($stuff['rep']);
}
return $temp;
}
Note: recursion can cause out of memory if not set up correctly similar to an infinite loop, as the recursive function call pushes the function call onto the call stack every time. Just make sure your function call has a subset of the parameter passed in.
If your array has the constantly number of layers, you may use foreach-function for the each layer. If your array has different number of layers every time - you should use the recursion, because it's the only solution in your situation.
Something like this:
array_walk_recursive($array, function($item, $key){
//your actions
});
I've continued drilling down and managed to get all the data I need by using...
foreach ($json_decoded['SiteRep']['DV']['Location']['Period'] as $key => $value) {
if (is_array($value)){
foreach ($value as $key2 => $value2){
if (is_array($value2)){
foreach ($value2 as $key3 => $value3) {
foreach ($value3 as $key4 => $value4) {
echo $key4 . ": " . $value4 . "<br>";
}
}
} else {
echo $key2 . ": " . $value2 . "<br>";
}
};
} else {
echo $key . ": " . $value . "<br>";
}
$i++;
};
This can map the array recursivly. Preserving they keys and structure.
You can however only return a new value and not change the key. But you will know which key you are looking at and if you just want to echo it you can do that to.
array_map_assoc_recursive('callback', $array)
function callback($key, $value) {
return "new" . $value;
}
function array_map_assoc_recursive($f, array $a) {
return array_column(array_map(function ($key, $value) use ($f) {
if (is_array($value)) {
return [$key, array_map_assoc_recursive($f, $value)];
} else {
return [$key, call_user_func($f, $key, $value)];
}
}, array_keys($a), $a), 1, 0);
}

how to get values from php multidimensional array?

How can i get value of number and month from this array.
Array
(
[mane] => Riya
[id] => 70
[order] => Array
(
[details] => Array
(
[number] => 4112
[month] => March
)
)
)
Here is code which i had tried
foreach($order as $row) {
echo $row['details']['number'];
echo $row['details']['month'];
}
Here is a simple solution:
foreach ($order['order'] as $key => $value)
{
echo $value['number']."<BR />";
echo $value['month']."<BR />";
}
Here is a working DEMO for you
You are missing order array.
Change from
echo $row['details']['number'];
echo $row['details']['month'];
Into
echo $row['order']['details']['number'];
echo $row['order']['details']['month'];
// ^ error was here.
You can do like this
foreach($order as $key => $value){
if( is_array($value) and !empty($value) ){
foreach($value as $k => $v){
printData($v['number']);
printData($v['month']);
}
}else{
printData($value);
}
}

Print out JSON with multiple arrays

I have this JSON string that has been converted into a PHP array:
Array (
[textfield] => Array (
[elements] => Array (
[0] => Array (
[type] => textField
)
)
[title] => textfield
)
[textarea] => Array (
[elements] => Array (
[0] => Array (
[type] => textArea
)
)
[title] => textarea
)
) textfield
And I'm trying to loop through it and print out the type and title of each array. This is what I have so far:
foreach($inputs as $key => $jsons) {
foreach($jsons as $key => $value) {
echo $value;
}
}
But that only prints out the title. Note, I do actually need to loop through the array and get all the values because I need to use them, I know I could use print_r to just dump the array but that's not what I need!
Here's an easy way... but without seeing more of what you're trying to do, who knows if this will even work.
foreach($json as $key => $value) {
$elements = $value['elements'];
foreach($elements as $key => $element) {
echo "$key = {$element['type']}\n";
}
$title = $value['title'];
echo "$key = $title\n";
}
foreach($inputs as $key => $jsons) {
foreach($jsons as $key1 => $value) {
if( $key1 == "title" ) {
echo "TITLE :-".$value;
} else if( is_array($value) {
foreach($value as $key2 => $value2) {
echo "Type :".$value;
}
}
}
}

How to loop 3 dimension array using foreach PHP

Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv
foreach ($stories as $key => $story){
//echo "<br />";
foreach($story as $subkey => $subvalue){
echo $subvalue."<br />";
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
}
}
Array
(
[270] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 270
[story_name] => Story C
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381418153311.jpg
[1] => story_photos/2012/0322/361332393792911587.jpg
)
[photo_added_date] => Array
(
[0] => 1332381418
[1] => 1332393792
)
)
[269] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 269
[story_name] => Story B
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381406580761.jpg
)
[photo_added_date] => Array
(
[0] => 1332381406
)
)
[268] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 268
[story_name] => Story A
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381393552719.jpg
)
[photo_added_date] => Array
(
[0] => 1332381393
)
)
)
Why not try this :
foreach ($stories as $key => $story){
if(is_array($story)){
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
} else {
echo $subvalue."<br />";
}
}
} else {
echo $story."<br />";
}
}
Also, I am not sure because your question isn't really clear or specified.
Or
function echoArray( $array )
{
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell);
}
else
{
echo "$cell<br />";
}
}
}
It works for N dimensionnal array
An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
{
$tag=$array[$arraytagkey];
}
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
}
else
{
$matches = array();
if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
{
if ( 0 != strcasecmp('input',$tag) )
{
echo "<input class='$cssclassprefix$depth' value='$cell' />";
}
else
{
echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
}
}
else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
{
echo "$cell<$tag />";
}
else
{
echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
}
}
}
}
This doesn't really answer your question, but note, your third loop is this:
foreach ($subvalue as $key => $subsubvalue){
But you've already used $key in the first loop:
foreach ($stories as $key => $story){
You should change the third to:
foreach ($subvalue as $subsubkey => $subsubvalue){
Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:
foreach($stories as $key => $story)
{
// here you can echo the $store['user_email'] etc
echo 'Email: ' . $story['user_email'] . '<br />';
// loop over the photo urls
foreach($story['photo_url'] as $photo_url)
{
echo 'Photo URL: ' . $photo_url . '<br />';
}
// loop over the photo added dates
foreach($story['photo_added_date'] as $photo_added_date)
{
echo 'Photo Date: ' . $photo_added_date . '<br />';
}
}
If you're wanting to recursively search the array then the other answers are what you want.

Categories