3

I have an array data like this

Array
(
    [0] => Array
        (
            [MACHINE] => A1
            [LOT] => B1077
            [slot] => 1
        )
    [1] => Array
        (
            [MACHINE] => A2
            [LOT] => B0229           
            [slot] => 2
        )
    [2] => Array
        (
            [MACHINE] => A2
            [LOT] => B0132
            [slot] => 2
        )
    [3] => Array
        (
            [MACHINE] => A2
            [LOT] => B3967
            [slot] => 2
        )               
    [4] => Array
        (
            [MACHINE] => A3
            [LOT] => B2644
            [slot] => 3
        )
)

in array, the "machine" and "slot" have duplicate values. I want keep only one value of "machine" and "slot" then push "lot" value of duplicate "machine" and "slot" into it. The result I want like this:

Array
(
    [0] => Array
        (
            [MACHINE] => A1
            [LOT] => B1077
            [slot] => 1
        )
    [1] => Array
        (
            [MACHINE] => A2
            [LOT] => B0229           
            [slot] => 2
            [Duplicate] => B0132, B3967
        )   
    [2] => Array
        (
            [MACHINE] => A3
            [LOT] => B2644
            [slot] => 3
        )
)

I have tried this to remove repeat value, but have no idea about how to get the result I want.

$temp = array();
foreach ($array as $v) {
    if (!isset($temp[$v['MACHINE']]))
        $temp[$v['MACHINE']] = $v;
}
$result = array_values($temp);
 echo '<pre>', print_r($result, true), '</pre>';
2
  • 1
    As soon as you will reach a large amount of data, what you are trying to do will be slow and unmaintainable. Your data structure is not adapted to that. Instead index by key, not by iterable. Or use a database and query structures. Commented Nov 5, 2020 at 3:05
  • @NVRM thanks for your suggestion, although this is for small scale of data but I glad to learn more about program skill. Could you please explain more or give an example about "Instead index by key, not by iterable."? I'm newbie in programming. Commented Nov 5, 2020 at 3:29

2 Answers 2

1

You are on the right track with your code, you just need to

  • take account of the slot when making the key for $temp; and
  • add a Duplicate entry when you encounter one

For example:

$temp = array();
foreach ($array as $v) {
    $key = $v['MACHINE'] . '#' . $v['slot'];
    if (!isset($temp[$key])) {
        $temp[$key] = $v;
    }
    else {
        $temp[$key]['Duplicate'][] = $v['LOT'];
    }
}
foreach ($temp as &$v) {
    if (isset($v['Duplicate'])) $v['Duplicate'] = implode(', ', $v['Duplicate']);
}
$result = array_values($temp);
print_r($result);

I've pushed duplicate values into an array, and then used implode in a second loop to make the comma separated string in your desired output. You might find an array is actually more useful, in which case you can leave out the second loop. You can also do the string concatenation on the fly in the loop, if you replace

$temp[$key]['Duplicate'][] = $v['LOT'];

with

$temp[$key]['Duplicate'] = isset($temp[$key]['Duplicate']) ? $temp[$key]['Duplicate'] . ',' . $v['LOT'] : $v['LOT'];

Output:

Array
(
    [0] => Array
        (
            [MACHINE] => A1
            [LOT] => B1077
            [slot] => 1
        )
    [1] => Array
        (
            [MACHINE] => A2
            [LOT] => B0229
            [slot] => 2
            [Duplicate] => B0132, B3967
        )
    [2] => Array
        (
            [MACHINE] => A3
            [LOT] => B2644
            [slot] => 3
        )
)

Demo on 3v4l.org

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

1 Comment

Thanks for the rapidly reply, the code is clean and easy to know.
1

You could utilize the fact that arrays in PHP can be used as Hashmaps that you might be familiar with from other programming languages:

<?php

function condense_duplicates($arrays) {
  $machine_slot_map = array();
  
  foreach ($arrays as $arr) {
    $machine_slot_key = "{$arr['MACHINE']}_{$arr['slot']}";
    if (array_key_exists($machine_slot_key, $machine_slot_map)) {
      $lot = $arr['LOT'];
      if (array_key_exists('Duplicate', $machine_slot_map[$machine_slot_key])) {
        $curr_duplicates = $machine_slot_map[$machine_slot_key]['Duplicate'];
        $machine_slot_map[$machine_slot_key]['Duplicate'] = "{$curr_duplicates}, {$lot}";
      } else {
        $machine_slot_map[$machine_slot_key]['Duplicate'] = $lot;
      }
    } else {
      $machine_slot_map[$machine_slot_key] = $arr;
    }
  }

  return array_values($machine_slot_map);
}

$arrays = array (
  array('MACHINE' => 'A1',
        'LOT' => 'B1077',
        'slot' => 1),
  array('MACHINE' => 'A2',
        'LOT' => 'B0229',
        'slot' => 2),
  array('MACHINE' => 'A2',
        'LOT' => 'B0132',
        'slot' => 2),
  array('MACHINE' => 'A2',
        'LOT' => 'B3967',
        'slot' => 2),
  array('MACHINE' => 'A3',
        'LOT' => 'B2644',
        'slot' => 3)
);

// echo 'Before:', PHP_EOL;
// print_r($arrays);

$arrays_with_condensed_duplicates = condense_duplicates($arrays);

// echo 'After:', PHP_EOL;
print_r($arrays_with_condensed_duplicates);

Output:

Array
(
    [0] => Array
        (
            [MACHINE] => A1
            [LOT] => B1077
            [slot] => 1
        )

    [1] => Array
        (
            [MACHINE] => A2
            [LOT] => B0229
            [slot] => 2
            [Duplicate] => B0132, B3967
        )

    [2] => Array
        (
            [MACHINE] => A3
            [LOT] => B2644
            [slot] => 3
        )

)

Demo on repl.it

Demo on 3v4l.org

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.