get unique data from multi-dimensional array list - php

I have 2 records from 2 different query results. I have merged those data. but i have to display unique data depends on vName key.
Here is my result:
{
"val1": "9",
"vName": "abc",
"bname": "",
"cname": "",
"brid": "",
"cmpid": "154",
"logo": "",
"banner": "",
"description": "",
"catids": "",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "",
"compressthumb_logo": "",
"bigthumb_banner": "",
"compressthumb_banner": "",
"sp_ar": "Aperitif"
},
{
"val1": "9",
"vName": "abc",
"bname": "abc",
"cname": "abc",
"brid": "341",
"cmpid": "154",
"logo": "",
"banner": "6c497c72bfec4c694c1cc2c49066e7a1.png",
"description": "http:\/\/www.abc.com\/",
"catids": "9",
"sp_ar": "Aperitif",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "40cbcede9429cdb44895e0e6f4c050d2.png",
"compressthumb_logo": "40cbcede9429cdb44895e0e6f4c050d2.png",
"bigthumb_banner": "6c497c72bfec4c694c1cc2c49066e7a1.png",
"compressthumb_banner": "6c497c72bfec4c694c1cc2c49066e7a1.png"
}
Expected result should be only one of them, compare with vName:
{
"val1": "9",
"vName": "abc",
"bname": "",
"cname": "",
"brid": "",
"cmpid": "154",
"logo": "",
"banner": "",
"description": "",
"catids": "",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "",
"compressthumb_logo": "",
"bigthumb_banner": "",
"compressthumb_banner": "",
"sp_ar": "Aperitif"
}
How can i get the result?

Convert the data into an array where the key is your unique value.
You can do something like:
$data = array('this is your array of data');
$unique = array();
foreach($data as $value) {
if(!array_key_exists($value['vName'], $unique)) {
$unique[$value['vName']] = $value;
}
}
// $unique now contains only arrays with a unique 'vName', using the first it finds in the original list
$new_array = array_values($unique);

Assuming you have a array of associative array stored in $data, the following code will filter it based on vName and store array of associative arrays with unique vName in $filtered_data
$taken = array();
$filtered_data = array_filter($data, function($value) {
global $taken;
if (in_array($value['vName'], $taken)) {
return false;
}
else {
$taken[] = $value['vName'];
return true;
}
});
Here, the variable $taken keeps track of all the vName values that are currently in the $filtered_data. If you find a duplicate vName (that is currently there in $taken), it discards that element.

Try this, it should work for you.
$input = array_map("unserialize", array_unique(array_map("serialize",$input)));

Related

How to Parse the dynamic key vale in php

how to manage dynamic key in json
[
{
"batch_id": "132",
"benf_id": "ANP-JAI-1190",
"NA3": "",
"NA4": "",
"status": "Y",
"NA5": "",
"datapoint_id": "105",
"status_update_time": "28/8/2017 ",
"user_id": "santoshk",
"pop_id": "40",
"measuring": "1000ltr",
"weight": "100gm"
},
{
"batch_id": "133",
"benf_id": "ANP-JAI-1195",
"NA3": "",
"NA4": "",
"status": "Y",
"NA5": "",
"datapoint_id": "106",
"status_update_time": "28/8/2017 ",
"user_id": "santoshk",
"pop_id": "41",
"water": "1000ltr",
"medicine": "100gm"
}
]
// my php code
public function benf_status_update($jdata)
{
$count=0;
$this->load->model('m_sf_user');
while($count < count($jdata))
{
$batch_id = $jdata[$count]->batch_id;
$benf_id = $jdata[$count]->benf_id;
$m3=$jdata[$count]->NA3;
$m4=$jdata[$count]->NA4;
$status = $jdata[$count]->status;
$m5=$jdata[$count]->NA5;
$m1=$jdata[$count]->0;
$m2=$jdata[$count]->0;
$datapoint_id = $jdata[$count]->datapoint_id;
$status_update_time = $jdata[$count]->status_update_time;
$user_id =$jdata[$count]->user_id;
$pop_id = $jdata[$count]->pop_id;
$benf_id = substr($benf_id,8,strlen($benf_id));
$qry = "UPDATE pop_b_m_points a JOIN m_pop_batch b ON
b.pop_id=".$pop_id."
and b.batch_id=".$batch_id."
and a.data_p_id=b.dpoint_id
and a.benf_id=".$benf_id."
and a.m_point_id=".$datapoint_id."
SET a.status='".$status."',
a.m1='".$m1."',
a.m2='".$m2."',
a.m3='".$m3."',
a.m4='".$m4."',
a.m5='".$m5."' ,
a.update_time= (SELECT (DATE_FORMAT(STR_TO_DATE('".$status_update_time."', '%d/%m/%Y' ), '%Y%m%d')))";
$this->m_sf_user->jSONservices_u($qry);
$count++;
}
$x=array('json_status'=>"success");
echo json_encode($x);
}
You can simply use json_decode
json_decode($string) to convert String into Array/Object (stdClass).
Example:
$myRawJson = '{
"batch_id": "133",
"benf_id": "ANP-JAI-1195",
"NA3": "",
"NA4": "",
"status": "Y",
"NA5": "",
"datapoint_id": "106",
"status_update_time": "28/8/2017 ",
"user_id": "santoshk",
"pop_id": "41",
"water": "1000ltr",
"medicine": "100gm"
}';
$myAssoc = json_decode($myRawJson);
$myObject = (object)(json_decode($myRawJson));
var_dump($myAssoc['medicine']); //100gm
var_dump($myObject->medicine); //100gm
If you don't know for sure which columns you will get, but do want to save all. Then make an extra column 'data' in which you save the complete set of data through json_encode()
If you can make sure some fields are always there, use them in seperate columns in order to filter and order by those, but also use the data column to add the complete data set.

