0

Here is my foreach

<?php 
    foreach ($_productCollection as $_product){
        echo "<PRE>";
        print_r($_product->getData());
    } 

    ?>

foreach Array Output:-

Array
(
    [entity_id] => 85
    [name] => Round Bur

)
Array
(
    [entity_id] => 86
    [name] => testile Bur

)
Array
(
    [entity_id] => 87
    [name] => Shovel

)
Array
(
    [entity_id] => 88
    [name] => Round Bur

)

I want to remove the same name array under foreach for example

Actual Output

Array
(
    [entity_id] => 85
    [name] => Round Bur

)
Array
(
    [entity_id] => 86
    [name] => testile Bur

)
Array
(
    [entity_id] => 87
    [name] => Shovel

)

How to remove the last 1 arrays because of a name as same. please give me a solution

13
  • if you only want to remove last element then use array_pop() Commented Nov 20, 2019 at 12:01
  • @VishnuBhadoriya,I think it's more that the last one (in the example) is the only duplicate, this may not always be the case. Commented Nov 20, 2019 at 12:02
  • @VishnuBhadoriya that won't solve the issue if the dupe was placed as the first and second elements in the array Commented Nov 20, 2019 at 12:02
  • 4
    Possible duplicate of How to remove duplicate values from a multi-dimensional array in PHP Commented Nov 20, 2019 at 12:04
  • 1
    @NigelRen it does, which means the dupe target is valid Commented Nov 20, 2019 at 12:26

3 Answers 3

2

You can put all the results in an array, only adding them if the namedoesn't already exist:

$products = array();
foreach ($_productCollection as $_product){
    $product = $_product->getData();
    if (!in_array($product['name'], array_column($products, 'name'))) {
        $products[] = $product;
    }
} 
echo "<PRE>";
print_r($products);
Sign up to request clarification or add additional context in comments.

Comments

2

You can splice repeated row which contains the same value:

$res_ar = [];     // resultant array
$ar_names = [];   // array of unique names 

foreach($ar as $ind => $row){
    if (in_array($row['name'],$ar_names)){   // if name was before
        array_splice($ar,$ind,1);            // remove element by its index
    } else {
        $ar_names[] = $row['name'];          // add new name to the unique array of names
        $res_ar[] = $row;                    // saving row with new name value
    }
}

Demo

Implementation for your case looks like next:

$res_ar = [];
$ar_names = [];

foreach ($_productCollection as $ind => $_product){

       $row = (array)$_product->getData();

       if (in_array($row['name'], $ar_names)){
           array_splice($_productCollection,$ind,1);
       } else {
           $ar_names[] = $row['name'];
           $res_ar[] = $_product;
       } 
}

Comments

1

One last version, just keep a track of the names added, but also add the original to the output rather than the array of data...

$output = [];
$existingNames = [];
foreach ($_productCollection as $product){
    $productName = $product->getData()['name'];
    if ( !isset($existingNames[$productName]))  {
        $existingNames[$productName] = true;
        $output[] = $product;
    }
}

3 Comments

Weird considering it essentially works the same way as the second answer which got an upvote. happy to compensate...
@Nick, thanks, it's more of a case if there is something wrong with my code I'd rather people say. Although sometimes it may be someone messing around :-/
I couldn't agree with you more. But for the most part that doesn't seem to be how SO works...

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.