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.
Related
I have a php array value but i don't know how to print it. there is my php code
$json = array();
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
$json[] = $result;
}
}
And here is my array value
Array
(
[0] => Array
(
[locationid] => 1
[locationname] => Anantapur
[locationvalue] => 1.1
)
[1] => Array
(
[locationid] => 2
[locationname] => Guntakal
[locationvalue] => 1.2
)
[2] => Array
(
[locationid] => 4
[locationname] => Guntur
[locationvalue] => 1.3
)
)
I'm Trying like
echo $json['locationname'];
But It's Not working please help me
You need to loop through your array first. Try foreach
foreach($json as $j){
echo $j['locationname'];
}
You have not need to make a new array after get the result. you can print your value within the loop
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
echo $result['locationaname'];
}
}
The array you created is associative array. For loo through these type of arrays requires foreach. Visit http://php.net/manual/en/control-structures.foreach.php
foreach($json AS $key =>$value){
echo $value['locationname'];
}
You can directly print the value of location, instead of creating a new array, unless you need it further.
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
echo $result['locationname'];
}
}
if you want to have $json, which is an associative array, you need to get into it, looping through it.
foreach($json as $key => $value){
echo $value['locationname'];
}
If you want json string try this:
echo json_encode($json);
after your loop.
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
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 />";
}
}
I have done a lot of looking around on the overflow, and on google, but none of the results works for my specific case.
I have a placeholder array called $holder, values as follows:
Array (
[0] => Array (
[id] => 1
[pid] => 121
[uuid] => 1
)
[1] => Array (
[id] => 2
[pid] => 13
[uuid] => 1
)
[2] => Array (
[id] => 5
[pid] => 121
[uuid] => 1
)
)
I am trying to pull out distinct/unique values from this multidimensional array. The end result I would like is either a variable containing (13,121), or (preferrably) an array as follows:
Array(
[0] => 13
[1] => 121
)
Again I've tried serializing and such, but don't quite understand how that works when operating with a single key in each array.
I tried to be as clear as possible. I hope it makes sense...
Seems pretty simple: extract all pid values into their own array, run it through array_unique:
$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
The same thing in longhand:
$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);
In php >= 5.5 you can use array_column:
array_unique(array_column($holder, 'pid'));
try this
foreach($arr as $key => $val) {
$new_arr[] = $val['pid'];
}
$uniq_arr = array_unique($new_arr);
Just iterate over it and apply an array_unique on the result:
foreach($holder as $yourValues){
$pids[] = $yourValues['pid'];
}
$yourUniquePids = array_unique($pids);
Assuming your array is called $holder:
$unique = array();
foreach( $holder as $h )
if( ! in_array($h, $unique ) )
$unique[] = $h;
is a slightly more efficient way than via array_unique, I believe. May be the same.
$uniquearray = [];
for($i=0;$i<count($assocarray);$i++){
if(!in_array($assocarray[$i]['KEY'],$uniquearray)){
$uniquearray[$i]= $assocarray[$i]['KEY'];
}
}
Hi Please try code given below for get unique values and then sort that values
<?php
$sort_arr = unique_sort($holder, 'pid');
echo "<pre>";
print_r($sort_arr);
echo"</pre>";
/*function for get unique value then sort them*/
function unique_sort($arrs, $id) {
$unique_arr = array();
foreach ($arrs AS $arr) {
if (!in_array($arr[$id], $unique_arr)) {
$unique_arr[] = $arr[$id];
}
}
sort($unique_arr);
return $unique_arr;
}
thanks
fo my situation i use this method
//get some data from db to array
while ($row = mysqli_fetch_assoc($query)){
$data[] = $row;
}
//display unique value
echo "<table>";
$uniq = array();
foreach ($data as $row) {
if(!in_array($row['path'], $uniq)) {
$uniq[] = $row['path'];
echo "<tr>";
echo "<td>";
echo $row['path'];
echo "</td>";
echo "<td>";
echo $row['relationship_id'];
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
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.