php based on conditions create multiple arrays from one array - php

Initial array
Array
(
[0] => Array
(
[RecordDay] => 23
[Amount] => 1.50
[DocumentName] => bank stmt
[DocumentNumber] => 1
)
[1] => Array
(
[RecordDay] => 17
[Amount] => 0.21
[DocumentName] => invoice
[DocumentNumber] => 2
)
[2] => Array
(
[RecordDay] => 17
[Amount] => 1.00
[DocumentName] => invoice
[DocumentNumber] => 2
)
)
From all subarrays, where [RecordDay], [DocumentName] and [DocumentNumber] are the same want to create new array.
For keys [1] and [2] [RecordDay], [DocumentName] and [DocumentNumber] are the same.
So as a result want to get this
Array
(
[0] => Array
(
[RecordDay] => 23
[Amount] => 1.50
[DocumentName] => bank stmt
[DocumentNumber] => 1
)
)
Array
(
[0] => Array
(
[RecordDay] => 17
[Amount] => 0.21
[DocumentName] => invoice
[DocumentNumber] => 2
)
[1] => Array
(
[RecordDay] => 17
[Amount] => 1.00
[DocumentName] => invoice
[DocumentNumber] => 2
)
)
But no idea how to do that
Made like this
$first_loop = true;
foreach($initial_array as $key => $one_dimensional_array){
if($first_loop == true){
$new_array1['RecordDay'][] = $one_dimensional_array['RecordDay'];
$first_loop = false;
}
if($first_loop == false){
if( in_array( $one_dimensional_array['RecordDay'], $new_array1['RecordDay'] ) ){
$new_array1['RecordDay'][] = $one_dimensional_array['RecordDay'];
}
else{
$new_array2['RecordDay'][] = $one_dimensional_array['RecordDay'];
}
}//if($first_loop == false){
}//foreach($initial_array as $key => $one_dimensional_array){
Got
Array
(
[RecordDay] => Array
(
[0] => 23
[1] => 23
)
[DocumentName] => Array
(
[0] => bank stmt
[1] => bank stmt
)
[DocumentNumber] => Array
(
[0] => 1
[1] => 1
)
)
Array
(
[RecordDay] => Array
(
[0] => 17
[1] => 17
)
[DocumentName] => Array
(
[0] => invoice
[1] => invoice
)
[DocumentNumber] => Array
(
[0] => 2
[1] => 2
)
)
But this is not what want. Any ideas what need to change?

Try like this,
$temp_array=array();
foreach($initial_array as $arr){
$temp_array[$arr['RecordDay'].",".$arr['DocumentNumber'].",".$arr['DocumentName']] []=$arr; //here it will **ADD** a new array in a key 'RecordDay,DocumentNumber,DocumentName' format. e.g. [23,1,'bank stmt'] , [17,2,'invoice'].
}
Now you need to process $temp_array to get individual arrays
foreach ($temp_array as $separate_array){
print_r($separate_array); //process your new array here one by one.
}
Working DEMO

Related

How to Combine Same ID with its Quantity in Multiple array in PHP

I want to combine same variant_id with its quantity and price from multiple array to one new array in PHP.
Currently, I have same variant_ids are '30929149493321, 19663805153353' in the sample data.
The Output Array should be combined the same variant_id with its quantity and price.
Input:
Array (
[0] => Array
(
[variant_id] => 30929149493321
[quantity] => 1
[price] => 299.00
)
[1] => Array
(
[variant_id] => 19663805153353
[quantity] => 1
[price] => 99.00
)
[2] => Array
(
[variant_id] => 19663804989513
[quantity] => 6
[price] => 99.00
)
[3] => Array
(
[variant_id] => 30929149493321
[quantity] => 1
[price] => 299.00
)
[4] => Array
(
[variant_id] => 19663805153353
[quantity] => 1
[price] => 99.00
)
[5] => Array
(
[variant_id] => 31108834754633
[quantity] => 1
[price] => 379.05
)
)
Desired output:
Array (
[0] => Array
(
[variant_id] => 30929149493321
[quantity] => 2
[price] => 299.00
)
[1] => Array
(
[variant_id] => 19663805153353
[quantity] => 2
[price] => 99.00
)
[2] => Array
(
[variant_id] => 19663804989513
[quantity] => 6
[price] => 99.00
)
[3] => Array
(
[variant_id] => 31108834754633
[quantity] => 1
[price] => 379.05
)
)
This code example below creates a new empty array $combined. It iterates over each element of your array ($array in the foreach should be replaced with your array name) and searches in the new $combined array if there is already an entry with the actual quantity_id. If it exists, it combines the two quantities. If it not exists, it pushes the actual values to the $combined array. Your desired result is stored in $combined.
$combined = array();
foreach( $array as $values ) {
if( ( $key = array_search( $values['variant_id'], array_column( $combined, 'variant_id') ) ) !== false ) {
$combined[$key]['quantity'] += $values['quantity'];
} else {
$combined[] = $values;
}
}

