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,
),
)
Related
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 a multidimensional array and I want to remove the top level array and merge all its sub array into one array.
Below is my array:
$arr = [KEY1] => Array
(
[0] => Array
(
[Feb] => 120
)
[1] => Array
(
[Jan] => 230
)
[3] => Array
(
[Mar] => 340
)
)
[KEY2] => Array
(
[0] => Array
(
[Feb] => 12
)
[1] => Array
(
[Jan] => 23
)
[3] => Array
(
[Mar] => 34
)
)
I need to arrange and sort this array like below:
Output:
[KEY1] => Array
(
[Jan] => 230,
[Feb] => 120,
[Mar] => 340
)
[KEY2] => Array
(
[Jan] => 23,
[Feb] => 12,
[Mar] => 34
)
I have used
call_user_func_array('array_merge', $arr ); but not working.
Please suggest any wise way to do this.
Thanks
You can try like this as simple way,
$arr = [
'key1' =>[
0 => ["feb" => 123],
1 => ["dev" => 213],
2 => ["jan" => 111],
],
'key2' =>[
0 => ["feb" => 132],
1 => ["dev" => 321],
2 => ["jan" => 555],
],
];
$result = [];
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2)
$result[$k][$k2] = $v2;
}
}
print_r($result);
Hope this will solve your problem.
EDIT
Here is your sorting function.
function sortNestedArray(&$a)
{
sort($a);
for ($i = 0; $i < count($a); $i++) {
if (is_array($a[$i])) {
sortNestedArray($a[$i]);
}
}
return $a;
}
$a = sortNestedArray($result);
print_r($a);
I have two different array how to get below out put.
Array One:-
[81799] => Array
(
[TOTAL_HITS] => 254
[UNIQUE_HITS] => 82
)
Array Two:-
[81799] => Array
(
[ACTIVATION] => 7561.60
[RENEWAL] => 300
[ACT_REVENUE] => 7310.22
)
Expected output:-
[81799] => Array
(
[TOTAL_HITS] => 254
[UNIQUE_HITS] => 82
[ACTIVATION] => 7561.60
[RENEWAL] => 300
[ACT_REVENUE] => 7310.22
)
try this,
$a = array("81799" => Array
(
"TOTAL_HITS" => 254,
"UNIQUE_HITS" => 82
)
);
$b = array("81799" => Array
(
"ACTIVATION" => 7561.60,
"RENEWAL" => 300,
"ACT_REVENUE" => 7310.22
)
);
foreach($a as $key_a => $val_a)
{
$temp = $b[$key_a] + $val_a;
//$result[$key_a] = $b[$key_a];
$result[$key_a] = $temp;
}
DEMO
This should do the trick.
I guess your array looks like below:
<?php
$a1 = array("81799" => array("TOTAL_HITS" => 254, "UNIQUE_HITS" => 82));
$a2 = array("81799" => array("ACTIVATION" => 7561.60, "RENEWAL" => 300, "ACT_REVENUE" => 7310.22));
foreach($a1 as $key => $val) {
$result[$key] = array_merge($a1[$key], $a2[$key]);
}
print_r($result);
This question already has answers here:
PHP - merging arrays
(2 answers)
Closed 7 years ago.
I have these two arrays and I want to combine them into one. Using the duplicate values from 0,1 in the second array. Below is an example of how I would want it to look. I hope someone can help.
Array(
[201500001] => Array
(
[0] => 1003123603
[1] => 3062226597
)
[201500002] => Array
(
[0] => 3067005512
)
)
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
)
)
Expected output:
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
[2] => 201500001
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
[2] => 201500002
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
[2] => 201500001
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
[2] =>
)
)
I rewrote your various arrays as valid arrays. Other than that, I think that what you are looking for is to use array_intersect. This works for me:
$source = array(
201500001 => array(
0 => 1003123603,
1 => 3062226597
),
201500002 => array(
0 => 3067005512
)
);
$target = array(
1127893457 => array(
0 => 1003123603
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$expected = array(
1127893457 => array(
0 => 1003123603,
2 => 201500001
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512,
2 => 201500002
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597,
2 => 201500001
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$newArray = [];
foreach ($target as $targetKey => $targetValue) {
foreach ($source as $sourceKey => $sourceValue) {
if (array_intersect($sourceValue, $targetValue)) {
$targetValue[2] = $sourceKey;
}
$newArray[$targetKey] = $targetValue;
}
}
echo ($newArray === $expected) ? "Match!" : "Miss." . PHP_EOL;
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
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']]]);
}
}