How to print multiple arrays with foreach? - php

I have 2 arrays: post_titles and posts. How do I print them one after another with foreach?
When I use 1 array, it works fine:
<?php foreach ($titles as $row) { ?>
<?php echo $row['post_title'] ?> <br>
<?php } ?>
I want data to be printed like this:
Title
Post
<br>
Title
Post
<br>
etc.

If each item in the titles and post correspond to each other (eg titles[1] and posts[1], titles[2] and posts[2]), you could use a for loop.
eg.
for($i = 0; $i < count($titles); $i++) {
echo $titles[$i];
echo $posts[$i];
echo "<br>";
}
or
foreach ($titles as $i => $value ){
echo $value ." <br>" . $posts[$i] . " <br>";
}

If the array have the same index key you can use this
<?php
foreach($titles as $key=> $value) {
echo $value . ' - ' $post[$key] . '<br>';
}
?>

If the 2 arrays are in sync i.e. the same length and arr1(0) equates to arr2(0) then its easy
<?php
foreach ($post_titles as $i => $title) {
echo $title . '<br>' . $posts[$i] . '<br>';
}
?>

Related

How to loop through an array and print each item into an <li> tag using PHP?

I'm trying to iterate through the array using php. I want the items to be stored in an <li> tag. I'm having trouble getting the names to print out. I can get a count of the array items printed, however not the names.
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
for($i=0; $i < count($names); $i++) {
echo "<li>" . $i . "</li>";
}
?>
</ul>
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $name) {
echo "<li>" . $name . "</li>";
}
?>
</ul>
This is a little more natural than both imploding and looping through the array by index.
Your code wasn't working was because you were echoing the index of the name in the array rather than the actual name
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach($names as $name) {
echo "<li>" . $name . "</li>";
}
?>
</ul>
You need to echo the array not the count which is $i. So your echo should look like this:
echo "<li>" . $names[$i] . "</li>";
This should work for you:
Just implode() your array, e.g.
echo "<li>" . implode("</li><li>", $names) . "</li>";
Your current code doesn't work, because your don't use your variable $i as index. So you would have to use:
$names[$i]
You need to pull the index out of the array, otherwise you will just get 0,1,2,3...
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
for($i=0; $i < count($names); $i++) {
echo "<li>" . $names[$i] . "</li>";
}
?>
</ul>
Another way to code it would be:
<ul>
<?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); ?>
<?php foreach ($names as $name) : ?>
<li><?php echo $name ?></li>
<?php endforeach ?>
</ul>

How to bring array values out of foreach loop?

I have a foreach loop like this.
if ( is_object($res_two)) {
if($res_two->num_rows() == 0) {
echo "No Records Found";}
else if( $res_two->num_rows() > 0) {
foreach ($res_two->result() as $row ) {
echo $row->js_id."\t". $row->designation."\t".$row->full_name."\t".$row->location."\t".$row->graduated_in."\t";
$mail2 = $row->email;
echo $mail2 ;
?> Download Resume <br/><br/> <?php
}
}
}
?>
Now I want to extract $mail2 details. However echoing $mail2 out of the code giving only one value instead of an array(if foreach loop iterates, it should have multiple values?).
How to get the multiple values of $mail2 outside the code?
<?php
if (is_object($res_two)) {
if ($res_two->num_rows() == 0) {
echo "No Records Found";
} else if ($res_two->num_rows() > 0) {
foreach ($res_two->result() as $row) {
echo $row->js_id . "\t" . $row->designation . "\t" . $row->full_name . "\t" . $row->location . "\t" . $row->graduated_in . "\t";
$mail2[] = $row->email;
?> Download Resume <br/><br/> <?php
}
}
}
print_r($mail2);
?>

foreach loop with more than one step

I am using PDO to get result from database and then using foreach loop to display data.
my code is
$result1=$objPage->getGallery($id);
foreach($result1 as $row)
{
echo "pic1=" . $row['pic'];
echo "pic2=" . $row['pic'];
echo "pic3=" . $row['pic'];
echo "<br>";
}
Actually, I want to display three pictures names in one line and then next 3 names.
But now its printing one name 3 times.
Do something like this:
$result1=$objPage->getGallery($id);
$count = 0;
foreach($result1 as $row)
{
echo "pic" . $count + 1 . "=" . $row['pic'];
if( $count % 3 == 2 )
{
echo "<br>";
}
$count++;
}
Information
Modulo %
first, prepare your data
$data = $objPage->getGallery($id);
$data = array_chunk($data,3);
then output it
<table>
<? foreach($data as $chunk): ?>
<tr>
<? foreach($chunk as $row): ?>
<td><img src="<?=$row['pic']?>"></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
You can use an external flag, like this:
$flag = 0;
$result1=$objPage->getGallery($id);
foreach($result1 as $row) {
echo "pic" . ($flag + 1) . "=" . $row['pic'];
if( $flag % 3 == 0 )
echo "<br>";
$flag++;
}
You could use modulo as suggested by others, but how about this more compact method?
$result1 = $objPage->getGallery($id);
$separators = array('', '', '<br/>');
foreach ($result1 as $index => $row) {
echo 'pic' . ($index % 3) . '=' . $row['pic'] . $separators[$index % 3];
}

To get the multi dimention array values in php

I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}

Looping through array objects to output

I have sent $data['items'] to my view which has created an array full of objects which I can echo with a foreach loop.
foreach($items as $row)
{
echo $row->NAME . " - " . $row->COLOUR . "<br>";
}
what I want to do is echo them to the browser in groups with the name of the colour as a header tag then start the loop for that colour. I'm just not sure what type of loop to do or should I have a loop within a loop?
BLUE
-item 1
-item 3
RED
-item 2
-item 4
-item 5
$list = array();
foreach($items as $row)
{
$list[$row->COLOUR][] = $row->NAME;
}
$header = null;
foreach($list as $item)
{
if($header != $item->COLOUR)
{
echo '<h3>' . $item->COLOUR . '</h3>';
$header = $item->COLOUR;
}
echo '- ' . $item->NAME . '<br />';
}
You probably want an interim two-dimensional array:
$tmp = array();
foreach($items as $row)
{
// this code groups all items by color
$name = $row->NAME;
if( !isset ($tmp[ $name ] ) ) $tmp[ $name ] = array();
$tmp[ $name ][] = $row->COLOUR;
}
foreach( $tmp as $color => $items )
{
// colors are now keys to the temp array
echo $color;
// these are all of the items grouped under the current color
foreach( $items as $item )
{
// output the item.
echo "<br /> - $item";
}
echo "<br />";
}

Categories