i am trying to print multidimensional array in php below is my array and array variable is gplus
Array ( [0] => Array ( [name] => krishna sitaram [email] => kasyapa25#gmail.com )
[1] => Array ( [name] => ravi kumar [email] => ravikumar#gmail.com )
i tried bellow
foreach($gplus as $gvalue){
echo $gvalue."<br />";
}
but not working.
the result should be
name email
krishna sitaram kasyapa25#gmail.com
ravi kumar ravikumar#gmail.com
Thank you all i got the the correct solution is
foreach($gplus as $array){
echo $array['name']."<br />";
echo $array['email']."<br />";
}
If you're just debugging:
print_r($your_associative_array);
If you want to print it:
foreach($gplus as $array){
foreach($array as $key=>$value){
echo "Key: $key / Value: $value<br />";
}
}
foreach ($gplus as $k => $gvalue) {
echo $gvalue["name"]." ".$gvalue["email"]."<br />";
}
I know a little late but you can do also this, if you like to use the var_dump function for debug information:
ob_start();
var_dump($array);
$array_content_string = ob_get_contents();
ob_end_clean();
you can use this $array_content_string as normal string
first your might know that you have a assoc array not a simple array , to display your assoc array you have to write :
foreach ( $arrayRow as $key => $value)
{
echo $value;
}
Related
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
I have a json array, looks like:
Array (
[0] => Array
(
[k1] => aaa
[k2] => aaa
[kTypes] => Array
(
[ktype1] => Array
(
[desc] => asd
)
[ktype2] => Array
(
[desc] => asd
)
)
)
And I try to get the desc values inside ktypes, tried this:
$items = $myArray;
// echo "<pre>";
// print_r($items);
// echo "</pre>";
echo '<table>';
echo '<tr><th>k1</th><th>k2</th><th>ktype1</th><th>ktype2</th></tr>';
foreach($items as $item)
{
echo "<tr><td>$item[k1]</td><td>$item[k2]</td><td>$item[kTypes][kType1][desc]</td><td>$item[kTypes][kType2][desc]</td>";
}
echo '</table>';
which works fine for the first both columns, but not the ktype ones. There the:
echo is "Array[kType2][desc]"
So I tried a nested loop, but this didn't work, too.
can anyone help me on the right track?
To interpolate multidimensional array accesses in a string, you have to use the "complex" format with curly braces.
echo "<tr><td>$item[k1]</td><td>$item[k2]</td><td>{$item['kTypes']['kType1']['desc']}</td><td>{$item['kTypes']['kType2']['desc']}</td>";
Try this foreach for your specific piece:
echo "<tr>";
foreach ($items as $field => $value) {
if($field =='ktype'} {
foreach($items['ktype'] as $ktype) {
foreach($ktype as $k) {
echo "<td>{$k}</td>";
}
}
} else {
echo "<td>{$value}</td>";
}
}
echo "</tr>";
However, I would do a recursive piece so it can go as deep into the array as possible.
I've done this a million times but for some reason I can't get this to work today...
I have this associative array
Array
(
[0] => stdClass Object
(
[registrantKey] => 106569618
[firstName] => xxx
[lastName] => yyy
[email] => x#x.x
)
[1] => stdClass Object
(
[registrantKey] => 106975808
[firstName] => qqq
[lastName] => ppp
[email] => aaa#aaa.com
)
...
...
I just want to get the first name of each one of them, im using a foreach loop but doesn't really let me get what I want.
Any ideas?
foreach($array as $key=>$value){
echo $value['firstName'];
}
For this case, your array element isn't an array but an object.
As such, it should be:
foreach($array as $key=>$value){
echo $value->firstName;
}
Try this:
$value->firstName;
You can also do:
foreach($array as $key=> (array) $value){
echo $value['firstName'];
}
This will typecast your object to an array.
foreach($array as $key=>$value){
echo $value->firstName;
}
You have stdClass Objects as array elements and not associative arrays so you need the option notation: $value->firstName
You could also convert the stdClass Object to array by type casting:
foreach($array as $key=> (array) $value){
echo $value['firstName'];
}
<?php
$array = (array)$array;
$firstNames = array();
foreach($array as $a)
{
$firstNames[] = $a['firstName'];
}
print_r($firstNames);
?>
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";
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®ion=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.