PHP Array Printing Using a Loop

Print Multi-dimensional array using loop in PHP

you have associative array inside associative array
You have to loop the first associative array
and inside it loop the associative array
like this

foreach($bazar as $key => $val){

echo $key.'<br>';

foreach($val as $k => $v){
echo $k . ' -> ' .$v . '<br>';
}

}

Printing multiple array items in one loop PHP

use a foreach loop and a counter

   $i=0;
foreach($PriceArray as $val){

$StatusArrayVal = $StatusArray[$i];

/*
echo each row
your code
*/

$i++;
}

you will have one value in the $val variable and other in $StatusArrayVal
you can echo all the html code or set between tags <? ? >

Get PHP array values and print in a loop

$names = array("Mike", "Kyle", "Johnny", "Will", "Vasques");
for($td=0; $td<=9; $td++) {
echo "<tr>";
if ($td == 0) {
foreach ($names as $name) {
echo "<td>$name</td>";
}
}
echo "<td></td>";
echo "</tr>";
}

PHP - Loop through array and print out value only if it changes between iteration

you can use foreach and group by using regulations_label and groups_label

$group = [];
foreach($data as $v){
$group[$v['regulations_label']][$v['groups_label']][] = $v['filters_label'];
}

DEMO

How to Loop Array Inside Associative Arrays

Use two foreach loop

<?php 

$siswa = array(
"Kelas-X" => array("Joko", "Budi", "Duduk"),
"Kelas-XI" => array("Entong", "Timun", "Opang"),
"Kelas-XII" => array("Mamat", "Sadaw", "Koreng"),
);

foreach($siswa as $key => $value){
foreach($value as $k => $v){
echo "Key : " . $key. "Value : " . $v;

}
}

?>

How can I echo or print an array in PHP?

This will do

foreach($results['data'] as $result) {
    echo $result['type'], '<br>';
}

PHP: How to print associative array using while loop?

try this syntax and this is best efficient way to do your job...........

while (list($key, $value) = each($array_expression)) {
statement
}

<?php

$data = array('a' => 1, 'b' => 2, 'c' => 3);

print_r($data);

while (list($key, $value) = each($data)) {
echo '$'.$key .'='.$value;
}

?>

For reference please check this link.........

Small Example link here...

PHP array is not printing outside foreach loop

It would be easier to build the string whilst reading the data than to manipulate it in more loops...

    $results = $con->query("SELECT DISTINCT ur.mobile FROM matchmst mst INNER JOIN matchdetail dtl on mst.matchid=dtl.matchid INNER JOIN user ur on ur.userid=dtl.userid WHERE mst.matchid = '$matchid2'");
// look through query
$mobiles = "";

while($row = $results->fetch_assoc()){
$mobiles .= $row['mobile'] . ",";
}
// Remove trailing , if needed
$mobiles = substr($mobiles, 0, -1);

You should also look at using prepared statements to improve security of your site.

foreach loop output first value only in loop

try this solution:

function array_unique_multidimensional($array, $key)
{
$temp_array = array();
$i = 0;
$key_array = array();

foreach ($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}

$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
$J_Min = strtolower($Jdata_cate);
$J_MinDecoded = json_decode($J_Min, true);

$Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
$D_Min = strtolower($Ddata_cate);
$D_MinDecoded = json_decode($D_Min, true);

$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);

$Delete_repeated = array_unique_multidimensional($both_arrays, 'name');

foreach ($Delete_repeated as $y => $y_value) {
echo $y_value['name'] . '<br>';
}

here instead of array_unique I'm using my defined function



Related Topics



Leave a reply



Submit