Remove duplicate data based on postID - php

I have an array which is as follows:
Array
(
[0] => Array
(
[postId] => 105
[postTitle] => Test
[postNonArray] => Subzero
[postDesc] => Array
(
[0] => Array
(
[para] => Subzero
[align] => L
)
)
[postDate] => 25.08.2016
[postTime] => 13:44
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472112857.png
[postVideo] =>
)
[1] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
[2] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
)
As you can see, there is two post details with postId=106;
How can I remove the redundant data from the array based on postId?
The project is on PHP.

I think this is what you are trying to achieve:-
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "<pre/>";print_r($array);
Check output(whole code with your original array):- https://eval.in/630678
Note:- It will remove the duplicate values (so whole duplicate array will gone as you asked in comment)

I would suggest loop like the one below. It will go through all the elements from $your_array_name and will make an unique array of id where we will store the postIds. We will also check if there are duplicated in the $unique_ids array, and if so we will remove that duplicate element.
$unique_ids = array();
foreach($your_array_name as $key => $value){
//check if the postId is in the array of the unique ids
if(!in_array($value['postId'], $unique_ids)()){
array_push($unique_ids,$value['postId']); //if it is not - push it there
} else {
unset($your_array_name($key)); //if it is - remove the whole element from the array
}
}

You will need to loop the data and create a new array with unique values so here you go:
$ShowNewArray = array();
foreach($array as $key => $value){
if(!array_key_exists('postId', $ShowNewArray)){
$ShowNewArray[$value['postId']] = $value;
}
}
print_r($ShowNewArray);
Hope it will help you.

Related

Looping through 2 arrays and retrieve the values from the first array if the id matches with the second array. (PHP)

