Sum different array multidimention [duplicate] - php

This question already has answers here:
Sum the values of two associative multi-dimensional arrays in PHP
(3 answers)
Closed last month.
anyone can you help me for sum 2 array multidimention, i try some syntax still not work. i use 2 array not 1 array multidimention.
this is the first array
Array
(
[0] => Array
(
[<-4] => 195
[-4] => 327
[-3] => 14
[-2] => 10
[-1] => 200
[0] => 213
)
[1] => Array
(
[<-4] => 35
[-4] => 0
[-3] => 0
[-2] => 0
[-1] => 0
[0] => 0
)
)
and this is the another array
Array
(
[0] => Array
(
[<-4] => 0
[-4] => 0
[-3] => 0
[-2] => 0
[-1] => 0
[0] => 0
)
[1] => Array
(
[<-4] => 0
[-4] => 587
[-3] => 17
[-2] => 20
[-1] => 359
[0] => 1300
)
)
and the result same format with that array

Input
$array1 = array(
array('<-4' => 195,'-4' => 327,'-3' => 14,'-2' => 10,'-1' => 200,0 => 213),
array('<-4' => 35,'-4' => 0,'-3' => 0,'-2' => 0,'-1' => 0,0 => 0)
);
$array2 = array(
array('<-4' => 0,'-4' => 0,'-3' => 0,'-2' => 0,'-1' => 0,0 => 0),
array('<-4' => 0,'-4' => 587,'-3' => 17,'-2' => 20,'-1' => 359,'0' => 1300)
);
Solution
$new = array();
for($i=0;$i<count($array1);$i++){
foreach($array1[$i] as $key => $row){
$new[$i][$key] = $array1[$i][$key]+$array2[$i][$key];
}
}
Output
Array
(
[0] => Array
(
[<-4] => 195
[-4] => 327
[-3] => 14
[-2] => 10
[-1] => 200
[0] => 213
)
[1] => Array
(
[<-4] => 35
[-4] => 587
[-3] => 17
[-2] => 20
[-1] => 359
[0] => 1300
)
)

Related

Join Two arrays in php

