0

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?

2
  • First, I need to check if the same account exists in the array. Without that how would I merge? Commented Jun 29, 2015 at 13:18
  • Please post the array structure you want to receive, by now it's somewhat unclear to me. Commented Jun 29, 2015 at 13:20

2 Answers 2

1

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

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

Comments

0

You need to use array_merge_recursive

1 Comment

But, array_merge_recursive merges the array based on the keys. Here I want to merge based on the values.

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.