0

I have an array that contains multiple transactions, with multiple transactions being from the same email address.

Some of these transactions have a value in the client key. Others don't.

I want to copy the filled client key in all matching arrays with the same email_address key, in order to always have data in the client key.

Example of my array:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => [email protected]
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => [email protected]
        )

    [5] => Array
        (
            [client] => 
            [email_address] => [email protected]
        )

)

What I want to achieve is to make sure that Array [5] has the same value in the client key as Array [1] because it's the same client (based on email_address key).

Example of the resulted array:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => [email protected]
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => [email protected]
        )

    [5] => Array
        (
            [client] => John John
            [email_address] => [email protected]
        )

)

How can I go through the array to make sure that matching arrays (based on email_address key) always have the same client key?

3
  • How much client you want to fill... Commented Jun 5, 2016 at 8:49
  • @RivnatNasah All, whatever data a filled client key contains. So, John John from Array [1] to be John John in Array [5] Commented Jun 5, 2016 at 8:52
  • So what's your question? "I want to do X" or "Write me code that does X" is not a question. Commented Jun 5, 2016 at 8:54

2 Answers 2

1

The solution using array_column, array_unique, array_flip, array_count_values, array_filter and array_intersect_key functions (This solution is also well-suited for processing multiple groups of "client" entries with same "email_address"):

// supposing $arr is your initial array

$ties = array_flip(array_unique(array_column($arr, "email_address", "client")));
$counts = array_filter(array_count_values(array_column($arr, "email_address")), function($v){
    return $v > 1;  // getting number of entries with same 'email' attribute
});    
$relations = array_intersect_key($ties, $counts);   // contains pairs of relative email/client entries, like "[[email protected]] => John John"

foreach ($arr as &$client) {
    if (!$client['client'] && key_exists($client['email_address'], $relations)) {
        $client['client'] = $relations[$client['email_address']];
    }
}

print_r($arr);

The output:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => [email protected]
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => [email protected]
        )

    [5] => Array
        (
            [client] => John John
            [email_address] => [email protected]
        )
)
Sign up to request clarification or add additional context in comments.

Comments

0

You can make an intermediate array that relates email addresses to client. Like this:

$intermediateArray = [];

foreach ($inputArray as $row) {
  if (isset($row['client']) {
    $intermediateArray[$row['email_address']] = $row['client']
  }
}

and then fill in the missing client cells:

foreach ($inputArray as $row) {
  if (!isset($row['client']) {
    if (isset($intermediateArray[$row['email_address']])) {
      $row['client'] = $intermediateArray[$row['email_address']];
    }   
  }
}

More efficient solutions are possible, but this one seems the simplest.

Comments

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.