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");
Related
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.
I'm trying to figure out how to merge multiple multidimensional arrays while preserving the keys and not overwriting.
I've got a script that loops and generates one multidimensional array every time it loops. I need to store all those arrays in one new array.
For example
$Array1 = Array ( [0] => Array ( [5] => 2 ) );
$Array2 = Array ( [0] => Array ( [6] => Array ( [3] => 4 ) ) );
$Array3 = Array ( [1] => 6 );
$Array4 = Array ( [2] => Array ( [5] => Array ( [3] => 8 ) ) );
$Array5 = Array ( [2] => Array ( [5] => Array ( [4] => 10 ) ) );
$Array6 = Array ( [2] => Array ( [5] => Array ( [5] => 12 ) ) );
$Array7 = Array ( [2] => Array ( [6] => Array ( [5] => 14 ) ) );
$Array8 = Array ( [4] => 16 );
$Array9 = Array ( [7] => Array ( [5] => 18 ) )
$Array10 = Array ( [670] => Array ( [5] => Array ( [3] => Array ( [4] => 20 ) ) ) );
$Array11 = Array ( [34] => Array ( [5] => Array ( [3] => Array ( [3] => Array ( [9] => 22 ) ) ) ) );
The number of arrays produced defer and the number of dimensions also defer per array.
I need to add them all and generate an array like:
$ArrayTotal = Array (
[0] => Array (
[5] => 2
[6] => Array (
[3] => 4
)
)
[1] => 6
[2] => Array (
[5] => Array (
[3] => 8
[4] => 10
[5] => 12
)
[6] => Array (
[5] => 14
)
)
[4] => 16
[7] => Array (
[5] => 18
)
[670] => Array (
[5] => Array (
[3] => Array (
[4] => 20
)
)
)
[34] => Array (
[5] => Array (
[3] => Array (
[3] => Array (
[9] => 22
)
)
)
)
)
There are duplicate keys, like $ArrayTotal[2] but all the keys used to acces the value are always different. For example $ArrayTotal[2][5][3] and $ArrayTotal[2][5][4] share the first key, but the keys used to get to the value are different.
I've tried using array_merge and array_merge_recursive and even just adding them using +. I've also tried:
$out = array();
foreach ($ArrayTotal as $key => $value){
$out[] = array_merge((array)$ArrayN[$key], (array)$value);
}
$ArrayTotal = $out;
Where I looped though all the $ArrayNs that where generated. But it overwrites duplicate keys in the first dimension. So it will for example not store $Array4, $Array5 and $Array6.
Another small but important detail is, that the keys can be strings.
Like:
$ArrayX = Array ( ["SomeKey"] => Array ( [5] => Array ( ["OtherKey"] => "TheValue" ) ) );
My question is:
How can I generate the $ArrayTotal as described?
I would prefer something that I can implement in the loop and that adds the arrays one by one.
Thanks
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 have this array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -5,7
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -7
[negative] => Array
(
[0] => Array
(
[amount] => -7
)
)
)
)
You can note that key 0 has +10.00 of positive and -5.7 of negative (they are money transactions).
Key 1 has +13 and -7.
Basically, I need to iterate into array and move 4.30 under key 0, taken from THE NEGATIVE of key 1.
This is must be the final array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -10.00
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
[3] => Array
(
[amount] => -4.30
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -2.70
[negative] => Array
(
[0] => Array
(
[amount] => -2.70
)
)
)
)
If you need code that performs the transformation on this particular array (where you know the array indexes), the following will do what you want:
$expense = -4.30;
$your_array[0]['negative'][] = ['amount' => $expense];
$your_array[0]['negative_sum'] += $expense;
$your_array[1]['negative'][0]['amount'] -= $expense;
$your_array[1]['negative_sum'] -= $expense;
If you want a more "general" approach for arbitrary indexes and arbitrary amount entries, you will need to rephrase your question in broader terms.
I have this Array
Array (
[0] => Array (
[0] => Array ( [value] => Cooking, [occurence] => 1 )
[1] => Array ( [value] => Music, [occurence] => 1 )
[2] => Array ( [value] => Football,Cooking, [occurence] => 1 )
[3] => Array ( [value] => Travel, [occurence] => 1 )
[4] => Array ( [value] => Cooking,Reading, [occurence] => 2 )
[5] => Array ( [value] => Football,Travel, [occurence] => 1 )
[6] => Array ( [value] => Football, [occurence] => 1 )
[7] => Array ( [value] => Music,Cooking, [occurence] => 1 )
[8] => Array ( [value] => Reading,Travel, [occurence] => 1 )
)
)
The [2], [4], [5], [7] and [8] have 2 values for the key [value].
What I want to do is to split the 2 values of these keys in different keys.
The new values should not go to new Arrays, but they will be added to the similar existing Arrays.
For example, if I break the [2] (Football,Cooking) the result will be that the occurence of [6] (Football) will be incremented by 1 and the [occurence] of [0] (Cooking) will be incremented by 1 also.
Thank you !
Yann
$newdata = array()
foreach($array as $data) { // $array being the inner array with the 9 elements
$keys = explode(',', $data['value']);
foreach ($keys as $subkey) {
$newdata[$subkey]++;
}
}
which would give you something like
$newdata = array(
'Cooking' => 4,
'Football' => 3
etc...
);
Not sure how you want your structure to look afterwards, but at least this'll do the inventory for you.