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
)
);
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
hi there i am new to php and i want to access isMultiple element of array how can i do that then i want to show if the value of isMultiple is 1 then it should show yes.. any help would be appreciated and it would be great......!!.
Array
(
[result] => Array
(
[0] => Array
(
[pkFeatureTypeId] => 72
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[1] => Array
(
[pkFeatureTypeId] => 32
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[2] => Array
(
[pkFeatureTypeId] => 33
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[3] => Array
(
[pkFeatureTypeId] => 35
[Name] =>
[Status] => 1
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
)
)
Try this.
foreach($data['result'] as $item){
if($item['isMultiple'] == '1'){
echo 'Yes';
}
}
I have a mixed index/associative array in PHP that can, in theory, be infinitely deep because some elements may have 'children' arrays. I attach a slimmed down example. The php array is derived from some JSON data.
I want to search on a given key/value pair and to delete any elements matching that pair at the index level.
So for example, in the array below, I wish to search for the key of 'formId' and a value of '44' and to delete elements such that $array[0][0][0][0] and $array[0][1] are removed. Obviously I will then need to re-number the array.
I need to be able to search on any key/value pair... only one pair at a time is necessary... that is to say that the above example of 'formId' of '44' might be 'viewId' of '16' next time.
I have tried numerous ways to do this, with iterators, recursive foreach loops, &$reference keys but I just cannot seem to get it right.
Array
(
[0] => Array
(
[formId] => 0
[subId] => 0
[viewId] => 0
[id] => 0
[children] => Array
(
[0] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 16
[id] => _st_node_5328_0_0
[children] => Array
(
[0] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 16
[id] => _st_node_3838_0_0_0
[children] => Array
(
[0] => Array
(
[formId] => 44
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_0
)
[1] => Array
(
[formId] => 7
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_1
)
[2] => Array
(
[formId] => 3
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_2
)
[3] => Array
(
[formId] => 10
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_3
)
[4] => Array
(
[formId] => 9
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_4
)
[5] => Array
(
[formId] => 8
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_5
)
[6] => Array
(
[formId] => 6
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_6
)
[7] => Array
(
[formId] => 5
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_7
)
[8] => Array
(
[relId] => 167
[formId] => 4
[text] => Laptop Computers
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_8
[isActive] =>
)
[9] => Array
(
[formId] => 1
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_9
)
)
)
[1] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 1
[id] => _st_node_3838_0_0_1
)
[2] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 11
[id] => _st_node_3838_0_0_2
)
[3] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 12
[id] => _st_node_3838_0_0_3
)
[4] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 13
[id] => _st_node_3838_0_0_4
)
[5] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 14
[id] => _st_node_3838_0_0_5
)
[6] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 110
[id] => _st_node_3838_0_0_6
)
)
)
[1] => Array
(
[formId] => 44
[subId] => 0
[viewId] => 144
[id] => _st_node_5328_0_1
)
[2] => Array
(
[formId] => 10
[subId] => 0
[viewId] => 26
[id] => _st_node_5328_0_2
)
[3] => Array
(
[formId] => 9
[subId] => 0
[viewId] => 9
[id] => _st_node_5328_0_3
)
[4] => Array
(
[formId] => 8
[subId] => 0
[viewId] => 8
[id] => _st_node_5328_0_4
)
[5] => Array
(
[formId] => 7
[subId] => 0
[viewId] => 25
[id] => _st_node_5328_0_5
)
[6] => Array
(
[formId] => 6
[subId] => 0
[viewId] => 6
[id] => _st_node_5328_0_6
)
[7] => Array
(
[formId] => 5
[subId] => 0
[viewId] => 5
[id] => _st_node_5328_0_7
)
[8] => Array
(
[formId] => 4
[subId] => 0
[viewId] => 4
[id] => _st_node_5328_0_8
)
[9] => Array
(
[formId] => 3
[subId] => 0
[viewId] => 3
[id] => _st_node_5328_0_9
)
)
)
)
The idea
Since you want to unset elements, you definitely want to pass the $array into the function as a reference (&$array).
You want to use recursion to allow you to dig as deep as necessary based on the actual array.
You loop over all elements of the array and the first thing you do is check whether an element is an array. If it is, you check if that subarray matches your criteria. If it does, you remove it. If it doesn't you invoke the function with the same $key, $value pair on that subarray.
The code
function removeMatching($key, $value, &$arr) {
// Iterate over all $arr elements
foreach ($arr as $i => $subarr) {
// Skip all elements that are not arrays
if (is_array($subarr)) {
// Check if the sub-array matches the criteria
if (isset($subarr[$key]) && $subarr[$key] === $value) {
// Remove it from the main array if it does
unset($arr[$i]);
} else {
// If the sub-array didn't match the criteria
// Check if it contains any other matching sub-arrays
removeMatching($key, $value, $arr[$i]);
}
}
}
}
Apply like this:
$array = []; // your array
removeMatching('formId', 44, $array);
var_dump($array);
I need help. I want to remove array on array list when there is duplicate value.
I have this array
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
As you may notice that there are duplication value on price key. But I only want to remove an array if the the count is equal to 0. But if the duplicate price has no value on count. It only remove one duplicate array. My expected output will be like this.
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
Please help thanks.