I have two arrays and the First array name is $balances and output is below:
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
)
)
Second array name is $market and it's output:
Array
(
[0] => Array
(
[company] => SOUTHEASTB
[high] => 0
[low] => 0
)
[1] => Array
(
[company] => IFIC
[high] => 0
[low] => 0
)
[2] => Array
(
[company] => ABBANK
[high] => 0
[low] => 0
)
)
I merge this two array and getting bellow output. The 2nd array push the array value to 1st array using the code below producing a result which is not in my requirement:
$result = array();
foreach ($balances as $key => $value) {
$result[] = array_merge($value, $market[$key]);
}
Output:
Array
(
[0] => Array
(
[index] => 0
[company_code] => ABBANK
[company] => 1JANATAMF
[high] => 5.3
[low] => 5
)
[1] => Array
(
[index] => 6
[company_code] => IFIC
[company] => 1STPRIMFMF
[high] => 16.9
[low] => 16.2
)
[2] => Array
(
[index] => 11
[company_code] => SOUTHEASTB
[company] => AAMRANET
[high] => 44
[low] => 43
)
)
My challenge to check the $balances[company_code] == $market['company'] and append the 2nd array to 1st array which will produce the following result.
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[high] => 0
[low] => 0
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[high] => 0
[low] => 0
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[high] => 0
[low] => 0
)
)
But above code block is not serving my demand. Because this code block only push the second array to 1st array using merge function. I tried every other solution in my stock. Is there anyone who can give me any solution will be highly appreciated.
This can be solved in minimal code by using array_column to re-index both arrays by the company name, the two arrays can then be merged using array_merge_recursive:
$output = array_merge_recursive(array_column($balances, null, 'company_code'),
array_column($market, null, 'company'));
Output:
Array
(
[ABBANK] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[company] => ABBANK
[high] => 0
[low] => 0
)
[IFIC] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[company] => IFIC
[high] => 0
[low] => 0
)
[SOUTHEASTB] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[company] => SOUTHEASTB
[high] => 0
[low] => 0
)
)
If you want a numerically indexed result, just pass $output through array_values.
$output = array_values($output);
Demo on 3v4l.org
I have taken two arrays '$a' and '$b', in your case it is $balances and $market, respectively. array_push() is the simplest thing you can do to push/append values of array '$b' to array '$a'.
$a = array(array(
"index" => 0,
"account_id" => 1,
"company_id" => 6,
"company_code" => "ABBANK"
),
array
(
"index" => 6,
"account_id" => 1,
"company_id" => 147,
"company_code" => "IFIC"
),
array
(
"index" => 11,
"account_id" => 1,
"company_id" => 293,
"company_code" => "SOUTHEASTB"
));
$b = array
(
array
(
"company" => "SOUTHEASTB",
"high" => 0,
"low" => 0,
),
array
(
"company" => "IFIC",
"high" => 0,
"low" => 0,
),
array
(
"company" => "ABBANK",
"high" => 0,
"low" => 0
)
);
$result = array();
foreach($a as $key=>$value){
foreach($b as $key2 => $value2){
if($value['company_code'] === $value2['company']){
//append matched part of $b array to $a array value
array_push($value,$value2['high'],$value2['low']);
//append to result array
array_push($result, $value);
}
}
}
print_r($result);
//Output
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[0] => 0
[1] => 0
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[0] => 0
[1] => 0
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[0] => 0
[1] => 0
)
)
While Nick's three-function approach is certainly attractive for its brevity, be aware that it stores the relating elements from both arrays (has redundant data in the output) and makes three iterating cycles.
A less concise alternative that doesn't require a recursive call is to forge a lookup array with one cycle (foreach()) and remove the unwanted element from the lookup, then iterate the balances array and swiftly merge related data set based on share company name values.
This assumes that the balances array should dictate which market values should be included in the result.
Code: (Demo)
$lookup = [];
foreach ($market as $row) {
$lookup[array_shift($row)] = $row;
}
var_export(
array_map(
fn($row) => $row + ($lookup[$row['company_code']] ?? []),
$balances
)
);

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

php - sorting by points and then alphabetically

I have an array:
Array (
[0] => Array
( [points] => 10
[id] => 58
[nazwa] => auser1 )
[1] => Array
( [points] => 15
[id] => 36
[nazwa] => cuser2 )
[2] => Array
( [points] => 15
[id] => 57
[nazwa] => buser3 )
[3] => Array
( [points] => 20
[id] => 56
[nazwa] => duser4 )
[4] => Array
( [points] => 20
[id] => 54
[nazwa] => euser5 ))
I would like to sort this array by points and then alphabetically by nazwa.
How can I do this?
I would like to create final points table for Russia Cup!
if you want to sort your multidimensional array in sequence first points then with name then you have to create your multidimensional array in same sequence format
example: first element should be points, second name, last id. Refer following sequence.
$array = [ [ 'points' => 10, 'nazwa' => 'auser1', 'id' => 58 ],
[ 'points' => 15, 'nazwa' => 'cuser2', 'id' => 36 ],
[ 'points' => 15, 'nazwa' => 'buser3', 'id' => 57 ],
[ 'points' => 20, 'nazwa' => 'duser4', 'id' => 56 ],
[ 'points' => 20, 'nazwa' => 'euser5', 'id' => 54 ]];
array_multisort( $array );
print_r(($array));
Output:
Array
(
[0] => Array
(
[points] => 10
[nazwa] => auser1
[id] => 58
)
[1] => Array
(
[points] => 15
[nazwa] => buser3
[id] => 57
)
[2] => Array
(
[points] => 15
[nazwa] => cuser2
[id] => 36
)
[3] => Array
(
[points] => 20
[nazwa] => duser4
[id] => 56
)
[4] => Array
(
[points] => 20
[nazwa] => euser5
[id] => 54
)
)

Make associative array from array structure build with JSON and Simple XML

