Find Duplicate Multidimensional Array and remove from array php - php

How to remove multi dimensional array if duplicate.
In this example Barcode is duplicate value of 111 i want to remove this if found duplicate. PLease help Im new to php. Thanks
Output:
Array
(
[0] => Array
(
[Barcode] => 111
[Transaction_No] => 256
)
[1] => Array
(
[Barcode] => 111
[Transaction_No] => 0
)
[2] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)
Expected Output:
Array
(
[0] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)

This keeps track of the keys of each barcode array item to find duplicates, then uses array_values at the end to fix the array indexing.
<?php
$myArray = array(
array
(
"Barcode" => 111,
"Transaction_No" => 256
),
array
(
"Barcode" => 111,
"Transaction_No" => 0
),
array
(
"Barcode" => 222,
"Transaction_No" => 0
)
);
$barcodeKeys = array();
foreach ($myArray as $key => $arr) {
$code = $arr["Barcode"];
if (!isset($barcodeKeys[$code])) {
$barcodeKeys[$code] = array();
}
$barcodeKeys[$code][] = $key;
if (count($barcodeKeys[$code]) > 1) {
foreach ($barcodeKeys[$code] as $dupKey) {
if (isset($myArray[$dupKey])) {
unset($myArray[$dupKey]);
}
}
}
}
$myArray = array_values($myArray);
print_r($myArray);
Output
Array
(
[0] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)

