0

I know this is a very common and repeated subject, but after spending some hours of googling I didn't find the right solution for my problem.

Using a foreach loop I'm filling an array, each row including a key and value. I just want to prevent duplicates or remove them.

$data['products'] = array();
$data['stores'] = array();

foreach ($products as $product) {
    //if (!in_array($product['store_id'], $data['stores'])) {
    if (!array_key_exists($product['store_id'], $data['stores'])) {
        $data['stores'][] = array(
            'store_id'   => $product['store_id'],
            'store_name' => $product['store_name']
        );
    }
}

The result of print_r($data['stores']):

Array
(
    [0] => Array
        (
            [store_id] => 1
            [store_name] => Karaca
        )

    [1] => Array
        (
            [store_id] => 1
            [store_name] => Karaca
        )

    [2] => Array
        (
            [store_id] => 7
            [store_name] => nurteks
        )

    [3] => Array
        (
            [store_id] => 7
            [store_name] => nurteks
        )
)

I tried all suggestions, but I don't know how to figure it out.
Thanks for any kind help!

1
  • Is this $products being generated by an sql resultset? If so, please post that query. $data ['products'] doesn't need to be in your question -- you don't use it. Please post the full data that is in $products. I have a sneaking suspicion that you have received fast/poor answers (in a hurry to earn rep points) that have not considered what is best for you, your project, and future SO readers. After posting this critical information, you may discover a more professional method to employ. Commented Nov 3, 2017 at 20:50

2 Answers 2

4

Use the store ID as the key in the stores array. This will ensure that the entries are unique since array keys are inherently unique.

foreach ($products as $product) {
    $data['stores'][$product['store_id']] = array(
        'store_id'   => $product['store_id'],
        'store_name' => $product['store_name']
    );
}

The two things you tried didn't work because:

  1. if (!in_array($product['store_id'], $data['stores'])) {...
    The "needle" ($product['store_id']) is an integer, but the "haystack" ($data['stores']) contains arrays.

  2. if (!array_key_exists($product['store_id'], $data['stores'])) {
    You aren't specifying array keys when you use $data['stores'][] to append items, so the store id won't be found as an array key, or if it is, it won't really be the store id, it will just be the automatically assigned sequential index that just coincidentally matches a store id.

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

2 Comments

OMG! I really appreciate for your help! That was so easy and I spend so long time for it!!!? :-(
Of course I'll. I'm waiting for time limit... 3 min is left.
0

Another solution could be saving the store Ids and test if it's exist or not

$storeIds = [];
foreach ($products as $product) {
    if(!empty( $storeIds[$product['store_id']])){
        //the store is taken before
    }else{
        // new store
        $data['stores'][] = array(
            'store_id'   => $product['store_id'],
            'store_name' => $product['store_name']
        );
    }
    $storeIds[$product['store_id']] = true;//save the store id

}

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.