how to format json with array of json - php

I want output in json like this
response:{url="www.google.com/raj.png", size=12.344KB},{url="www.google.com/raj2.png", size=12.344KB},{url="www.google.com/raj4.png", size=12.344KB}
But currently i am getting
"url=> www.google.com/img1.png size => 12.344 KB,url=> www.google.com/img2.png size => 12.344 KB"

//Using some loop here
{
$response[] = array('url' => 'url_value','size' => 'file_size');
}
//without loop hardcoded values:
$response = array ( array('url' => "www.google.com/raj.png",'size' => "12.344KB"),
array('url' => "www.google.com/img2.png",'size' => "10.344KB") );
return json_encode($response);

$out= Array(
Array('url'=>'www.goog', 'size'=>'12KB'),
Array('url'=>'moogle', 'size'=>'13KB')
);
echo Json_encode($out);

Not sure how you are formatting your array, but if you define it like this it should do the trick:
$response_array = array(
'response' => array(
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
),
);
$respose_json = json_encode($response_array);
echo($response_json);
For looping through some results, try something like this:
$response_array = array('response' => array());
foreach($result_set as $result_item) {
$response_item = array();
$response_item['url'] = $result_item['url'];
$response_item['size'] = $result_item['size'];
$response_array['response'][] = $response_item;
}
$respose_json = json_encode($response_array);
echo($response_json);
With the $result_set above being whatever data you've pulled from a DB, server, etc.

Related

How to use a while loop result within three dimensional array in php?

I would like to pass a while loop result as a value to three dimensional array,i have tried but i couldn't get it.I am trying to solve this from few days.Any suggestions or answers are appreciated.Thank you
$messages = array(
'sender' => "akhil",
'messages' => array(
//////////////while loop starts
while($i < $data){
array(
'number' =>$data[$i],//////here i want to pass the while loop
variable
'text' => rawurlencode('Hello,.........')
)
$i++;
}
/////////////while loop ends
)
);
///the would like to get the below result
$messages = array(
'sender' => "akhil",
'messages' => array(
array(
'number' => 918xxxxxx,
'text' => rawurlencode('Hello,------')
),
array(
'number' => 9196xxxxxx,
'text' => rawurlencode('Hello,----')
)
), array(
'number' => 919xxxxx,
'text' => rawurlencode('Hello,----')
)
)
);
You just need to create the array outside the while loop and then push values into it inside the loop. Your code is almost there...
$messages = array('sender' => "akhil",
'messages' => array()
);
while ($i < count($data)) {
$messages['messages'][] = array('number' => $data[$i],
'text' => rawurlencode('Hello,.........'));
$i++;
}
Demo on 3v4l.org
What you are looking for is called an Anonymous function.
You can achieve your expected behavior by doing this:
'messages' => (function(){
$res = [];
while($i < $data){
$res[] = [
'number' =>$data[$i],//////here i want to pass the while loop variable
'text' => rawurlencode('Hello,.........')
];
$i++;
}
return $res;
})(),
...
I do not know the exact structure of your data, but I would swap the while for an array_map(). It would look like this:
'messages' => array_map(function($d){
return [
'number' =>$d,
'text' => rawurlencode('Hello,.........')
]
},$data),
...
Hohpe that helps,

Formatting json to geojson via PHP

I am trying to get some data that I have called from a mySQL database to correctly display in a GeoJSON format. Here's some of my PHP code:
$data = array(); //setting up an empty PHP array for the data to go into
if($result = mysqli_query($db,$query)) {
while ($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
}
$jsonData =json_encode($data);
$original_data = json_decode($jsonData, true);
$coordinates = array();
foreach($original_data as $key => $value) {
$coordinates[] = array($value['latitude'], $value['longitude']);
}
$new_data = array(
'type' => 'FeatureCollection',
'features' => array(array(
'type' => 'Feature',
'properties' => array('time' => $value['time']),
'geometry' => array('type' => 'Point', 'coordinates' => $coordinates),
),
),
);
$final_data = json_encode($new_data, JSON_PRETTY_PRINT);
print_r($final_data);
I've managed to get my results to look like this so far:
But I need them to look like this so that every set of coordinates has its own "type" and "properties" key-value pair:
I've already found some help with this issue here, but I just can't manage to get over this last formatting hurdle...
Instead of building coordinates, you need to build features:
$data = array(); //setting up an empty PHP array for the data to go into
if($result = mysqli_query($db,$query)) {
while ($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
}
$jsonData =json_encode($data);
$original_data = json_decode($jsonData, true);
$features = array();
foreach($original_data as $key => $value) {
$features[] = array(
'type' => 'Feature',
'properties' => array('time' => $value['time']),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$value['latitude'],
$value['longitude'],
1
),
),
);
}
$new_data = array(
'type' => 'FeatureCollection',
'features' => $features,
);
$final_data = json_encode($new_data, JSON_PRETTY_PRINT);
print_r($final_data);

