This is my array structure:
Array
(
[Title] => Array
(
[0] =>
[1] => One
[2] => Two
[3] => Three
[4] => Four
[5] => test
[6] => fsfd
[7] => wa
)
)
I would like to print the title and array elements so that it is structured like this:
Title
One
Two
Three
etc
I am currently having trouble doing this using the conventional for each loop:
foreach($items as $key => $notice ){?>
}?>
What is the best way to do this? Thanks
Your array is nested. Either use
foreach($items['Title'] as $key => $noticeArr ){
Or if you wish to print the keys of the first array use:
foreach($items as $key => $noticeArr ){
echo $key . "\n";
foreach($noticeArr as $notice){
echo $notice . "\n";//Wrap in <li> tags or however you want to display.
}
}
You have an array "Title" inside your array.
You could use the traditional for loop:
for ($i = 0; $i < count($yourArray['Title']); $i++) {
echo $yourArray['Title'][$i];
}
or a foreach:
foreach ($yourArray['Title'] as $item) {
echo $item;
}
You just have to iterate two times, one for the first array and another for the nested one.
$data = array(
'Title' => array('One','Two','Three'),
);
foreach ($data as $name => $results) {
echo $name . "<br />";
foreach ($results as $label)
{
echo $label . "<br />";
}
}
Try this, will work
foreach($items as $key => $noticeArr ){
echo $key . "<br />";
$array = array_filter($noticeArr, create_function('$a','return $a!=="";'));
foreach($array as $notice){
echo "<li>".$notice ."</li>"."<br />";
}
}
Related
<?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
)
)
)
I made an array $brands inside class Cars, the print_r output looks like this:
Array ( [0] => Array ( [id] => 1
[name] => 'Porsche'
)
[1] => Array ( [id] => 2
[name] => 'Bugatti'
)
[2] => Array ( [id] => 3
[name] => 'BMW'
)
)
But when I'd like to link to a certain brand, I don't want to use strtolower() to make a lowercased hyperlink. I would like to echo $cars->brands[$i]['url'] (instead of strtolower($cars->brands[$i]['name'])).
class Cars {
So I needed to create a for loop, to create the ['url'] key in the array. I thought that a foreach would work:
foreach ($this->brands as $brand => $row) {
$row['url'] = strtolower($row['name']);
}
But it didn't. Even this did not work: $row['name'] = strtolower($row['name']);.
But this worked, though:
for ($i = 0; $i < count($this->brands); $i++) {
$this->brands[$i]['url'] = strtolower($this->brands[$i]['name']);
}
}
My question here is: how? why?
You need to work on a reference. Insert a & and it will work
foreach ($this->brands as $brand => &$row) {
$row['url'] = strtolower($row['name']);
}
or you could work on the original array like:
foreach ($this->brands as $brand => $row) {
$this->brands[$brand]['url'] = strtolower($row['name']);
}
if you want to edit the element being iterated you can add a & before $row
foreach ($this->brands as $brand => &$row) {
$row['url'] = strtolower($row['name']);
}
but this is not necessary, just access the array from the variable available outside of the foreach loop e.g $this->brands
foreach ($this->brands as $brand => $row) {
$this->brands[$brand]['url'] = strtolower($row['name']);
}
because you are overwriting array key('url').
$row is a local copy of $this->brands any changes to $row will not reflect on $this->brands.
change this
foreach ($this->brands as $brand => $row) {
$row['url'] = strtolower($row['name']);
}
with this
foreach ($this->brands as $brand => $row) {
$this->brands[$brand]['url'] = strtolower($row['name']);
}
Happy Coding.
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);
}
}
I have an associative array in php, how can i print it with php foreach loop
Array( [0] => Array ( [fb_user_id] => 100000058716604 [accept_status] => 1 ) [1] => Array ( [fb_user_id] => 100004069844270 [accept_status] => 1 ) )
Tried this but no success, i want to print the fb_user_id
foreach($resulttotal[fb_user_id] as $value)
{
echo $value;
}
Please help me, Thanks
You doing incorrectly, It will be like:
foreach($resulttotal as $value)
{
echo $value['fb_user_id'];
}
It would make more sense to use a regular for loop.
for($i = 0; $i < count($resulttotal); $i++) {
$row = & $resulttotal[$i];
echo $row['fb_user_id'];
}
But this could be a foreach too.
foreach($resulttoal as $key) {
echo $key['fb_user_id']
}
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 months ago.
How do I access the first level key of a two-dimensional array using a foreach loop?
I have a $places array like this:
[Philadelphia] => Array
(
[0] => Array
(
[place_name] => XYX
[place_id] => 103200
[place_status] => 0
)
[1] => Array
(
[place_name] => YYYY
[place_id] => 232323
[place_status] => 0
)
This is my view code that loops over the array:
<?php foreach($places as $site): ?>
<h5><?=key($site)?></h5>
<?php foreach($site as $place): ?>
<h6><?=$place['place_name']?></h6>
<?php endforeach?>
<?php endforeach ?>
Where I call key($site), I want to get Philadelphia, but I am just getting place_name from each row.
You can access your array keys like so:
foreach ($array as $key => $value)
As Pekka stated above
foreach ($array as $key => $value)
Also you might want to try a recursive function
displayRecursiveResults($site);
function displayRecursiveResults($arrayObject) {
foreach($arrayObject as $key=>$data) {
if(is_array($data)) {
displayRecursiveResults($data);
} elseif(is_object($data)) {
displayRecursiveResults($data);
} else {
echo "Key: ".$key." Data: ".$data."<br />";
}
}
}
You can also use array_keys() . Newbie friendly:
$keys = array_keys($arrayToWalk);
$arraySize = count($arrayToWalk);
for($i=0; $i < $arraySize; $i++) {
echo '<option value="' . $keys[$i] . '">' . $arrayToWalk[$keys[$i]] . '</option>';
}
foreach($shipmentarr as $index=>$val){
$additionalService = array();
foreach($additionalService[$index] as $key => $value) {
array_push($additionalService,$value);
}
}