0

How to remove multi dimensional array if duplicate. In this example Barcode is duplicate value of 111 i want to remove this if found duplicate. PLease help Im new to php. Thanks

Output:

 Array 
(
[0] => Array
    (
        [Barcode] => 111
        [Transaction_No] => 256
    )

[1] => Array
    (
        [Barcode] => 111
        [Transaction_No] => 0
    )

[2] => Array
    (
        [Barcode] => 222
        [Transaction_No] => 0
    )

)

Expected Output:

 Array 
(
[0] => Array
    (
        [Barcode] => 222
        [Transaction_No] => 0
    )

)
4
  • You want to remove duplicate value / both same value? Commented Jul 22, 2016 at 5:30
  • What I want is to remove this array 2 array. Actually its dynamic meaning when found barcode duplicate remove all duplicate Commented Jul 22, 2016 at 5:41
  • Means you want to remove same barcode value array right? Commented Jul 22, 2016 at 5:43
  • Yes Correct Nikhil Vaghla. if all are the same then remove all Commented Jul 22, 2016 at 5:45

2 Answers 2

1

This keeps track of the keys of each barcode array item to find duplicates, then uses array_values at the end to fix the array indexing.

<?php
$myArray = array(
    array
    (
        "Barcode" => 111,
        "Transaction_No" => 256
    ),
    array
    (
        "Barcode" => 111,
        "Transaction_No" => 0
    ),
    array
    (
        "Barcode" => 222,
        "Transaction_No" => 0
    )
);

$barcodeKeys = array();
foreach ($myArray as $key => $arr) {
    $code = $arr["Barcode"];
    if (!isset($barcodeKeys[$code])) {
        $barcodeKeys[$code] = array();
    }

    $barcodeKeys[$code][] = $key;

    if (count($barcodeKeys[$code]) > 1) {
        foreach ($barcodeKeys[$code] as $dupKey) {
            if (isset($myArray[$dupKey])) {
                unset($myArray[$dupKey]);
            }
        }
    }
}

$myArray = array_values($myArray);
print_r($myArray);

Output

Array
(
    [0] => Array
        (
            [Barcode] => 222
            [Transaction_No] => 0
        )

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

1 Comment

Woah! Thanks ebildude123 expected output correct. Thanks a lot you save my day! Thanks
1

Fast approach to your question:

<?php
$barcodes =  array(array( 'Barcode' => 111,'Transaction_No' => 256),array('Barcode' => 111,'Transaction_No' => 0),array('Barcode' => 222,'Transaction_No' => 0),array('Barcode' => 333,'Transaction_No' => 0)); 
$result = array();
$exist = array();
foreach($barcodes as $key => $item){
    if( in_array( $item['Barcode'], array_values( $exixt ) ){
         unset( $result[ array_search ( $item['Barcode'], $exist ) ] );
    } else {
        $result[ $key ] = array('Barcode' => $item['Barcode'],'Transaction_No' => $item['Transaction_No'] );
        $exist[ $item['Barcode'] ] = $key;
    }
}

var_dump($result);

2 Comments

Where did you get the $barcode variable? it does not work
Hi xAqweRx thanks for the code. ebildude123 code is much near to my expectation. Anyway thanks for suggestion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.