'How to compare two array items in associative array and assign new value

I am trying to assign primary and secondary static key => value into an associative array based on two conditions.

I have an array like this,

$testarray = array(
array(
    array(
        'id' => 'ccdbh-743748',
        'name' => 'test',
        'email' => '[email protected]',
        'newsletter' => 'abc',
        'created_at' => '1546753453'
    ),
    array(
        'id' => 'uisvuiacsiodciosd',
        'name' => 'test',
        'email' => '[email protected]',
        'newsletter' => 'def',
        'created_at' => '1546753453'
    ),
    array(
        'id' => 'sdcisodjcosjdocij',
        'name' => 'test',
        'email' => '[email protected]',
        'newsletter' => 'ghi',
        'created_at' => '1546753453'
    )
),
array(
    array(
        'id' => 'sdcisodjcosjdocij',
        'name' => 'test',
        'email' => '[email protected]',
        'newsletter' => 'abc',
        'created_at' => '1546753453'
    ),
    array(
        'id' => 'ccdbh-743748',
        'name' => 'test',
        'email' => '[email protected]',
        'newsletter' => 'def',
        'created_at' => '1546753453'
    )
),
array(
    array(
        'id' => 'sdcisodjcosjdocij',
        'name' => 'test',
        'email' => '[email protected]'
        'newsletter' => 'abc',
        'created_at' => '1546753453'
    ),
    array(
        'id' => 'sdcisodjcoscisudhiu',
        'name' => 'test',
        'email' => '[email protected]'
        'newsletter' => 'def',
        'created_at' => '1515217453'
    )
)

);

The first condition would be against this ID ccdbh-743748, if we found any matching ID then this must be the primary one, and others will be secondary then. But if there is no ccdbh-743748 found in the array item, then we need to check with the created_at field whichever is older gets the primary value and the remaining will get the secondary attribute.

I have tried this code so far, but I am not sure at this stage how created_at will going to work in this code.

$data = [];
              
foreach( $testarray as $main_items ){
    $newitem=[];
    foreach ($main_items as $sub_item) {
        $p = ($sub_item['id']==='ccdbh-743748') ? 'primary' : 'secondary';
        $sub_item['profile_type']=$p;
        $newitem[]=$sub_item;
    }
    $data[]=$newitem;
}

print_r($data);

At this point, if the array contains ccdbh-743748, it will set primary to that item and others will get secondary value. Do I need to run another loop to check if no array item contains a primary value then does it's mean it should be calculated with the create_at field? Is there a way that we can use array_search with array_column in the existing loop, or is there any better approach to do this?

The final results that I am looking for are like this.

$finalarray = array(
    array(
        array(
            'id' => 'ccdbh-743748',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'abc,def,ghi',
            'created_at' => '1546753453',
            'profile_type' => 'primary'
        ),
        array(
            'id' => 'uisvuiacsiodciosd',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'def',
            'created_at' => '1546753453',
            'profile_type' => 'secondary'
        ),
        array(
            'id' => 'sdcisodjcosjdocij',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'ghi',
            'created_at' => '1546753453',
            'profile_type' => 'secondary'
        )
    ),
    array(
        array(
            'id' => 'sdcisodjcosjdocij',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'abc',
            'created_at' => '1546753453',
            'profile_type' => 'secondary'
        ),
        array(
            'id' => 'ccdbh-743748',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'abc,def',
            'created_at' => '1546753453',
            'profile_type' => 'primary'
        )
    ),
    array(
        array(
            'id' => 'sdcisodjcosjdocij',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'abc',
            'created_at' => '1546753453',
            'profile_type' => 'secondary'
        ),
        array(
            'id' => 'sdcisodjcoscisudhiu',
            'name' => 'test',
            'email' => '[email protected]',
            'newsletter' => 'abc,def',
            'created_at' => '1515217453',
            'profile_type' => 'primary'
        )
    )
);

Thanks

php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source