How to retrieve values from multilevel json

Im trying to get the values from the various sections of this json entry.
{
"gallery_show": "1",
"gallery": {
"path": ["images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg"],
"title": ["", "", "", "", "", ""],
"caption": ["", "", "", "", "", ""],
"thumb": ["\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg"]
},
"videos_show": "1",
"videos": {
"title": ["Arnie", "Arnie", "Arnie", "Arnie", "Arnie"],
"sharelink": ["http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY"]
},
"attachments_show": "1",
"attachments": {
"path": ["images\/slide2.jpg", "images\/slide1.jpg"],
"title": ["Attchment", "Attchment"],
"caption": ["Attachment Description", "Attachment Description"]
},
"links_show": "1",
"links": {
"title": ["Link1", "Link2"],
"httplink": ["http:\/\/www.mydomain.com", "http:\/\/www.mydomain.com"],
"targetlink": ["_blank", "_blank"]
}
}
If i use this method
$entries= json_decode($this->item->entries);
and then i echo this
echo $entries->gallery_show;
I get the desired result = 1
But how do i now get the things beneath it if gallery_show=1 and display them as values i can use inside my php?
Any help for this old brain would be much appreciated.
Cheers in advance
Jonny
You could do somewhat like this...
$arr = json_decode($json);
foreach($arr as $k=>$v)
{
if($k=='gallery_show' && $v==1)
{
foreach($arr->gallery->path as $path)
{
echo "<img src=$path />";
}
break;
}
}
Demo
Code update..
$arr = json_decode($json);
$i=0;
foreach($arr as $k=>$v)
{
if($k=='gallery_show' && $v==1)
{
foreach($arr->gallery as $gall)
{
echo "Title :".$arr->gallery->title[$i]."<br/>";
echo "Caption :".$arr->gallery->caption[$i]."<br/>";
echo "Thumbnail :".$arr->gallery->thumb[$i]."<br>";
echo "<img src=".$arr->gallery->path[$i]." /><br>";
$i++;
}
break;
}
}
Demo

pushed encoded json into another json

