2

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

DocumentName and Number no relations. Number is document number, for example, invoice No.AB123, statement 04/14-01 and so on
Ok, I've updated answer to work with that condition:)
All works very good. By the way trying to understand how it works. For example if change to foreach($initial_array as $key => $arr){ and $temp_array [$key] []=$arr;, then get the same array as initial. As understand all is based on $arr['RecordDay'].",".$arr['DocumentName'].",".$arr['DocumentNumber']
yes, $arr['RecordDay'].",".$arr['DocumentName'].",".$arr['DocumentNumber'] will create a key based on the respective value. So new array will be added to that unique key. If key isn't exist then it will create or else it will add new element to existing one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.