I've been trying to figure out how I would be able to loop through two arrays and match values from each array which should only return the value that matches of the first array.
Array 1:
[0] => Array
(
[contact] => 68
[field] => 11
[value] => DBSA
[cdate] => 2019-11-14T11:21:08-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
[1] => Array
(
[contact] => 68
[field] => 131
[value] => ABC
[cdate] => 2019-11-22T08:34:03-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
Array 2:
[0] => Array
(
[source] => analysis_utm_source
[id] => 131
)
[1] => Array
(
[source] => analysis_utm_medium
[destination] => UTM medium
[id] => 132
)
So in this example I would like to retrieve the values from the first array but only where id/field = 131
I've tried to work with 'array_intersect' but this doesn't seem to give the right output.
I'd appreciate it if someone would be able to push me in the right direction.
EDIT: I've been able to solve it by using array_filter, final code like so;
foreach ($fieldValuesDB as $arr) {
$options[] = $arr['id'];
}
$result = array_filter($fieldValuesAC, function($v) use ($options) {
return in_array($v['field'], $options);
});
foreach($array2 as $k => $v)
{
$key = array_search($v['id'], array_column($array1, 'field'));
if($key){
echo print_r($array1[$key]);
}
}
you will get the index of the element in your first array stored in $key variable

Manipulation or modification of array

I'm new to PHP. I'm doing one my project with php and I'm new to array functions and all those things. I have tried but not get success in that. let me show you my sql query array.
I have one my array which is as below:
Array
(
[0] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[1] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[2] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[3] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[4] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[5] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Cancer Specialist
)
[6] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Breach Candy Hospital
)
)
Now I want my resulted array as below :
Array
(
[2016-08-25] => Array
(
[ Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 2
)
)
)
[2016-08-26] => Array
(
[Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 1
)
)
[Cancer Specialist] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
[Breach Candy Hospital] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
)
)
you want to loop through your appointments array and use its contents to generate the other data structure. let's call your first array $input and your second array $output:
// initialize output array
$output = [];
// loop through each $appt in the $input array
foreach($input as $appt) {
// get shorter var names for appt data
$date = $appt['pc_eventDate'];
$name = $appt['name'];
$uname = $appt['ufname'].' '.$appt['ulname'];
// initialize each level of the data structure if it doesn't already exist
if(!isset($output[$date])) $output[$date] = [];
if(!isset($output[$date][$name])) $output[$date][$name] = [];
if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
// initialize the number of appts to 0
if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
// increment the number of appts
$output[$date][$name][$uname]['appointments']++;
}
the important thing is the intialization of each sub-array in the new structure according to the data in the old structure -- from there we're just counting the number of appointments that match the new data.
good luck!
Say the array is question is $arr
This will arrange the array in the way you wanted as solution
foreach ($arr as $key => $value) {
if(!is_array($value['pc_eventDate]))
$value['pc_eventDate] = [];
if(!is_array($value['pc_eventDate]['name']))
$value['pc_eventDate]['name'] = [];
if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
$value['pc_eventDate']['name']['ufname'.' ulname'] = [];
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
}else{
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
}
}
You need to do something like the above.
Try running the above code, there could be some typo. If its doesnt yields your desired result, try commenting out all the lines in the body of the foreach and var_dump() each step to test the building of the array structure.
Thanks

replace json string using regex in PHP

How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace
but i don't know how to add the regex
Please help me.
here my json
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
will i use this function to export the json to csv
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column
I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column....
so for the experiment i add this to the above code but seems nothing happen
$flat[$k] = str_replace('[1-7] => http', "http", $v);
here i expect the output something like that
....
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
http://example.com/images/user_adv/144.jpg
http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
http://example.com/images/user_adv/small/144.jpg
http://example.com/images/user_adv/small/147.jpg
)
.....
edit this the .csv file output look like this
and I'm looking to be something like that
ok I've fixed by
foreach (
new RecursiveArrayIterator($nonFlat) as $k=>$v) {
$flat[$k] = is_array($v)?implode(" ",$v):$v;
now i got each group of images on one column
thanks :)

Compare 2 Arrays and get matching items

I have a multidimensional array that is displayed to users in a table, where they can select items by a checkbox.
When they've checked their items and submit, I've now got an array of id values that correspond to the myid key of the original sub arrays.
How can I search the original array and create a new array of only the matching selected items?
Array (
[0] => Array (
[myid] => 22
[Price] => Some price
[Title] => Some text
)
[1] => Array (
[myid] => 36
[Price] => Some price
[Title] => Some text
)
)
Any help would be greatly appreciated!
Simple way but can be optimized
<?php
$submittedVaule = array('12','14');
$subArray = array(0 => array('myid' => 12,'price' => '100','title' => 'test1'),1 => array('myid' => 13,'price' => '100','title' => 'test2'),2 => array('myid' => 14,'price' => '100','title' => 'test3'));
$finalarray = array();
foreach($subArray as $key=>$value){
if(in_array($value['myid'], $submittedVaule )) {
$finalarray[]=$subArray[$key];
}
}
print_r($finalarray);
?>

Array unique multidimensional on one column

I have a big array like this this one (only 5 entries here)
Array
(
[1] => Array
(
[last_token] => atokenforexample
[idmembre] => 31800
[key] => 821fbe3f4623649562f75a3de132c908
[idmembre_mobile] => 230106
)
[2] => Array
(
[last_token] => atoken1
[idmembre] => 83586
[key] => 0854ea148c95c08a30c623a313e645a9
[idmembre_mobile] => 126634
)
[3] => Array
(
[last_token] => atoken1
[idmembre] => 138727
[key] => 622c0803a8f662c1df852001017b5db8
[idmembre_mobile] => 119326
)
[4] => Array
(
[last_token] => atokensocool
[idmembre] => 163737
[key] => 8bab884bf46c14abfceaef7abcdf14f9
[idmembre_mobile] => 222066
)
[5] => Array
(
[last_token] => WOWanothetoken
[idmembre] => 236345
[key] => 41c17896091d9417b305e70541039249
I need to get a unique array from this on only one column, the last_token :
So here i need to swipe the array with key 3 (array[3]) because his last_token is the same as array[2][last_token].
Array unique can't work here.. :(
To save the last occurrence:
foreach($array as $value) {
$result[$value['last_token']] = $value;
}
$result = array_values($result);
Or to save the first occurrence:
foreach(array_reverse($array) as $value) {
$result[$value['last_token']] = $value;
}
$result = array_reverse(array_values($result));
Probably a more elegant way but I'm starving and off to eat.

Categories