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.
Related
I can't quite get my data from an array, it just shows a blank list of list items when I run my foreach loop.
When I print my array it outputs the following:
Array (
[Description] => Array (
[0] => sidewindo
[1] => sidewindo
[2] => sidewindo
[3] => testing )
[comments] => Array (
[0] => sidewindo
[1] => sidewindo
[2] => sidewindo
[3] => testing )
[Fld_id] => 625
[fld_IsPublic] => Array (
[0] => 1 )
[action] => s
[pk_id] => 7 )
My code so far
public function save_data($data)
{
foreach ($data as $row)
{
foreach ($row['Description'] as $show)
{
echo $show;
}
}
And i have tried the following kind of for loop also to retrieve data from the array. I am trying to retrieve data from the array and update it in another table in the database. This code I have written in my model before passing it to the view.
for($i=0;$i<count($comments);$i++)
{
$this->db->update->update('tblname',array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id'],'fld_description'=>$data['Description'],'fld_comments'=>$data['comments'],'fld_IsPublic'=>$data['fld_IsPublic']),array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id']));
}
}
First of all you should know that echo an array will produce a Notice ( Array to string conversion ), so depending in what environment you are working on you will prefer to use print_r.
Answering your question, before you echo, you should check if it's an array or not
public function save_data($data){
$update = array();
foreach ($data as $key=>$row){
if(is_array($row)){
foreach($row as $key2=>$row2){
$update[$key2] = $row2;
//echo $row2 . "<br>";
}
}else{
$update[$key] = $row;
//echo $row ."<br>";
}
}
}
And now you can use $update to update your table , include this on inside save_data() function, and if you needed, include save_data() inside a foreach. Remember that is recommended to use a where when updating data so:
//$this->db->where('id', $id);
$this->db->update('tblname', $update);
hope that helps
Change Your code from
public function save_data($data)
{
foreach ($data as $row)
{
foreach ($row['Description'] as $show)
{
echo $show;
}
}
to
public function save_data($data)
{
foreach ($data["comments"] as $key=>$row)
{
$data_update = array(
'TK_id' => $data['pk_id'],
'Fld_id' => $data['Fld_id'],
'fld_description' => $data['Description'][$key],
'fld_comments' => $row,
'fld_IsPublic' => $data['fld_IsPublic'][0]
);
$this->db->update('tblname',$data_update,array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id']));
//but this will update single row again and again
//my suggetion is use $this->db->insert('tblname',$data_update); instead
}
}
ANd you will be just fine
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 have a html form in which i use array like this (name="courts[]"). when it send data to php file i use foreeach loop to create multidimensional array for inserting records in mysql. In php file i write foreeach loop to iterate like this
$data = array();
$i = 0;
foreach ($court_name as $result)
{
$data[] = array(
'court_name' => $result[0]
);
$i++;
}
it display result this
Array
(
[0] => Array
(
[court_name] => P
)
[1] => Array
(
[court_name] => S
)
)
instead of this
Array
(
[0] => Array
(
[court_name] => Punjab
)
[1] => Array
(
[court_name] => Sindh
)
)
(referring the outputs) in your loop, $result contains court name. So if you use $result[0], you get first character of string.
Try this:
foreach ($court_name as $result)
{
$data[] = array(
'court_name' => $result
);
$i++;
}
foreach loop gives you one element of array ($result) now you access to the first character of value via $result[0], change it to $result
foreach ($court_name as $result) {
$data[] = array( 'court_name' => $result );
}
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>";
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";