First value duplicate in array while using array_search function [duplicate] - php

This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
I am trying to remove duplicates from two dimensional array based on client ID. The script removes all duplicates EXCEPT the first value, first client.
I have tried few more ways of comparing result from array_search to different values, trying to use !== and === with no promising result.
Inserting non numeric value as first in the array, makes everything deduplicate flawlessly.
Here is the code:
// Build client list
$ClientList = array();
$counter = 0;
foreach ($ClientTrans as $order => $value) {
$ClientId = $ClientTrans[$order]['customer_id'];
if (array_search($ClientId, array_column($ClientList, 0)) == FALSE && is_numeric($ClientId)) {
$ClientList[$counter][] = $ClientId;
$counter += 1;
}
}
The final result is a client and a sum up value from two dimensional array. Everything works as it should except for the first client, that appears multiple times in the new build client list without duplicates.
Here's the Input Array
Array (
[0] => Array (
[customer_id] => 50245901
[points] => 299
)
[1] => Array (
[customer_id] => 50245907
[points] => 3847
)
[2] => Array (
[customer_id] => 50245908
[points] => 159
)
[3] => Array (
[customer_id] => 50245910
[points] => 3175
)
[4] => Array (
[customer_id] => 50245914
[points] => 641
)
[5] => Array (
[customer_id] => 50245916
[points] => 449
)
[6] => Array (
[customer_id] => 50245921
[points] => 551
)
[7] => Array (
[customer_id] => 50245927
[points] => 0
)
[8] => Array (
[customer_id] => 50245928
[points] => 602
)
[9] => Array (
[customer_id] => 50245929
[points] => 495
)
[10] => Array (
[customer_id] => 50245931
[points] => 539
)
[11] => Array (
[customer_id] => 50245941
[points] => 0
)
[12] => Array (
[customer_id] => 50245901
[points] => 124
)
[13] => Array (
[customer_id] => 50245901
[points] => 512
)
)
And desired output - customer id 50245901 is not appearing multiple times:
Array (
[0] => Array (
[customer_id] => 50245901
)
[1] => Array (
[customer_id] => 50245907
)
[2] => Array (
[customer_id] => 50245908
)
[3] => Array (
[customer_id] => 50245910
)
[4] => Array (
[customer_id] => 50245914
)
[5] => Array (
[customer_id] => 50245916
)
[6] => Array (
[customer_id] => 50245921
)
[7] => Array (
[customer_id] => 50245927
)
[8] => Array (
[customer_id] => 50245928
)
[9] => Array (
[customer_id] => 50245929
)
[10] => Array (
[customer_id] => 50245931
)
)

You need to simplify your foreach() like below:-
$ClientList = array();
foreach ($ClientTrans as $order => $value) {
$ClientList[$value['customer_id']]['customer_id'] = $value['customer_id'];
}
$ClientList = array_values($ClientList);
Output:-https://3v4l.org/f7Bfn

You can simply write code,
$arr = array_values(array_unique(array_column($arr, 'customer_id')));
$temp = [];
array_walk($arr, function(&$item,$key) use(&$temp){
$temp[]['customer_id'] = $item;
});
print_r($temp);
array_values — Return all the values of an array
array_unique — Removes duplicate values from an array
array_column — Return the values from a single column in the input array
array_walk — Apply a user supplied function to every member of an array
Demo.

Related

Unset array key if value is empty

Array
(
[2] => Array
(
[option_id] => 2
[price] => 15
[processor] => Array
(
[3] => Array
(
[processor_id] => 3
[price] => 15
)
[4] => Array
(
[processor_id] => 4
[price] => 15
)
)
)
[3] => Array
(
[option_id] => 3
[price] => 15
[processor] => Array
(
[3] => Array
(
[processor_id] => 3
[price] => 15
)
[4] => Array
(
[processor_id] => 4
[price] => 15
)
)
)
[4] => Array
(
[processor] => Array
(
[3] => Array
(
[price] => // empty value
)
[4] => Array
(
[price] => // empty value
)
)
)
)
I have this array, Now I want to unset the array which array doesn't have any value like in the last array there is no value given so I want to unset the whole array key.
In This array, i have empty value so how can i unset the [4] key. So is it possible without foreach loop.
You can use array_filter for this. As you supplied no code I recommend reading the documentation on php.net.
function filter_function($var) {
// return an expression evaluating to true to keep value
return ($var["option_id"]);
}
$filtered = array_filter($unfiltered, "filter_function");

