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";
}
Related
Array
(
[data] => Array
(
[0] => Array
(
['degree_level'] => Bachelor's
)
[1] => Array
(
['field_of_study'] => Science
)
[2] => Array
(
['grade_point'] => 3
)
[3] => Array
(
['criteria'] => desired
)
)
)
What I want :
Array
(
[data] => Array
(
['degree_level'] => Bachelor's
['field_of_study'] => Science
['grade_point'] => 3
['criteria'] => desired
)
)
You should use array_flatten(); to achieve your goal like this,
$flattened = array_flatten(Your_Data_Array);
Please give it a try and let me know.
UPDATE
$flattened = array_map(function($item) {
return $item[0];
}, Your_Data_Array);
For more information you can visit this for PHP functions.
Let me know in case of any queries.
$output = array_map(function($item) { return $item[0]; }, $myArray);
Try this,
foreach($data as $key1=>$val1){
foreach($val1 as $key2=>$val2){
$new_array[$key2] = $val2;
}
}
You can do it by loop.
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value2) {
$data[$key1] = $value2;
}
}
You could use for example a double foreach loop to use the key and the value from the second loop and add those to the $arrays["data"] array.
Then you could use unset to remove the nested arrays.
$arrays = [
"data" => [
["degree_level" => " Bachelor's"],
["field_of_study" => "Science"],
["grade_point" => 3],
["criteria" => "desired"]
]
];
foreach($arrays["data"] as $dataKey => $data) {
foreach ($data as $key => $value) {
$arrays["data"][$key] = $value;
}
unset($arrays["data"][$dataKey]);
}
print_r($arrays);
That would give you:
Array
(
[data] => Array
(
[degree_level] => Bachelor's
[field_of_study] => Science
[grade_point] => 3
[criteria] => desired
)
)
Demo
you can achieve this using array_collapse.
Link
EDIT :
while tag has changed.
Here is the core php solution based on Laravel array_collapse:
function collapse($array)
{
$results = [];
foreach ($array as $values) {
if (! is_array($values)) {
continue;
}
$results = array_merge($results, $values);
}
return $results;
}
<?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
)
)
)
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;
}
I have a multidimensional array in variable $comments, containing:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => appr
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => appr
)
// There is more...
)
How do I loop through this array and echo each value using for each?
foreach($arrayOfArrays as $array){
foreach($array as $index => $value){
echo $array[$index];
}
}
You should use two foreach loops as your array has to levels :
foreach ($comments as $comment)
foreach ($comment as $comment_data)
echo $comment_data;
If your array structre stay like the one you show, you can do this like follow :
foreach($comments as $comment) {
echo $comment[0];
echo $comment[1];
echo $comment[2];
echo $comment[3];
}
Just use two foreach loops. One inside the other
foreach($comments as $commentArray){
foreach($commentArray as $comment){
echo $comment;
}
}
Hope this helps you
$i=0;
$c=count($array);
while ($i<$c) {
foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}
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
}