I have 2 arrays:
Array ( [0] => Array ( [intTrackId] => 41 [intAverageRating] => 10 [bolNewRelease] => 0 [dtDateAdded] => 2013-03-08 17:32:26 ) [1] => Array ( [intTrackId] => 1 [intAverageRating] => 7 [bolNewRelease] => 0 [dtDateAdded] => 2013-03-08 18:54:35 ))
Array ( [0] => Array ( [intTrackId] => 41 [intAverageRating] => 5.5000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ) [1] => Array ( [intTrackId] => 361 [intAverageRating] => 8.0000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ))
I want to remove the items in the second which have a matching track ID in the first. So in this example, I would get:
Array ( [0] => Array ( [intTrackId] => 361 [intAverageRating] => 8.0000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ))
Is this possible with array_filter or is this a little complex for that?
Just use array_udiff() - it's intended to do this:
$one = Array (
0 => Array ('intTrackId' => 41, 'intAverageRating' => 10, 'bolNewRelease' => 0, 'dtDateAdded' => '2013-03-08 17:32:26' ),
1 => Array ('intTrackId' => 1, 'intAverageRating' => 7, 'bolNewRelease' => 0, 'dtDateAdded' => '2013-03-08 18:54:35' )
);
$two = Array (
0 => Array ('intTrackId' => 41, 'intAverageRating' => 5.5000, 'bolNewRelease' => 1, 'dtDateAdded' => '2014-03-25T09:39:28Q' ),
1 => Array ('intTrackId' => 361, 'intAverageRating' => 8.0000, 'bolNewRelease' => 1, 'dtDateAdded' => '2014-03-25T09:39:28Q' )
);
$result = array_udiff($two, $one, function($x, $y)
{
return $x['intTrackId']-$y['intTrackId'];
});
Yes it can be done with array_filter:
$array1 = array(...);
$array2 = array(...);
$newArray = array_filter($array2, function($item) use ($array1){
foreach($array1 as $elem){
if($item['intTrackId'] == $elem['intTrackId']){
return false;
}
}
return true;
});
I would first create a loop and store all track IDs from the first array in a separate array.
Then I'd loop over the second array and delete those keys that exist in the track ID array.
$track_ids = array();
foreach($array1 as $index => $items) {
$track_ids[$items['intTrackId']] = $index;
}
foreach($array2 as $items) {
if (isset($track_ids[$items['intTrackId']])) {
unset($array2[$track_ids[$items['intTrackId']]]);
}
}
Related
I have below array $billitems_taxes
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 12
)
[1] => Array
(
[id] => 2
[tax_name] => A
[tax_value] => 8
)
[2] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
and I want output as below, find two common tax_name and do some of same and then create a new array.
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 20
)
[1] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
I tried with below code, but it did not return a correct array.
$return_array = [];
foreach($billitems_taxes as $b)
{
$return_array['tax_name'] = $b->tax_name;
$return_array['tax_value'] += $b->tax_value;
}
First off, you have an array of arrays, not objects.
Then your loop needs to know if it has already seen a this tax name which will already be in the new array to check that I used array_key_exists()
$return_array = [];
foreach($billitems_taxes as $b)
{
if ( array_key_exists($b['tax_name'], $return_array) ) {
$return_array[$b['tax_name']]['tax_value'] += $b['tax_value'];
} else {
$return_array[$b['tax_name']] = $b;
}
}
RESULT
Array(
[A] => Array
([id] => 1
[tax_name] => A
[tax_value] => 20
)
[B] => Array
([id] => 3
[tax_name] => B
[tax_value] => 12
)
)
And if its important for the array to be numerically indexed just add
$return_array = array_values($return_array);
after the end of the loop
You must group by 'tax_name' and must sum 'tax_value'.
$billitems_taxes = [
['id' => 1, 'tax_name' => 'A', 'tax_value' => 12],
['id' => 2, 'tax_name' => 'A', 'tax_value' => 8],
['id' => 3, 'tax_name' => 'B', 'tax_value' => 12]
];
$result = [];
foreach($billitems_taxes as $row){
$groupKey = $row['tax_name'];
if(array_key_exists($groupKey,$result)){
$result[$groupKey]['tax_value'] += $row['tax_value'];
} else {
$result[$groupKey] = $row;
}
}
$result = array_values($result);
echo '<pre>';
var_export($result);
/*
array (
0 =>
array (
'id' => 1,
'tax_name' => 'A',
'tax_value' => 20,
),
1 =>
array (
'id' => 3,
'tax_name' => 'B',
'tax_value' => 12,
),
)
*/
The solution with the external class tableArray is very simple. The result is the same.
$result = tableArray::create($billitems_taxes)
->filterGroupAggregate(['tax_value' => 'SUM'],['tax_name'])
->fetchAll()
;
I have 2 arrays
$arr1 = Array
(
[REG1] => 94
[REG3] => 45
)
$arr2 =Array
(
[0] => REG1
[1] => REG2
[2] => REG3
[3] => REG4
)
I have to loop 2 arrays and I would like a result in one array like this:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>
)
[2] => Array(
[REG3] => 45
)
[3] => Array(
[REG4] =>
)
)
But for the instand I can not do what I want, here is where I am:
private function mergeData($arr1, $arr2){
$result = array_map(function($v1, $v2){
$t[$v1] = $v2;
return $t;
}, $arr2, $arr1);
return $result;
}
output:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>45
)
[2] => Array(
[REG3] =>
)
[3] => Array(
[REG4] =>
)
)
I cannot correctly put the bone values with the right keys.
I tried with array_merge_recursive but also failed.
Thanks for your help
$arr3 = array_fill_keys( $arr2, '' );
creates an array containing the values of $arr2 as keys with as value an empty string.
$arr3 =Array
(
[REG1] => ''
[REG2] => ''
[REG3] => ''
[REG4] => ''
)
After that just merge.
$result = array_merge ( $arr3, $arr1 );
Another option would be to use the array_map function and map key with the corresponding values, if any:
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = array_map(function($item) use($arr1){
return [$item => isset($arr1[$item]) ? $arr1[$item] : ""];
},$arr2);
This will return:
Array
(
[0] => Array
(
[REG1] => 94
)
[1] => Array
(
[REG2] =>
)
[2] => Array
(
[REG3] => 45
)
[3] => Array
(
[REG4] =>
)
)
Using a loop:
<?php
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = $arr2;
foreach($result as &$v)
$v = [$v => $arr1[$v] ?? null];
unset($v);
var_export($result);
Output:
array (
0 =>
array (
'REG1' => 94,
),
1 =>
array (
'REG2' => NULL,
),
2 =>
array (
'REG3' => 45,
),
3 =>
array (
'REG4' => NULL,
),
)
I try to convert an array to an indexed array but none of the array functions I found can solve my problem
I have this array
Array(
[no_discount] => 0
[manufacturers_id] => 2
[id] => 3
)
Array(
[no_discount] => 1
[manufacturers_id] => 1
[id] => 1
)
and I would like to convert this array to
Array(
[0] => Array(
[no_discount] => 0
[manufacturers_id] => 2
[id] => 3
)
[1] => Array(
[no_discount] => 1
[manufacturers_id] => 1
[id] => 1
)
)
Is there a simple array function or do I have use a loop?
loop - or write you own ..
$tgt = [];
foreach ( $src as $t) { $tgt[] = $t; }
I'm not sure how you are getting your array but will this work for you?
$main_array = array();
$one = Array(
"no_discount" => 0,
"manufacturers_id" => 2,
"id" => 3,
);
$two = Array(
"no_discount" => 1,
"manufacturers_id" => 1,
"id" => 1,
);
array_push($main_array, $one);
array_push($main_array, $two);
print_r($main_array);
I have 2 arrays and want to add key & value of one array into each member of another array.
The first array is :
Array
(
[0] => Array
(
[supply_id] => 2
)
[1] => Array
(
[supply_id] => 4
)
[2] => Array
(
[supply_id] => 5
)
)
The second array is :
Array
(
[status] => 1
[t1_id] => 59
)
The result I need is :
Array
(
[0] => Array
(
[supply_id] => 2,
[status] => 1,
[t1_id] => 59,
)
[1] => Array
(
[supply_id] => 4,
[status] => 1,
[t1_id] => 59,
)
[2] => Array
(
[supply_id] => 5,
[status] => 1,
[t1_id] => 59,
)
)
It looks easy I think , but I could not solve it, any body can help me please ?
you can try this:
$res = array();
foreach($secondArray as $k => $v){
$res[$k] = array_merge($secondArray[$k], $firstArray[$k]);
}
Loop through first array then merge values in new array:
$array_1= array
(
0 => array
(
"supply_id" => 2
),
1 => array
(
"supply_id" => 4
),
2 => array
(
"supply_id" => 5
),
);
$array_2=array
(
"status" => 1,
"t1_id" => 59
);
$new_array = array();
foreach ($array_1 as $key => $value) {
$new_array[] = array_merge($value,$array_2);
}
var_dump($new_array);
o/p:
array (size=3)
0 =>
array (size=3)
'supply_id' => int 2
'status' => int 1
't1_id' => int 59
1 =>
array (size=3)
'supply_id' => int 4
'status' => int 1
't1_id' => int 59
2 =>
array (size=3)
'supply_id' => int 5
'status' => int 1
't1_id' => int 59
Here we are using simple foreach loop for achieving desired output.
Try this code snippet here
foreach($firstArray as $key => &$value)
{
$value= array_merge($value,$secondArray);
}
print_r($array);
This question already has answers here:
Find intersecting rows between two 2d arrays comparing differently keyed columns
(3 answers)
Closed 4 months ago.
I have two arrays
Array
(
[0] => Array
(
[id] => 1
[affiliate_id] => 190
)
[1] => Array
(
[id] => 2
[affiliate_id] => 946
)
)
Array
(
[0] => Array
(
[id] => 1
[user_id] => 190
)
[1] => Array
(
[id] => 2
[user_id] => 246
)
[2] => Array
(
[id] => 3
[user_id] => 249
)
[3] => Array
(
[id] => 3
[user_id] => 250
)
)
Now i want to get an array which has value like this
if affiliate_id of first array exists in second array as user_id then i will get its value in third array like
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)
i just want affiliate_id which is exists in second array as user_id
$a = Array(
Array('id' => 1, 'affiliate_id' => 190),
Array('id' => 2, 'affiliate_id' => 946)
);
$b = Array(
Array('id' => 1, 'user_id' => 190),
Array('id' => 2, 'user_id' => 246),
Array('id' => 3, 'user_id' => 249),
Array('id' => 3, 'user_id' => 250)
);
$c = array_map(function ($arr) { return $arr['affiliate_id']; }, $a);
$d = array_map(function ($arr) { return $arr['user_id']; }, $b);
$e = array_intersect($c, $d);
print_r($e);
try in_array() with loop
$a = firstarray;
$b = second array;
$i =0;
foreach($b as $k=>$v) {
if(!empty($a[$i])) {
if(in_array($v['user_id'], $a[$i])) {
$c[]['affiliate_id'] = $v['user_id'];
}
}
$i++;
}
print_r($c);
output :-
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)
Use the following code:
<?php
$arr1 = array(array('id' => 1, 'affiliate_id' => 190),
array('id' => 2, 'affiliate_id' => 946));
$arr2 = array(array('id' => 1, 'user_id' => 190),
array('id' => 2, 'user_id' => 246),
array('id' => 3, 'user_id' => 249),
array('id' => 4, 'user_id' => 250));
$count = 0;
foreach ($arr1 as $k1 => $v1) {
if (in_array($v1['affiliate_id'], $arr2[$count]))
{
$arr3[]['affiliate_id'] = $v1['affiliate_id'];
}
$count++;
}
echo '<pre>'; print_r($arr3);
Output
Array
(
[0] => Array
(
[affiliate_id] => 190
)
)