how to make incremental value in hierarchal array in Php?

Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...

PHP array reformat to highchart json ob

I am try to format an array to a JSON object that highcharts supports. My array from the database is as follows:
Array
(
[0] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 4
[amount] => 7700
)
[1] => Array
(
[Group_ID] => 2
[Name] => B Line graph
[month] => 4
[amount] => 390
)
[2] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 5
[amount] => 5000
)
[3] => Array
(
[Group_ID] => 2
[Name] => B line graph
[month] => 5
[amount] => 210
)
)
I need to create an array like this to be able to create a highchart compatible JSON object:
Array
(
[0] => Array
(
[name] => A revenue
[data] => Array
(
[4] => 7700 //amount for the fourth month
[5] => 5000 //amount for the fifth month
)
)
[1] => Array
(
[name] => B revenue
[data] => Array
(
[4] => 390 //amount for the fourth month
[5] => 210 //amount for the fifth month
)
)
)
I have managed to come up with this array using my foreach but I cant seem to find a way to do it correctly:
Array
(
[0] => Array
(
[name] => A line graph
[amount] => 7700
[month] => 4
)
[1] => Array
(
[name] => B line graph
[amount] => 390
[month] => 4
)
[2] => Array
(
[name] => A line graph
[amount] => 5000
[month] => 5
)
[3] => Array
(
[name] => B line graph
[amount] => 210
[month] => 5
)
)
My foreach:
foreach ($data as $key => $value) {
$r[] = [
'name' => $value['Line_GraphName'],
'data' => $value['amount'],
'month' => $value['month']
];
}
You can create it very simply like this:
foreach ($data as $value) {
$r[$value['Group_ID']]['name'] = $value['Line_GraphName'];
$r[$value['Group_ID']]['data'][$value['month']] = $value['amount'];
}
Loop and create a result with Group_ID as the key and add the name key and value
Add data array and append month as key and amount as value
If you don't like having Group_ID as the key and want to re-index:
$r = array_values($r);
The solution using array_walk and array_values functions:
$result = [];
array_walk($data, function($v) use(&$result) {
$key = $v['Group_ID'] . $v['Name'][0]; // compound key: "group_id + revenue's prefix"
if (!isset($result[$key])) {
$result[$key] = ['name' => $key[1] . " Revenue", 'data' => [$v['month'] => $v['amount']]];
} else {
$result[$key]['data'][$v['month']] = $v['amount'];
}
});
print_r(array_values($result));
The output:
Array
(
[0] => Array
(
[name] => A Revenue
[data] => Array
(
[4] => 7700
[5] => 5000
)
)
[1] => Array
(
[name] => B Revenue
[data] => Array
(
[4] => 390
[5] => 210
)
)
)

PHP: move values of array under others keys

I have this array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -5,7
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -7
[negative] => Array
(
[0] => Array
(
[amount] => -7
)
)
)
)
You can note that key 0 has +10.00 of positive and -5.7 of negative (they are money transactions).
Key 1 has +13 and -7.
Basically, I need to iterate into array and move 4.30 under key 0, taken from THE NEGATIVE of key 1.
This is must be the final array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -10.00
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
[3] => Array
(
[amount] => -4.30
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -2.70
[negative] => Array
(
[0] => Array
(
[amount] => -2.70
)
)
)
)
If you need code that performs the transformation on this particular array (where you know the array indexes), the following will do what you want:
$expense = -4.30;
$your_array[0]['negative'][] = ['amount' => $expense];
$your_array[0]['negative_sum'] += $expense;
$your_array[1]['negative'][0]['amount'] -= $expense;
$your_array[1]['negative_sum'] -= $expense;
If you want a more "general" approach for arbitrary indexes and arbitrary amount entries, you will need to rephrase your question in broader terms.

Parse a multi dimensional array and pull values

How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);

Categories