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
)
);
Related
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
)
)
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
)
)
This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array
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);
I am trying to insert an array into a 2-dimensional array at a specific position.
According the manual, i should be able to do this with array_splice(), but it only deletes the contents of my receiving array without insertion.
I want to get an array with all the values (arrays) of $receivingArray plus the new value (array).
What am I doing wrong??
manual info:
array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )
If length is specified and is zero, no elements will be removed.
If replacement is just one element it is not necessary to put array() around > it, unless the element is an array itself, an object or NULL.
input:
$newArray = array_splice($receivingArray, 0, 0, array($value));
result: $newArray is an empty array
input :
$newArray = array_splice($receivingArray, 1, 0, array($value));
result: $newArray is an empty array
this input:
print_r($receivingArray);
print_r(array($value));
$newArray = array_splice($receivingArray, 1, 1, array($value));
print_r($newArray);
gives: (interestingly)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
From the docs for array_splice()
Return Values
Returns the array consisting of the extracted elements.
$newArray = array_splice($receivingArray, 0, 0, array($value));
array_splice modifies its input, so the results you're looking for are in $receivingArray and not $newArray
I missed the fact that array_slice() doesn't actually return its output, but rather acts upon the receiving array itself, which is passed by reference. I didn't notice that there is an ammpersand in front of the first parameter in the manual specification.
this input:
print_r($receivingArray);
print_r(array($value));
array_splice($receivingArray, 0, 0, array($value));
print_r($receivingArray);
gives the correct result:
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
[2] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)