I have this two arrays:
$arr1 = Array ( 600 => 580, 500 => 480, 100 => 80 ) <- always 3
$arr2 = Array ( 'filt' => grey, 'or' => 90 ) <- this array is dynamic
How can I combine each key and value from first array with the other array? I mean, I need:
$array = ( 600 => 580, 'filt' => grey, 'or' => 90 )
$array = ( 580 => 480, 'filt' => grey, 'or' => 90 )
successively in for/foreach loop. How can I do this?
Thanks a lot, im new on php ^^ and sorry for my english :P
I think you need this. Just loop through the first array, copy the first array in the out array and add key, value of loop array into the out array.
As you mention:
$array = ( 600 => 580, 'filt' => grey, 'or' => 90 )
$array = ( 580 => 480, 'filt' => grey, 'or' => 90 )
I think you need to create different array each time. For each of the
element of first array you need different array as output.
$arr1 = array( "600" => 580, "500" => 480, "100" => 80 );
$arr2 = array( 'filt' => "grey", 'or' => 90 );
$out = array();
foreach($arr1 as $key => $value){
$out = $arr2;
$out[$key] = $value;
print_r($out);
}
Result
First Iteration:
Array
(
[filt] => grey
[or] => 90
[600] => 580
)
Second Iteration:
Array
(
[filt] => grey
[or] => 90
[500] => 480
)
Third Iteration:
Array
(
[filt] => grey
[or] => 90
[100] => 80
)
Try this:
$arr1 = array( '600' => '580', '500' => '480', '100' => '80' ) ;
$arr2 = array( 'filt' => 'grey', 'or' => '90' );
$arra_new = array();
foreach($arr1 as $key=>$arr) {
$temp = $arr2;
$temp[$key] = $arr;
$arra_new[] = $temp;
}
print '<pre>';print_r($arra_new);exit;
The solution using array_walk and array_replace functions:
$arr1 = Array(600 => 580, 500 => 480, 100 => 80);
$arr2 = Array('filt' => 'grey', 'or' => 90, 'and' => 150, 'if' => 10);
$combined = [];
array_walk($arr1, function($v, $k) use($arr2, &$combined) {
$combined[] = array_replace([$k => $v], $arr2);
});
print_r($combined);
The output:
Array
(
[0] => Array
(
[600] => 580
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
[1] => Array
(
[500] => 480
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
[2] => Array
(
[100] => 80
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
)
try this method..
<?php
$arr1 = array(600 => 580, 500 => 480, 100 => 80 );
$arr2 = array( 'filt' => 'grey', 'or' => 90 );
$newArray =array();
foreach($arr1 as $key =>$val)
{
$temp =array();
$temp[$key]= $val;
$newArray[] =$temp+$arr2;
}
echo "<pre>"; print_r($newArray);
?>
This will Output:
Array
(
[0] => Array
(
[600] => 580
[filt] => grey
[or] => 90
)
[1] => Array
(
[500] => 480
[filt] => grey
[or] => 90
)
[2] => Array
(
[100] => 80
[filt] => grey
[or] => 90
)
)
Related
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 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);
I've array multidimentional ..
Array (
[123] => Array ( [0] => 120 [1] => 200 [2] => 180 [3] => 130 )
[124] => Array ( [0] => 150 [1] => 155 [2] => 160 [3] => 165 )
[125] => Array ( [0] => 121 [1] => 120 [2] => 121 [3] => 121 )
)
I want to convert like this
120,200,180,130
150,155,160,165
121,120,121,121
how to code this guys ?
my code from stackoverflow too ..
echo join("','", array_map(function ($data) { return $data[0]; }, $data))
but .. the output
120, 150, 121 .. i want to get from 123
This should work for you:
(Here I just go through each innerArray with array_map() and implode() it and print it)
<?php
$arr = [
"123" => [120, 200, 180, 130],
"124" => [150, 155, 160, 165],
"125" => [121, 120, 121, 121]
];
array_map(function($v){
echo implode(",", $v) . "<br />";
}, $arr);
?>
Output:
120,200,180,130
150,155,160,165
121,120,121,121
You can simply iterate over all items in the $arrs and use implode to format every single array:
$arrs = Array (
123 => Array ( 0 => 120, 1 => 200, 2 => 180, 3 => 130 ),
124 => Array ( 0 => 150, 1 => 155, 2 => 160, 3 => 165 ),
125 => Array ( 0 => 121, 1 => 120, 2 => 121, 3 => 121 ),
)
foreach($arrs as $arr) {
echo implode(",",$arr)."\n";
}
"\n" means you append a new line in raw text. In case you want to use HTML for formatting, you should evidently use <br/>:
foreach($arrs as $arr) {
echo implode(",",$arr).'<br/>';
}
$newArr = array();
foreach($yourArr as $key =>$val)
{
$newArr[] = implode(",",$val);
}
foreach($newArr as $arr)
{
echo $arr."<br>";
}
output
120,200,180,130
150,155,160,165
121,120,121,121
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']]]);
}
}
i have a Multiple Dimensional array and i need to sum up values which have the same keys .
print_r($inputs)
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 12
[red] => 5
[orange] => 9
[black] => 6
[white] => 5
[blue] => 11
)
)
[1] => Array
(
[id] => colors
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
[2] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
i need the result to be like
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 32
[red] => 45
[orange] => 89
[black] => 66
[white] => 105
[blue] => 121
)
)
[1] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
i tried to use array_shift to sort the values and sum the sub array values but i failed
$finalRate = array_shift($inputs);
foreach ($inputs as $val) {
foreach ($val as $key => $val) {
$finalRate[$key] += $val;
}
}
but failed and return empty array .
Assuming your arrays have always the same structure I'd go with:
$outcome = array();
foreach ($colors as $array) {
$id = $array['id'];
if (array_key_exists($id, $outcome)) {
foreach ($array['power'] as $color => $value) {
$outcome[$id]['power'][$color] += $value;
}
continue;
}
$outcome[$array['id']] = $array;
}
array_values($outcome);
$array1 = array_slice($input,0,1); //slicing first value of $input i.e Array([0]=>array)
$array2 = array_slice($input,1,1); //slicing second value of $input i.e Array([1]=>array)
$array = array_sum_values($array1,$array2); //summing values of two arrays
$input = array_splice($input,0,2,$array) //Removing [0] and [1] from $input and replacing with $array.
Please refer PHP manual for more details.
This probably isn't the most efficient way for going about this.. but it works and was easy to implement:
$arr = array(
0 => array(
'id' => 'colors',
'power' => array(
'green' => 12,
'red' => 5,
'orange' => 9,
'black' => 6,
'white' => 5,
'blue' => 11,
),
),
1 => array(
'id' => 'colors',
'power' => array(
'green' => 20,
'red' => 40,
'orange' => 80,
'black' => 60,
'white' => 100,
'blue' => 110,
),
),
2 => array(
'id' => 'glossycolors',
'power' => array(
'green' => 20,
'red' => 40,
'orange' => 80,
'black' => 60,
'white' => 100,
'blue' => 110,
),
),
);
foreach ( $arr as $k1 => $v1 ) {
foreach ( $arr as $k2 => $v2 ) {
if ( $k1 === $k2 ) continue;
if ( ! isset( $arr[ $k1 ] ) || ! isset( $arr[ $k2 ] ) ) continue;
if ( $v1['id'] === $v2['id'] ) {
foreach ( $v2['power'] as $power_k => $power_v ) {
$arr[$k1]['power'][$power_k] += $power_v;
}
unset( $arr[$k2] );
}
}
}
print_r( $arr );
And this results in:
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 32
[red] => 45
[orange] => 89
[black] => 66
[white] => 105
[blue] => 121
)
)
[2] => Array
(
[id] => glossycolors
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
So basically, it loops through the same array twice and sums the values of common 'id' elements, then removes the second copy from the array leaving only the original with the sum of the later. Cheers