Reputation: 1442
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?
Upvotes: 2
Views: 1731
Reputation: 4414
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
Upvotes: 2