nirvair
nirvair

Reputation: 4180

Merging two common array into a single array

I have this array:

Array
(
    [0] => stdClass Object
        (
            [client_id] => 70
            [client_name] => Berws
            [account_identifier] => ACL70
            [ticket_identifier] => B21
            [ticket_id] => 21
            [stage_name] => New

        )


    [1] => stdClass Object
        (
            [client_id] => 75
            [client_name] => ASDF
            [account_identifier] => 
            [ticket_identifier] => BB17
            [ticket_id] => 17
            [stage_name] => New

        )

    [2] => stdClass Object
        (
            [client_id] => 71
            [client_name] => QWERT
            [account_identifier] => ACI71
            [ticket_identifier] => B15
            [ticket_id] => 15
            [stage_name] => Won

        )

    [3] => stdClass Object
        (
            [client_id] => 70
            [client_name] => Berws
            [account_identifier] => ACL70
            [ticket_identifier] => B14
            [ticket_id] => 14
            [stage_name] => In Progress

        )
)

Here the 0 and the 3rd index are under the same account, but are different ticket - they are linked.

I want to manipulate this array so that all the linked ticket under a account are grouped together in an array, and the rest to show as it is.

foreach($data as $result) {
  if(in_array($value->account_identifier, $result)) {
                echo $value->account_identifier;
  }
}

I am expecting something like this:

[0] => stdClass Object
            (
                [client_id] => 70
                [client_name] => Berws
                [account_identifier] => ACL70

                [ACL70] => (
                       [0] => (
                            [ticket_identifier] => B21
                            [ticket_id] => 21
                            [stage_name] => New
                       )
                       [1] => (
                            [ticket_identifier] => B21
                            [ticket_id] => 21
                            [stage_name] => New
                       )
                 )
            )

I tried using in_array, but it doesn't give me any result.

How should I approach this?

Upvotes: 0

Views: 53

Answers (2)

ptkoz
ptkoz

Reputation: 2507

I don't think you can do this without iterating whole array and check for record duplication manually. You can use array_filter & array_map to speed things up, but that's it.

This will be efficient only if you have a lot of duplicated tickets. If you don't I'd rather advise do make a quick hash map.

$initial = [/* your initial array */];
$merged = []; /* this will be your merged array */
$alreadyMergedClients = []; /* what clients have been already merged */

foreach($initial as $key => $record) {
    /* if client has been already merged, ignore */
    if(in_array($record->client_id, $alreadyMergedClients))
        continue;

    /* search for all clients with ID 70 */
    $clientTickets = array_filter($initial, function($item) use ($record) {
         return $item->client_id == $record->client_id;
    });

    if(count($clientTickets) > 1) {
        /*  there are several tickets, merge */
        $mergedRecord = (object)[
            'client_id' => $record->client_id,
            'client_name' => $record->client_name,
            'account_identifier' => $record->account_identifier
        ];

        $mergedRecord->{$record->account_identifier} = array_map(function($item) {
            return (object)[
                'ticket_identifier' => $item->ticket_identifier,
                'ticket_id' => $item->ticket_id,
                'stage_name' => $item->stage_name
            ];
        }, $clientTickets);

        $merged[$key] = $mergedRecord;
    } else {
        /* there is only one record, live it alone */
        $merged[$key] = $record;
    }   

    $alreadyMergedClients[] = $record->client_id;
}

Note that you need PHP >= 5.3 to use anonymous functions in array_filter and array_map and PHP >= 5.4 to use simplified array notation

Upvotes: 1

Stanimir Dimitrov
Stanimir Dimitrov

Reputation: 1890

You need to use array_merge_recursive

Upvotes: 0

Related Questions