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++;
}
Related
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 make a foreach with this type of array please ?
Thanks.
Array (
[0] => Array (
[0] => TPK12\"
[1] => MRP59\"
[2] => MSM105\"
[3] => RGS70\"
[4] => GDN36\"
)
)
I tried that but it echoes just Array():
foreach($match as $value) {
echo $value;
}
For a two-dimensional array you can use this:
foreach($match as $subArray) {
foreach($subArray as $value) {
echo $value;
}
}
Since you have a multidimensional array, you need to either have a foreach inside another, or specify which array you want to iterate.
foreach ($match[0] as $value) {
echo $value;
}
I have an array structure like this, which I'm able to print out just fine:
Array
(
[0] => Array
(
[title] => blah
[author] => Bob
[link] => randomlink
)
[1] => Array
(
[title] => random
[author] => George
[link] => randomlink
)
[2] => Array
(
[title] => blah
[author] => Bob
[link] => randomlink
)
)
Basically, I want to be able to print out only the information in the array that's related to the 'author' 'Bob'. As you can see, he has two items in there. When I print out the array, it should only show the 0 and 2 array since those are the only ones that contain the 'author' which is 'Bob'. Any ideas?
foreach ($array as $a)
{
if($a['author'] === 'Bob') {
echo $a['title'];
echo $a['author'];
echo $a['link'];
}
}
foreach($arr as $item)
{
if($item['author'] != 'Bob')
{
continue;
}
// print out Bob's stuff
}
This is the code:
foreach($array as $subarray)
{
if(strcasecmp($subarray['author'],'Bob') === 0)
print_r($subarray);
}
Simply foreach
foreach ($array as $item) {
if ($item['author']) {
// Do something with $item
}
}
ok ! try this :
for($i=0;$i<count($array);$i++){
if($array[$i]['author'] == 'bob'){
echo $array[$i]['title']." > ".$array[$i]['author']." > ".$array[$i]['link']."\r\n<br>";
}
}
...
I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";
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";
}