Question related to array - php

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";

Related

Nested 'foreach' for multidimensional array

<?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
)
)
)

PHP associate array

I have a output of print_r below and I want to access all the individual elements value with foreach loop but unfortunately I am unable to do that via foreach. Could anyone please help me with the associate array question.
I can access via this $arr['Level1'][Date] and it returns value as "2015-04-14 07:15".
But how to get all the element values via foreach loop?
Array
(
[Level1] => Array
(
[Date] => 2015-04-14 07:15
[img1] => pic1
[img2] => pic2
[InnerLevel] => Array
(
[0] => value1
[1] => value2
)
)
[Level2] => Array
(
[Date] => 2015-04-15 08:15
[img1] => pic1
[img2] => pic2
[InnerLevel] => Array
(
[0] => value3
[1] => value4
)
)
)
<?php
foreach ($myarray as $level => $itemArr) {
if(is_array($itemArr)) {
foreach ($itemArr as $levelArr) {
if(is_array($levelArr)) {
foreach ($levelArr as $key => $interlevelValue) {
echo $interlevelValue;
}
} else {
echo $levelArr;
}
}
} else {
echo $itemArr;
}
}
?>
foreach ($myarray as $item) {
echo $item['Date'] . "\n";
}
The fact that the array is associative or not doesn't change anything.
$item is successively a copy of $myarray['Level1'] then $myarray['Level2'] (etc. if more) in the foreach loop.
It depends on index depth.
To extract simple associative arrays like:
$mainarray['Name']='Value';
Use:
foreach ($mainarray as $aname=>$avalue)
{
echo $aname." in ".$mainarray." = ".$avalue." <br>";
}
To extract deeper associative arrays like:
$mainarray['Child']['Name']='Value';
Use:
foreach ($mainarray as $aname=>$asubarray)
{
echo "In ".$aname." from ".$mainarray."...<br>";
foreach ($asubarray as $asubname=>$asubvalue){
echo $asubname." = ".$avalue." <br>";
}
echo "<br>";
}
This:
<br>
represents new line. If you're running the code from a command line or you just want text only output, then use this for a new line:
\n
Actually there is another way of doing this in php.
"Iterating over Multidimensional arrays is easy with Spl Iterators :
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $key=>$value) {
echo $key.' -- '.$value.'<br />';
}
This will iterate through all possible iterable objects.
See
http://php.net/manual/en/spl.iterators.php
http://www.phpro.org/tutorials/Introduction-to-SPL.html
" - Quoted from - https://stackoverflow.com/a/2149106/1766122 answered by #Gordon

Foreach loop through multidimentional array like this

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++;
}

Search multidimensional array by specific key and print the value php

I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
Example: key['user_id'] this will print all user_id from array. is it possible? please help me thanks
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 2
)
)
[Post3] => Array
(
[0] => Array
(
[user_name] => 1
)
)
)
Here is my PHP code:
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;
}
}
}
You can print_r to achive the same results you want with your triple for each.
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
You can easily use recursion for such a problem. You can use something along the lines of:
function printValuesByKey($array, $key) {
if (!is_array($array)) return;
if (isset($array[$key]))
echo $key .': '. $array[$key] .'<br>';
else
foreach ($array as $v)
printValuesByKey($v, $key);
}
In your example:
printValuesByKey($array, 'user_id');
will print:
user_id: 1
user_id: 2

Any idea how I pull those values?

foreach ($_GET as $field => $label) {
$datarray[]=$_GET[$field];
echo "$_GET[$field]";
echo "<br>";
This is the output I am getting. I see the data is there in datarray but when I echo $_GET[$field] I only get "Array"
But print_r($datarray) prints all the data. Any idea how I pull those values?
OUTPUT:
Array (
[0] => Array (
[0] => Grade1
[1] => ln
[2] => North America
[3] => yuiyyu
[4] => iuy
[5] => uiyui
[6] => yui
[7] => uiy
[8] => 0:0:5
)
)
foreach ($_GET as $key => $value)
{
if(is_array($value))
{
foreach($value as $childKey => $childValue)
{
echo $childKey." ".$childValue; // or assign them to an array
}
}
else
echo $key." ".$value; // or assign them to an array
}
Seems like $_GET[$field] is basically $_GET[0], which is an array:
You'll have to loop through $_GET[$field] with a forloop to get the content to echo out. By the way you can't echo array you'll have to use print_r
something like this:
foreach ($_GET as $field => $label) {
$datarray[]=$_GET[$field];
for($i=0; $i<$_GET[$field]; $i++){
echo $_GET[$field][$i];
}
echo "<br>";
}
EDIT: When I completed your test, here was the final URL:
http://hofstrateach.org/Roberto/process.php?keys=Grade1&keys=Nathan&keys=North%20America&keys=5&keys=3&keys=no&keys=foo&keys=blat&keys=0%3A0%3A24>
This is probably a malformed URL. When you pass duplicate keys in a query, PHP makes them an array. The above URL should probably be something like:
http://hofstrateach.org/Roberto/process.php?grade=Grade1&schoolname=Nathan&region=North%20America&answer[]=5&answer[]=3&answer[]=no&answer[]=foo&answer[]=blat&time=0%3A0%3A24
This will create individual entries for most of the fields, and make $_GET['answer'] be an array of the answers provided by the user.
Bottom line: fix your Flash file.

Categories