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
}
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 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
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]; }
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;