Creating JSON array of arrays with keys from MySQL into sing JSON object

I am trying to create an array of arrays in a loop to a JSON object, but It returns two objects instead. If I remove the array around the array with a key, it works but I need the key.
This is the format I am looking for:
$shop = array( "1408842145690" => array( id => "1408842145690",
code => "1",
title => "zdfdsf",
date => "2014-08-01",
description => "fghgf"
),
"1408840099517" => array( id => "1408840099517",
code => "1",
title => "test",
date => "2014-08-01",
description => "this is a test"
)
);echo json_encode($shop);
This is the code I am using
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array(
$row['task_id'] => array( 'id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
)
);
$alltasks[] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}
This is the result I get:
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01 00:00:00","description":"fghgf"}},{"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01 00:00:00","description":"this is a test"}}
This is the result I am looking for
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01","description":"fghgf"},"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01","description":"this is a test"}}
Replace the contents of your while loop with the following:
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
EDIT: I tested the above, and it does work. Here is the full code with the replacement intact... try a copy/paste.
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}

Multidimensional array in php to save mysqli output

Here is a piece of my code:
if($all_pages)
foreach ($all_pages as $page)
{
$all_hokms = $mysqli->query("SELECT * FROM qm_hokm WHERE page_id = ".$page['page_id']."");
if($all_hokms)
foreach ($all_hokms as $hokm)
{
$one_page_ahkams[] = array(
'hokm_id_inPage' => $hokm['hokm_id_inPage'],
'type' => $page['type'],
);
}
else
$one_page_ahkams[] = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams), //Here Is the PROBLEM : (((
);
}
else
$all_pages_data[] = array();
echo json_encode($all_pages_data);
As you see, I want to send multidimensial array() in a json format (from server to client), but how can correct the insertion of an array in an other, I mean that I need to add $one_page_ahkams array in $all_pages_data one, any ideas
$one_page_ahkams = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams);//this comma (,) causes the problem i guess**
);
An other way to do it :
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array(['one_page_ahkams']=>$one_page_ahkams);
);
Just put that: 'ahkams' => array($one_page_ahkams), but check the content of $one_page_ahkams

Can you compare an array of strings in PHP and if there are two that are the same it will only return it once?

Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both. I dont know where to add the array_unique() I have added it to my script but it doesnt seem to work properlly, instead it is taking out all the strings with the same value Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both
//Get slider data from theme options
$company1 = $data['md_media_company_img1'];
$company2 = $data['md_media_company_img2'];
$company3 = $data['md_media_company_img3'];
$company4 = $data['md_media_company_img4'];
$company5 = $data['md_media_company_img5'];
$company6 = $data['md_media_company_img6'];
$company7 = $data['md_media_company_img7'];
$company8 = $data['md_media_company_img8'];
$company9 = $data['md_media_company_img9'];
$company10 = $data['md_media_company_img10'];
$company11 = $data['md_media_company_img11'];
$company12 = $data['md_media_company_img12'];
/*Slides Array*/
$company_name = array(
'company1' => array(
'name' => $company1,
),
'company2' => array(
'name' => $company2,
),
'company3' => array(
'name' => $company3,
),
'company4' => array(
'name' => $company4,
),
'company5' => array(
'name' => $company5,
),
'company6' => array(
'name' => $company6,
),
'company7' => array(
'name' => $company7,
),
'company8' => array(
'name' => $company8,
),
'company9' => array(
'name' => $company9,
),
'company10' => array(
'name' => $company10,
),
'company11' => array(
'name' => $company11,
),
'company12' => array(
'name' => $company12,
)
);
/*check if exist slide*/
$check_exist_company = 0;
$result = array_unique($company_name);
foreach($result as $company => $value) {
if (!empty ($value['name'])){
$check_exist_company = 1;
}
}
?>
<?php if($check_exist_company == 1) {// check if any slide image added in theme option, return custom slide?>
<?php $i = 1; ?>
<?php foreach($company_name as $company => $value) {
if (!empty ($value['name'])) {?>
<li><a class="nivoLink4" rel="<?php echo $i;?>" href="#"><?php echo $value['name'];?></a></li>
<?php ++$i ?>
<?php } ?>
<?php }?>
<?php } ?>
<!--/slider-->
You could just run array_unique() on the source array and just iterate over the result.

Categories