How to replicate multidimensional subarray values according to conditions?

Viewed 146

I have an array like this:

Array
(
[05049207256] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 05049207256
                [CID] => 29113082
                [GROUP_ID] => 
                [B_CODE] => 10152/6277
            )

        [1] => Array
            (
                [ID_NUMBER] => 05049207256
                [CID] => 29093574
                [GROUP_ID] => 
                [B_CODE] => 10241/6244
            )

    )

[16992375298] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 16992375298
                [CID] => 29112537
                [GROUP_ID] => 398242
                [B_CODE] => 10152/6277
            )

        [1] => Array
            (
                [ID_NUMBER] => 16992375298
                [CID] => 29112169
                [GROUP_ID] => 398223
                [B_CODE] => 10152/6276
            )

    )

[21871540019] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 21871540019
                [CID] => 29112605
                [GROUP_ID] => 398048
                [B_CODE] => 10152/6277
            )

        [1] => Array
            (
                [ID_NUMBER] => 21871540019
                [CID] => 29113036
                [GROUP_ID] => 398040
                [B_CODE] => 10152/6277
            )

        [2] => Array
            (
                [ID_NUMBER] => 21871540019
                [CID] => 29112733
                [GROUP_ID] => 398039
                [B_CODE] => 10152/6277
            )

        [3] => Array
            (
                [ID_NUMBER] => 21871540019
                [CID] => 29110803
                [GROUP_ID] => 398039
                [B_CODE] => 10152/6275
            )

    )

[19897986583] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 19897986583
                [CID] => 29112613
                [GROUP_ID] => 
                [B_CODE] => 10152/6277
            )

        [1] => Array
            (
                [ID_NUMBER] => 19897986583
                [CID] => 29111627
                [GROUP_ID] => 
                [B_CODE] => 10152/6276
            )

    )

[21228943839] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 21228943839
                [CID] => 29112573
                [GROUP_ID] => 398250
                [B_CODE] => 10152/6277
            )

        [1] => Array
            (
                [ID_NUMBER] => 21228943839
                [CID] => 29111947
                [GROUP_ID] => 3982505
                [B_CODE] => 10152/6276
            )
            
        [2] => Array
            (
                [ID_NUMBER] => 21228943839
                [CID] => 29111947
                [GROUP_ID] => 
                [B_CODE] => 10152/6276
            )

    )

)

I want to rearrange some stuff in it: If array has only one subarray, regardless of 'GROUP_ID' value, delete it:

[80388889886] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 80388889886
                [CID] => 29122690
                [GROUP_ID] =>           --> some value or empty value
                [B_CODE] => 10152/6295
            )

    )

if array has two or more subarrays, with atleast one 'GROUP_ID' value present (if there are more than one GROUP_ID, one random GROUP_ID value is ok), take that value and assign it to all other GROUP_ID's in that subarray, regardless if there are existing or empty GROUP_ID values, (except when array also has two or more subarrays and all GROUP_ID values in subarray are empty, leave them empty as they are):

[85225663143] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 85225663143
                [CID] => 29098209
                [GROUP_ID] =>
                [B_CODE] => 10241/6244
            )

        [1] => Array
            (
                [ID_NUMBER] => 85225663143
                [CID] => 29122764
                [GROUP_ID] => 399676
                [B_CODE] => 10152/6295
            )

    )
    

to:

[85225663143] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 85225663143
                [CID] => 29098209
                [GROUP_ID] => 399676
                [B_CODE] => 10241/6244
            )

        [1] => Array
            (
                [ID_NUMBER] => 85225663143
                [CID] => 29122764
                [GROUP_ID] => 399676
                [B_CODE] => 10152/6295
            )

    )
    

or:

[92723208008] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 29134479
                [GROUP_ID] => 399733
                [B_CODE] => 10304/6332
            )

        [1] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 29122696
                [GROUP_ID] => 399764
                [B_CODE] => 10152/6295
            )
            
        [2] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 2912665
                [GROUP_ID] => 399777
                [B_CODE] => 10152/6295
            )

    )
    

to:

[92723208008] => Array
    (
        [0] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 29134479
                [GROUP_ID] => 399733
                [B_CODE] => 10304/6332
            )

        [1] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 29122696
                [GROUP_ID] => 399733
                [B_CODE] => 10152/6295
            )
            
        [2] => Array
            (
                [ID_NUMBER] => 92723208008
                [CID] => 2912665
                [GROUP_ID] => 399733
                [B_CODE] => 10152/6295
            )

    )