Fast approach to your question:
<?php
$barcodes = array(array( 'Barcode' => 111,'Transaction_No' => 256),array('Barcode' => 111,'Transaction_No' => 0),array('Barcode' => 222,'Transaction_No' => 0),array('Barcode' => 333,'Transaction_No' => 0));
$result = array();
$exist = array();
foreach($barcodes as $key => $item){
if( in_array( $item['Barcode'], array_values( $exixt ) ){
unset( $result[ array_search ( $item['Barcode'], $exist ) ] );
} else {
$result[ $key ] = array('Barcode' => $item['Barcode'],'Transaction_No' => $item['Transaction_No'] );
$exist[ $item['Barcode'] ] = $key;
}
}
var_dump($result);

Related

Compare values using strpos - PHP

I am trying to return the user_id if value matches with any comma separated value, I am using strpos but I don't why is it not working with 3rd case:
To Compare: (This value is stored in $myArray variable)
Array
(
[0] => cloud
[1] => ai
[2] => test
)
Compare with: (This value is stored in $array_meta_values variable)
Array
(
[0] => Array
(
[tags] => cloud,ai
[user_id] => 1
)
[1] => Array
(
[tags] => cloud,ai,test
[user_id] => 108
)
[2] => Array
(
[tags] => storage,backup,ai
[user_id] => 101
)
)
function searchForId($meta_value, $array)
{
foreach ($array as $key => $val) {
if (strpos($val['tags'], $meta_value)) {
return $val['user_id'];
}
}
}
foreach ($myArray as $usertags) {
$userids[] = searchForId($usertags, $array_meta_values);
}
print_r($userids);
Getting this Output:
Array
(
[1] => 1
[2] => 108
)
It was supposed to add 101 as third element in output array but don't know why it is not working.
Any help appreciated.
Hi so I tried to replicate you question,
$existing = ['cloud', 'ai', 'test'];
$checker = [
array(
"tags" => "cloud,ai",
"user_id" => 1
),
array(
"tags" => "cloud,ai,test",
"user_id" => 108
),
array(
"tags" => "storage,backup,ai",
"user_id" => 101
)
];
function searchForId($meta_value, $array)
{
$ret_array = [];
foreach ($array as $val) {
$et = explode(",", $val['tags']);
if (in_array($meta_value, $et)) {
$ret_array[] = $val['user_id'];
}
}
return $ret_array;
}
foreach ($existing as $usertags) {
$userids[$usertags][] = searchForId($usertags, $checker);
}
echo "<pre>";
print_r($userids);
Is this what you looking for?
Your question is quite unclear, but I'll try with this one:
<?php
$myArray = array('cloud', 'ai', 'test');
$array_meta_values = array(
array(
'tags' => 'cloud,ai',
'user_id' => 1,
),
array(
'tags' => 'cloud,ai,test',
'user_id' => 108,
),
array(
'tags' => 'storage,backup,ai',
'user_id' => 101,
),
);
$userids = [];
foreach ($myArray as $value) {
foreach ($array_meta_values as $array) {
$arrayValues = explode(',', $array['tags']);
if (in_array($value, $arrayValues)) {
$userids[$value][] = $array['user_id'];
}
}
}
echo '<pre>';
print_r($userids);
Output:
Array
(
[cloud] => Array
(
[0] => 1
[1] => 108
)
[ai] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
[test] => Array
(
[0] => 108
)
)

How to prepare a new array out of two arrays inside foreach loop?

I am trying to compare the keys of an array with values of another array. In case of greater values of the current key, I would like to push the value into a new array. Then I want to insert all the collected values for that specific key into a DB table.
These are my input arrays:
$productlist = [result] => Array
(
[0] => Array
(
[configoption1] => 2M
[id] => 96
)
[1] => Array
(
[configoption1] => 5M
[id] => 97
)
[2] => Array
(
[configoption1] => 15M
[id] => 98
)
[3] => Array
(
[configoption1] => 30M
[id] => 99
)
)
$myplans = Array
(
[2M] => Array
(
[price] => 10
)
[5M] => Array
(
[price] => 10
)
[15M] => Array
(
[price] => 10
)
[30M] => Array
(
[price] => 10
)
)
The following is my sample code:
$upgradelist = array()
foreach ($myplans as $plan => $data) {
foreach($productlist['result'] as $key=>$value){
if($plan == 'ENTERPRISE'){
//no higher plans than Enterprise
}else{
$plan1 = (int)substr_replace($plan, "", -1);
$value['configoption1'] = (int)substr_replace($value['configoption1'], "", -1);
#echo " configconfig=> ".$value['configoption1'];
if($plan > $value['configoption1']){
$upgrade_product_ids[$plan][] = $value['id'];
}
}
}
//insert upgrade products list
if(!empty($upgradelist)){
foreach($upgradelist as $key => $upgrade_product_id){
#$insert_stmt_upgradeproduct = <insert statement> for each $plan
}
}
}
expected output each time I come out from the foreach loop: > foreach($productlist['result']
$upgradelist = Array
(
[2] => Array
(
[0] => 97 //5M
[1] => 98 //15M
[2] => 99 //30M
)
)
$upgradelist = Array
(
[5] => Array
(
[0] => 98
[1] => 99
)
)
$upgradelist = Array
(
[15] => Array
(
[0] => 99
)
)
$upgradelist = Array
(
[30] => Array
(
)
)
You can solve like below:
foreach($myplans as $key => $plan) {
$data_to_be_inserted = [];
foreach($productlist['result'] as $product) {
if ($key > $product['configoption1']) {
$data_to_be_inserted [] = $product['id'];
}
}
if (count($data_to_be_inserted)) {
//insert into database(expected value will be available here)
}
}
YOu can do like this
<?php
$myArray = [];
$myArray['result'] = array(
array("configoption1" => "2m", "id" => 96),
array("configoption1" => "5m", "id" => 97),
array("configoption1" => "15m", "id" => 98),
array("configoption1" => "30m", "id" => 99)
);
//print_r($myArray);
$myArray2 =[
"2m" => ["price" => 10],
"5m" => ["price" => 10],
"15m" => ["price" => 10],
"30m" => ["price" => 10]
];
$resultArray = [];
foreach($myArray2 as $key => $values) {
$keyNum = (int)$key;
$newTempArray[$keyNum] = [];
foreach($myArray['result'] as $newresult) {
$configoption1Num = (int)$newresult['configoption1'];
if($configoption1Num > $keyNum) {
array_push($newTempArray[$keyNum], $newresult['id']);
}
}
print_r($newTempArray[$keyNum]);
echo "<br>";
}
http://sandbox.onlinephpfunctions.com/code/008c839fb4fad4ed2aceae7c4ed1220579e8e318

Get common values from all index in an array

I have an array like this,
Array
(
[0] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 179
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 182
)
)
[1] => Array
(
[0] => Array
(
[productId] => 147
[variantId] => 181
)
[1] => Array
(
[productId] => 147
[variantId] => 184
)
)
[2] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 178
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 185
)
[4] => Array
(
[productId] => 147
[variantId] => 186
)
)
)
For this input we can see that [productId] => 147,[variantId] => 181 is common an all index of array.
Im using foreach to check this. But my code is working fine for 2 index, but not for more than 2 index.
In foreach im taking 0th index and comparing with other index, but not working.
Here is the code i have used so far.
$first = $name1[0];
$a = array();
unset($name1[0]);
foreach ($name1 as $row) {// foreach main array
foreach ($first as $row1) {// foreach first index of array array
foreach ($row as $kkk => $r1) {//foreach index of main array
// if first array's index's product is is equal to main array index's product id, push values to one array
if ($row1['productId'] == $r1['productId']) {
if (isset($row1['variantId']) && isset($r1['variantId'])) {
if ($row1['variantId'] == $r1['variantId']) {
$a[] = $r1;
}
} else {
$a[] = $r1;
}
}
}
}
}
First method
Use this, PHP functions are faster than trying to build your own algorithm.
$result = array_intersect($array1, $array2);
Second Method
$array1 = array("5","26","38","42");
$array2 = array("15","36","38","42");
$final_array = array();
foreach($array1 as $key=>$val){
if(in_array($val,$array2)){
$final_array[] = $val;
}
}
print_r($final_array);
Result: Array ( [0] => 38 [1] => 42 )
for 2d array
function multi_intersect($arr) {
$return = array();
foreach ($arr as $a) {
foreach ($arr as $b) {
if ($a === $b) continue;
$return = array_merge($return, array_intersect($a, $b));
}
}
return array_unique($return);
}
you should get
Array
(
[0] => 58
)
so finally you use this to get result
$intersect = call_user_func_array('array_intersect', $arr);
You can use foreach inside a foreach loop
$my_array = array
(
'0' => array
(
'0' => array
(
'productId' => 145,
'variantId' => 179
),
'1' => array
(
'productId' => 145,
'variantId' => 180
),
'2' => array
(
'productId' => 147,
'variantId' => 181
),
'3' => array
(
'productId' => 147,
'variantId' => 182
)
),
'1' => array
(
'0' => array
(
'productId' => 147,
'variantId' => 181
),
'1' => array
(
'productId' => 147,
'variantId' => 184
)
),
'2' => array
(
'0' => array
(
'productId' => 145,
'variantId' => 178
),
'1' => array
(
'productId' => 145,
'variantId' => 180
),
'2' => array
(
'productId' => 147,
'variantId' => 181
),
'3' => array
(
'productId' => 147,
'variantId' => 185
),
'4' => array
(
'productId' => 147,
'variantId' => 186
)
)
);
$final_array = array();
foreach($my_array as $nested_array){
foreach($nested_array as $val ){
$final_array[] = $val;
}
}
echo "<pre>"; print_r($final_array);
Here is the answer which i wanted,
function serialize_array_values($arr){
foreach($arr as $key=>$val){
sort($val);
$arr[$key]=serialize($val);
}
return $arr;
}
$result = array_map("unserialize", array_intersect(
serialize_array_values($name1[0]),
serialize_array_values($name1[1]),
serialize_array_values($name1[2])
));
https://eval.in/738165, check the working link
Reference : array_intersect throws errors when arrays have sub-arrays
Finally here is my working code
foreach ($name1 as $kkk => $row) {
$list[] = $this->serializeArrayValues($row);
}
$intersect = call_user_func_array('array_intersect', $list);
$a = array_map("unserialize", $intersect);
function serializeArrayValues($arr) {
foreach ($arr as $key => $val) {
$arr[$key] = serialize($val);
}
return $arr;
}