I read an XML file and want to get the values from the output array. This is the code to write the array:
$xml = simplexml_load_file('http://www.xyz.be/meteo');
$json_string = json_encode($xml);
$result_array = json_decode($json_string, TRUE);
The output array has the following structure:
Array
(
[STATION] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 6407
[NAME] => Kust
)
[DAY] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 20161110
)
[TMAX] => 10
[WEATHER] => 11
[DD] => NW
[FF] => 25
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 20161111
)
[TMAX] => 8
[WEATHER] => 2
[DD] => ZO
[FF] => 8
)
)
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 6479
[NAME] => Kempen
)
[DAY] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 20161110
)
[TMAX] => 9
[WEATHER] => 6
[DD] => ZW
[FF] => 11
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 20161111
)
[TMAX] => 5
[WEATHER] => 3
[DD] => NO
[FF] => 6
)
)
)
[2] => Array
(
[#attributes] => Array
(
[ID] => 6476
[NAME] => Ardennen
)
[DAY] => Array
(
...
I reayly tried a lot of things but I don't find the right solution.
** Question **
How can I put it all together in an associative array like this:
Array (
[0] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6407
['STAT_NAME'] => Kust
['DAY_INDEX'] => 0
['DAY_DATE'] => 20161110
['DAY_TMAX'] => 10
['DAY_WEATHER'] => 11
['DAY_DD '] => NW
['DAY_FF '] => 25
)
[1] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6407
['STAT_NAME'] => Kust
['DAY_INDEX'] => 1
['DAY_DATE'] => 20161111
['DAY_TMAX'] => 8
['DAY_WEATHER'] => 2
['DAY_DD '] => ZO
['DAY_FF '] => 8
)
[2] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6451
['STAT_NAME'] => Centrum
['DAY_INDEX'] => 0
['DAY_DATE'] => 20161110
['DAY_TMAX'] => 10
['DAY_WEATHER'] => 11
['DAY_DD '] => W
['DAY_FF '] => 16
)
....
)
I get a lot of errors (notices): Array to string conversion, Undefined offset: 0, Undefined offset: 1...
Does anyone know a solution?
Thanks in advance!
I think I found it.
Maybe not the best code but it works.
$data = array();
$i = 0;
foreach($result_array['STATION'] as $station) {
$dag = 1;
foreach($station['DAY'] as $day) {
$data[$i]['ID'] = $station['#attributes']['ID'];
$data[$i]['NAME'] = $station['#attributes']['NAME'];
$data[$i]['DATUM'] = $day['#attributes']['ID'];
$data[$i]['DAG'] = $dag;
$data[$i]['TMAX'] = $day['TMAX'];
$data[$i]['WEATHER'] = $day['WEATHER'];
$data[$i]['DD'] = $day['DD'];
$data[$i]['FF'] = $day['FF'];
$i += 1;
$dag += 1;
}
}
gives me the result:
Array
(
[0] => Array
(
[ID] => 6407
[NAME] => Kust
[DATUM] => 20161110
[DAG] => 1
[TMAX] => 10
[WEATHER] => 11
[DD] => NW
[FF] => 25
)
[1] => Array
(
[ID] => 6407
[NAME] => Kust
[DATUM] => 20161111
[DAG] => 2
[TMAX] => 8
[WEATHER] => 2
[DD] => ZO
[FF] => 8
)
[2] => Array
(
[ID] => 6451

Remove array1 value which dosnt exist in array2 in php associative arrays

I have five associative arrays in PHP which contain data from the database.
Here is an example of array 1 :
Array1
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => info#mail.com
[day] => 06/30/2013
)
[1] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => infox#mail.com
[day] => 06/30/2013
)
)
Now here is array 2
Array
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[task] => 36
[email] => info#mail.com
[day] => 06/30/2013
)
)
I want to apply filter on my first array from array2 to show only this after applying filter in result array
result
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => info#mail.com
[day] => 06/30/2013
)
)
You can simply use :
array_merge($array1,$array2);

Categories