I've got the first issue solved, but my condition in the second part doesn't work as intended, this is my code:

foreach ($result as $key => $val) {
    foreach ($val as $key1 => $val1) {
            if (count(array_keys($val)) == 1) {                 
                unset ($result[$key]);
            }
                    
            if (count(array_keys($val)) >= 2 && empty($result[$key][$key1]['GROUP_ID'])) {
                $assign_value = $val[0]['GROUP_ID'];                    
                $result[$key][$key1]['GROUP_ID'] = $assign_value;
            }
    }
}

Any help would be appreciated. Thx

3 Answers

My approach, as outlined in comments earlier, would be this:

$result = […]; // your input data

foreach( $result as $key => $item ) {
  // get group IDs for current item
  $group_ids = array_column( $item, 'GROUP_ID' );
  // if count is 1, remove item
  if( count( $group_ids ) == 1 ) {
      unset( $result[$key] );
  }
  else {
    // get only unique group IDs
    $group_ids_unique = array_unique( $group_ids );

    // is there is only one unique group ID, and it is empty?
    if( count( $group_ids_unique ) == 1 && !$group_ids_unique[0] ) {
      // do nothing
    }
    else {
        // get one “random” group id - either the 1st, or if that is empty, the 2nd one
        $group_id_set = $group_ids_unique[0] ? $group_ids_unique[0] : $group_ids_unique[1];
        // set that group ID for all entries in current item
        foreach( $item as $sub_key => $sub_value ) {
            $result[$key][$sub_key]['GROUP_ID'] = $group_id_set;
        }
    }
  }
}

I left the // do nothing if in there, for clarity - of course that could also be rewritten, negating the whole condition, and making the current else branch coding that of the if branch.

Here's a simple solution using only array_column

foreach($result as $key => $array){
    if(count($array) >= 2){
        $group_ids = array_column($array,'GROUP_ID');//get all group_ids
        foreach($group_ids as $k => $v){
            if(!empty($v)){//if there is non-empty one change the values and break the loop
                foreach($array as $key2 => $array2){
                    $result[$key][$key2]['GROUP_ID'] = $v;
                }
                break;
            }
        }
    }else{
        unset($result[$key]);
    }
}
foreach ($result as $key => $val) {
        if (count(array_keys($val)) == 1) {                 
            unset ($result[$key]);
        } else if (count(array_keys($val)) >= 2/* && empty($result[$key][$key1]['GROUP_ID'])*/) {
            /*$assign_value = $val[0]['GROUP_ID'];                    
            $result[$key][$key1]['GROUP_ID'] = $assign_value;*/
            $GROUP_ID = null;
            for ($index = 0; ($GROUP_ID) && ($index < count($val)); $index++) {
                if ((isset($val["GROUP_ID"])) && (!!$val["GROUP_ID"])) {
                    $GROUP_ID = $avl["GROUP_ID"];
                }
            }
            if (!!$GROUP_ID) {
                for ($index = 0; $index < count($val); $index++) {
                    $val["GROUP_ID"] = $GROUP_ID;
                }
            }
        }
}

Removed inner for loop around the if

The if does not require the use of a cycle around it, we can determine the number of items inside an array without looping them.

The second if was modified into an else-if

While it is formally true in this case that if the first if was true, then the second will be false, it's better to make that formal for better readability.

I don't check for the emptyness of GROUP_ID in the else

Regardless of whether the first element of the subarray has an empty GROUP_ID or not, the second might be nonempty. So, instead of checking for the first item in the subarray to have a nonempty GROUP_ID you will rather need to check whether any items in the subarray is nonempty, because that's the real condition

Checking whether there is a nonempty GROUP_ID requires a cycle

At the point after the initialization of $GROUP_ID we do not know whether there is at least a nonempty GROUP_ID in the subarray. As a result, we loop through the subarray and, if we find a nonempty GROUP_ID, then we take note of that and stop the loop.

Determining whether we need to override GROUP_ID

If $GROUP_ID is truey, that means that we have overriden its initial value, which was null, or, in other words, it means that we have found a nonempty GROUP_ID.

Overriding elements

Inside the last if we override all GROUP_ID values in the subarray to the value we found.

Related