Nested 'foreach' for multidimensional array - php

<?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
)
)
)

Related

How can i return all the results from my array?

Array
(
[edit] => true
[id] => 1
[type] => Array
(
[0] => LC
)
[userid] => 1
[norooms] => 1
[park] => Central
[start] => 09:00
[end] => 11:00
[length] => 2
[student] => 79
[status] => Rejected
)
<?php
$posted_data = array();
if (!empty($_POST['edit'])) {
$posted_data = json_decode($_POST['editVal'], true);
}
print_r ($posted_data);
foreach ($posted_data as $key => $value) {
echo '<p>'.$key.'</p>';
echo '<p>'.$value.'</p>';
}
?>
The array at the top is the jason_decode returned. However with my foreach function it does not display the first index of the array within the array. ie. ( [0] => LC ).
Where am I going wrong?
You need to build a recursive function, something like:
function print_recursively(array $array)
{
foreach ($array as $key => $value)
{
if(is_array($value))
{
print_recursively($value);
}
else
{
echo '<p>'.$key.'</p>';
echo '<p>'.$value.'</p>';
}
}
}
Tune it according to your needs.
If you know there is array hierarchy to one level only
Keep printing the values and if the value is an array using is_array.. Iterate again.
foreach($dataArray as $key =>$value){
if(is_array($value)){
foreach($value as $array2Data){
echo $array2Data; //you can use keys as well
}
}
else
echo $value;
}

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;
}
}
}
}

PHP Going through array?

I have a $search_array like this
Array
(
[1] => Array
(
[type] => book
[search] => steve
)
[2] => Array
(
[type] => book
[search] => john
)
foreach ($search_array as $s) {
$arrayid = //???????
$searchtype = $s['type'];
$search = urlencode($s['search']);
getResult($arrayid);
}
I'm trying to figure out how to get the array number. So for the first result i need $arrayid to be 1. How do I reference that in the foreach loop?
Thanks
Use a foreach with "$key => $value"
Array
(
[1] => Array
(
[type] => book
[search] => steve
)
[2] => Array
(
[type] => book
[search] => john
)
foreach ($search_array as $key => $s) {
$arrayid = $key
$searchtype = $s['type'];
$search = urlencode($s['search']);
getResult($arrayid);
}
Adding $arrayid => in your foreach loop declaration will auto-assign $arrayid with the current array index.
foreach ($search_array as $arrayid => $s) {
// ...
getResult($arrayid);
}
See foreach on PHP Manual.
foreach ($search_array as $arrayid => $s) {
Please read PHP documentation before asking such basic questions
foreach ($search_array as $arrayid => $s) {
// your code here
}
This is a way you could retrieve data from a Php array. It will retrieve all entries from array $value and their key $key.
foreach ($array as $key => $value) {
// $key is the array index
}

php how to create a foreach loop?

Array
(
[0] => Array
(
[id] =>
[test] => 145198
[defender] => 5590478
[stake] => 107
[game_id] =>
)
[1] => Array
(
[id] =>
[test] => 145198
[defender] => 5590478
[stake] => 107
[game_id] =>
)
how to do a foreach loop for it?
so far i have:
$mresults = $game_set->get_it();
foreach ($mresults as $key => $row)
{ ...dosomething}
but i believe i need to do another one inside this one.
any ideas?
thanks
Your foreach assigns the nested array to the $row variable and can be accessed like:
$row["test"]
$row["id"]
If you wish to loop again, you can do:
$mresults = $game_set->get_it();
foreach ($mresults as $key => $row)
{
foreach($row as $k => $v)
{
echo $k." = ".$v;
}
}
Don't know what you want to do, but try:
$mresults = $game_set->get_it();
foreach ($mresults as $key => $row) {
echo "key: $key, row: $row\n";
}
To see how it works.
In your case, $row will contain that inner array, so you can output those values using $row['test'], $row['defender'], etc.
Most likely this is all you need:
foreach ($game_set->get_it() as $game)
{
echo $game['defender']."\n";
}

Categories