How to convert PHP multi-dimensional associative array to simpler indexed array [duplicate]

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 5 years ago.
I have a massive multi-dimensional associative array which I need to simplify. Here is an example of the data:
Array ( [0] => Array ( [sku] => 283085 ) [1] => Array ( [sku] =>
283119 ) [2] => Array ( [sku] => 283127 ) [3] => Array ( [sku] =>
283168 ) [4] => Array ( [sku] => 283184 ) [5] => Array ( [sku] =>
283200 ) [6] => Array ( [sku] => 283234 ) [7] => Array ( [sku] =>
283275 ) [8] => Array ( [sku] => 283309 ) [9] => Array ( [sku] =>
283382 ) [10] => Array ( [sku] => 283531 ) [11] => Array ( [sku] =>
283549 ) [12] => Array ( [sku] => 283556 ) [13] => Array ( [sku] =>
283564 ) [14] => Array ( [sku] => 283598 ) [15] => Array ( [sku] =>
507813 ) [16] => Array ( [sku] => 507814 ) [17] => Array ( [sku] =>
507821 ) [18] => Array ( [sku] => 507823 ) [19] => Array ( [sku] =>
507828 ) [20] => Array ( [sku] => 507829 ) [21] => Array ( [sku] =>
507830 ) [22] => Array ( [sku] => 507832 ) [23] => Array ( [sku] =>
507833 ) [24] => Array ( [sku] => 507836 ) [25] => Array ( [sku] =>
508019 ) [26] => Array ( [sku] => 508053 ) [27] => Array ( [sku] =>
3636 ) [28] => Array ( [sku] => 350138 ) [29] => Array ( [sku] =>
350139 ) [30] => Array ( [sku] => 311144 ) [31] => Array ( [sku] =>
311145 ) [32] => Array ( [sku] => 311147 ) [33] => Array ( [sku] =>
311148 ) [34] => Array ( [sku] => 311150 ) [35] => Array ( [sku] =>
311151 ) [36] => Array ( [sku] => 311153 ) [37] => Array ( [sku] =>
311154 ) [38] => Array ( [sku] => 311156 ) [39] => Array ( [sku] =>
311157 ) [40] => Array ( [sku] => 311159 ) [41] => Array ( [sku] =>
311160 ) [42] => Array ( [sku] => 311161 ) [43] => Array ( [sku] =>
311162 ) [44] => Array ( [sku] => 311164 ) [45] => Array ( [sku] =>
311165 ) [46] => Array ( [sku] =......
I would like to return a minimal indexed array with any values of keys matching "sku" - doesn't matter where they lie in the array nesting.
End result should be something like this as I have a function which is anticipating an array of sku data:
Array(283085, 283119, 283119, ... )
What PHP array function would best above achieve the above? Currently I'm using array_push within a foreach loop to populate an indexed but I feel like I should be able to filter values somehow.
Example:
$result = $connection->fetchAll($sql);
$skus = $result;
$simpleSkuArray=array();
foreach ($skus as $sku)
{
array_push($simpleSkuArray, $sku['sku']);
}
myFunction($simpleSkuArray);
Simple use array_column() function returns the values from a single column in the input array
$new_arr = array_column($array,'sku');
print_r($new_arr);
Use array_map() or array_column()
// (PHP 4 >= 4.0.6, PHP 5, PHP 7)
print_r(array_map(function($a){return $a['sku'];}, $array));
// (PHP 5 >= 5.5.0, PHP 7)
print_r(array_column($array,'sku'));
you new array will be
$new_array = array_map(function($a){return $a['sku'];}, $array);
$new_array = array_column($array,'sku');
There are lots of array functions that could be of help, but readability suffers. Why not do this:
$simpleArray = [];
foreach ($complicatedArray as $skuArray) {
$simpleArray[] = $skuArray['sku'];
}
Not as short as it can be, but comprehensible. It might even execute slightly faster, but the difference will be minimal.
OK, I have to admit that array_column() is a very nice solution, and most likely quite a bit faster.

How to merge array using same id

I have two arrays and I want to combine them together
1) first look like this:
[11] => Array
(
[id] => 11
[name] => test
)
[12] => Array
(
[id] => 12
[name] => test1
)
2) second array look like this:
[0] => Array
(
[offer_id] => 11
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[1] => Array
(
[offer_id] => 12
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
I want this result. How is it possible can any one provide solution for same?
[11] => Array
(
[id] => 11
[name] => test
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[12] => Array
(
[id] => 12
[name] => test
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
Thank you for the help!
Try this:
foreach ($array1 as &$arr1) {
$offer_id = $arr1['id']; // Search for this offer_id in array 2
$match = array_filter($array2, function($v) use ($offer_id){
return $v['offer_id'] == $offer_id; // Return matching offer id
});
$arr1['countries'] = current($match)['countries']; // Assign matched country to array
}

Sort multimedinsional array with dynamic key name

I want to sort a php array whose key value combination is dynamic thus making it difficult to define a function and apply usort()
Here is the array
Array (
[0] => Array ( [PAYE] => 43 )
[1] => Array ( [VAT] => 2 )
[2] => Array ( [NHIF] => 1 )
[3] => Array ( [NSSF] => 2 )
[4] => Array ( [MPESA] => 1 )
[5] => Array ( [EQUITEL] => 1 )
[6] => Array ( [AIRTEL] => 1 )
[7] => Array ( [CER] => 2 )
[8] => Array ( [BDD] => 4 )
[9] => Array ( [BMI] => 1 )
[10] => Array ( [TG] => 7 )
[11] => Array ( [BT] => 3 )
[12] => Array ( [EPL] => 4 )
[13] => Array ( [KPL] => 8 )
)
I want to sort the array using the right most value. The result should be
Array (
[0] => Array ( [PAYE] => 43 )
[13] => Array ( [KPL] => 8 )
[10] => Array ( [TG] => 7 )
[8] => Array ( [BDD] => 4 )
[12] => Array ( [EPL] => 4 )
[11] => Array ( [BT] => 3 )
[7] => Array ( [CER] => 2 )
[3] => Array ( [NSSF] => 2 )
[1] => Array ( [VAT] => 2 )
[3] => Array ( [NSSF] => 2 )
[6] => Array ( [AIRTEL] => 1 )
[9] => Array ( [BMI] => 1 )
[4] => Array ( [MPESA] => 1 )
[2] => Array ( [NHIF] => 1 )
)
How should I go about it?
use uasort function to save keys and array_shift to take values to compare
uasort($array, function($i1, $i2) {
return array_shift($i2) - array_shift($i1); });
print_r($array);
uasort and current functions will do the job:
// $arr is your initial array
uasort($arr, function($a, $b){ // will maintain index association
return current($b) - current($a);
});
http://php.net/manual/en/function.current.php

i want to show set to array which is not empty, if search hits empty array then count upto 10 still empty array then close array and continue

in the following array i want to show from array[0] to array[2] is pack1, array[5] to array[10] pack2, array[10] to array[12] is pack3 pack4 starts from array[14]. (if empty array is above 2 then close the pack and start another pack)
array
(
[0] => array
(
[id] => 1
[num] => 980909
)
[1] => array
(
[id] => 2
[num] => 090909
)
[2] => array
(
[id] => 3
[num] => 909
)
[3] => array
(
)
[4] => array
(
)
[5] => array
(
[id] => 6
[num] => 6565
)
[6] => array
(
[id] => 7
[num] => 6565
)
[7] => array
(
[id] => 8
[num] => 65
)
[8] => array
(
)
[9] => array
(
[id] => 10
[num] => 665
)
[10] => array
(
[id] => 11
[num] => 600
)
[11] => array
(
)
[12] => array
(
)
[13] => array
(
)
[14] => array
(
[id] => 15
[num] => 700
)
$datas = array();
$empcount = 10; $emp = $dIndex = 0;
for($i=0;$i
if( $emp >= $empcount) { $dIndex++; $emp = 0; } $datas[$dIndex][] = $finaldata[$i]; }

Categories