How to count same keys data from array in PHP

I have array like below I want to convert it as per requirement
Array
(
[0] => Array
(
[2017-01-23] => 5885e363e4b0e341d3e47fc3
)
[1] => Array
(
[2017-01-23] => 5885e4f9e4b0e341d3e47fd7
)
[2] => Array
(
[2017-01-24] => 5885e363e4b0e341d3e47fc3
)
[3] => Array
(
[2017-01-24] => 5885e4f9e4b0e341d3e47fd7
)
)
I want count of same key data as in output. That is I want to count of same keys so that i want to plot it on graph
Array
(
[0]=>Array
(
[2017-01-23]=> 2
)
[1]=>Array
(
[2017-01-24]=> 2
)
)
Try this:
$temp = array();
$array = array(
'0' => Array
(
'333' => 2,
'222' => 5,
'444' => 4
),
'1' => Array
(
'111' => 2
),
'2' => Array
(
'111' => 5,
'222' => 9
)
);
foreach ($array as $key => $value)
{
foreach ($value as $k => $v)
{
if (!isset($temp[$k]))
{
$temp[$k] = 1;
}
else
{
$temp[$k] = $temp[$k]+1;
}
}
}
echo "<pre>";
print_r($temp);
die;
Output :
Array
(
[333] => 1
[222] => 2
[444] => 1
[111] => 2
)