I'd encoded my result into json and I want to merge them. I wish to achieve this
[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
"img_src" [{img_src:1.jpg},{img_src:2.jpg}]
}
]
http://pastebin.com/7jKp9BUn
the result of each statement
[
{
"img_src": "1.jpg"
},
{
"img_src": "2.jpg"
}
]
the next one
[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
}
]
You can decode the JSON into arrays, append one to the other, then re-encode:
$s1 = '[
{
"img_src": "1.jpg"
},
{
"img_src": "2.jpg"
}
]';
$s2 = '[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
}
]';
//decode each
$arr1 = json_decode($s1, true);
$arr2 = json_decode($s2, true);
$arr2[0]['img_src'] = $arr1;
//re-encode as a JSON string and show output
$sf = json_encode($arr2);
echo $sf;
This should give the result you described, with "img_src"... as a sub-array of the larger set.
Looking at your php code you could do it this way as well in one shot.
$stmt = $db->prepare("
SELECT post_id,content,date,category_id,lp_title,lp_image,lp_canonicalUrl,lp_url,lp_desc,lp_iframe,lp_iframe_id
FROM post_items inner JOIN user ON post_items.user_id = user.user_id
WHERE post_items.user_id = ? order by post_items.post_id desc LIMIT ?,?");
$stmt->bind_param('iii', $userid, $itemStart, $itemEnd);
$stmt2 = $db->prepare("SELECT photo_upload.img_src FROM photo_upload inner JOIN post_items ON post_items.photo_set_id = photo_upload.photo_set_id
WHERE post_items.photo_set_id = photo_upload.photo_set_id ");
//store the images in a separate array
$imgArray = array();
if ($stmt2->execute()) {
$photo_items = $stmt2->get_result();
while ($obj2 = $photo_items->fetch_object()) {
$imgArray[] = $obj2;
}
}
if($stmt->execute()){
$post_items = $stmt->get_result();
if(mysqli_num_rows($post_items) > 0){
while ($obj = $post_items->fetch_object()) {
$jsonData[] = $obj;
// check if there are any elements in the array
if(count($imgArray) > 0){
// set the img_src index of the jsonData array with the elements of the other array
$jsonData['img_src'] = $imgArray;
}
}
$i = json_encode($jsonData);
echo $i;
}
}

How can I edit my json to not include the row numbers

php to connect and get all rows
$query = 'SELECT * FROM cdr';
$sql = $remotedbh->prepare($query);
$sql->execute();
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
if($sql->rowCount() > 0){
header('Content-type: application/json');
echo json_encode($dblist,JSON_FORCE_OBJECT);
}
else {
echo 0;
}
The problem is my json looks like this
{
"0": {
"calldate": "2013-08-14 11:41:28",
"clid": "\"tet\" <1002>",
"src": "1002",
"dst": "8834404",
"dcontext": "from-internal",
"channel": "SIP\/1002-00000000",
"dstchannel": "IAX2\/voipms-6749",
"lastapp": "Dial",
"lastdata": "IAX2\/voipms\/14798834404,300,",
"duration": "7",
"billsec": "0",
"disposition": "NO ANSWER",
"amaflags": "3",
"accountcode": "",
"uniqueid": "1376498488.1",
"userfield": "",
"did": "",
"recordingfile": "",
"cnum": "",
"cnam": "",
"outbound_cnum": "",
"outbound_cnam": "",
"dst_cnam": ""
},
"1": {
"calldate": "2013-08-14 11:42:55",
"clid": "\"Rtest\" <1002>",
"src": "1002",
"dst": "9187755592",
"dcontext": "from-internal",
"channel": "SIP\/1002-00000001",
"dstchannel": "IAX2\/voipms-121",
"lastapp": "Dial",
"lastdata": "IAX2\/voipms\/19187755592,300,",
"duration": "494",
"billsec": "485",
"disposition": "ANSWERED",
"amaflags": "3",
"accountcode": "",
"uniqueid": "1376498575.3",
"userfield": "",
"did": "",
"recordingfile": "",
"cnum": "",
"cnam": "",
"outbound_cnum": "",
"outbound_cnam": "",
"dst_cnam": ""
},
"2": {
so on so forth
I also am having the problem of everytime it pulls all the information a row is only half complete so it has a problem with that aswell, I was thinking of finding away to query the everything that is 5 minutes or older.
It's including the row numbers as you're using JSON_FORCE_OBJECT
Remove it and it it'll successfully return as an array.
e.g.
echo json_encode($dblist);
JSON_FORCE_OBJECT option in json_encode() forces the array to a json object with its indexes as keys. Removing that might get working for you.
update :
because while testing with a sample array
$test = array(array("calldate"=> "2013-08-14","src"=> "1002"),
array("calldate"=> "2013-08-18","src"=> "1003")
);
echo json_encode($test,JSON_FORCE_OBJECT);
//Outputs :
//{"0":{"calldate":"2013-08-14","src":"1002"},"1":{"calldate":"2013-08-18","src":"1003"}}
and
echo json_encode($test);
//Outputs :
// Gave me [{"calldate":"2013-08-14","src":"1002"},{"calldate":"2013-08-18","src":"1003"}]
so using just
echo json_encode($dblist);
should do the trick

Mootools get first value from each

I have one array images from Json:
images=JSON.decode(images);
This is the array value :
[
{
"title": "",
"description": "",
"name": "loc commerciale.jpg"
},
{
"title": "",
"description": "",
"name": "foto2.jpg"
},
{
"title": "",
"description": "",
"name": "foto 1.jpg"
},
{
"title": "",
"description": "",
"name": "a01.jpg"
}
]
I get the value from name :
images.each(function(image){
alert(image.name);
});
I need to get only the first value name
Like php :
$images = (array) json_decode($row->images);
$first = true;
foreach ($images as $k => $image) {
if ($first)
{
$firstimage= $image->name;
$first = false;
}
}
Try:
if (images[0].length){
var firstImageName = images[0].name;
}

Categories