Removing duplicates from multidimensional array based on the first elements only PHP

This is the array
Array
(
[0] => Array
(
[artist] => John Keys
[postID] => 254
)
[1] => Array
(
[artist] => Jay Bloom
[postID] => 249
)
[2] => Array
(
[artist] => John Keys
[postID] => 216
)
[3] => Array
(
[artist] => Angie Belle
[postID] => 225
)
)
and I want to remove all duplicates from the artist elements only. Notice that 0 and 2 have the same artist but a different postID. I just want to keep the first occurence of the artist and remove all others. So the result I want to have is
Array
(
[0] => Array
(
[artist] => John Keys
[postID] => 254
)
[1] => Array
(
[artist] => Jay Bloom
[postID] => 249
)
[2] => Array
(
[artist] => Angie Belle
[postID] => 225
)
)
I've tried array_unique and also serializing and doing an array_map, nothing seems to work right.
<?php
$array = Array
(
'0' => Array
(
'artist' => 'John Keys',
'postID' => 254
),
'1' => Array
(
'artist' => 'Jay Bloom',
'postID' => 249
),
'2' => Array
(
'artist' => 'John Keys',
'postID' => 216
),
'3' => Array
(
'artist' => 'Angie Belle',
'postID' => 225
)
);
$newarray = array();
foreach($array as $key => $val){
if(!array_key_exists('artist', $newarray)){
$newarray[$val['artist']] = $val;
}
}
echo '<pre>';
print_r($newarray);
Edit: using numeric keys:
$newarray = array();
$temp = array();
foreach($array as $key => $val){
if(!in_array($val['artist'], $temp)){
$temp[] = $val['artist'];
}
if( !array_key_exists(array_search($val['artist'], $temp), $newarray) ){
$newarray[array_search($val['artist'], $temp)] = $val;
}
}
Simple approach:
$result = array();
foreach ($input as $item) {
if (!array_key_exists($item['artist'], $result)) {
$result[$item['artist']] = $item;
}
}
<?php
$a=array(
"0" => array
(
"artist" => "John Keys",
"postID" => "254"
),
"1" => array
(
"artist" => "Jay Bloom",
"postID" => "249"
),
"2" => array
(
"artist" => "John Keys",
"postID" => "216"
),
"3" => array
(
"artist" => "Angie Belle",
"postID" => "225"
)
);
var_dump($a);
for($i=0;$i<count($a);$i++)
{
for($j=$i+1;$j<count($a); $j++)
{
$tmp1=$a[$i]["artist"];
$tmp2=$a[$j]["artist"];
if($tmp1==$tmp2)
{
unset($a[$j]);
}
$tmp3=array_values($a);
$a=$tmp3;
}
}
var_dump($a);
?>

Categories