2

I have two arrays - $products and $userProducts (this has to be done with two queries since I am searching for products based on the name, and there is another table where I store products which user wants to sell, and I want to show to the user that he already put this product for sale).

$products array looks like this:

[
    'id' => 1,
    'name' => 'product name',
    'synonim' => 'synonim'...

],
[
    'id' => 2,
    'name' => 'product name 2',
    'synonim' => 'synonim'...

]....

and $userProducts looks like this:

[
    'product_id' => 1 
],
[
    'product_id' => 75
]

I want to add attribute to the $products array (or if there is a better way to do this, that can also work) 'userHasProduct' => true, if $userProducts contains product_id which mathces with id from $products id. How can I make this work?

4
  • 3
    Why does it have to be two separate queries? Because it seems like a left join would solve your problem Commented Sep 8, 2017 at 14:12
  • Check this one php.net/manual/en/control-structures.for.php Commented Sep 8, 2017 at 14:13
  • @Erik - tried this, and I am not getting a result I want (I am not that proficient with MySQL, so it is possible that I don't have the knowledge to write the query I need). Commented Sep 8, 2017 at 14:16
  • @Sasha- I'd recommend asking a new question and share the data structure, and the query you've tried, to get help writing this query. And don't forget about the folks who are trying to help with this question below! Commented Sep 8, 2017 at 14:20

2 Answers 2

4

Agreeing with @Erik that if you can amend your original query then this would probably be best solved in the database layer. If not, then you can use something like this:

$products = array_map(function ($product) use ($userProducts) {
  if (in_array($product['id'], array_column($userProducts, 'product_id'))) {
    $product['found_in_user_products'] = true;
  }

  return $product;
}, $products);

This will loop over each entry in your $products array, and compare the ID to the list in $userProducts. If it's found, then it sets the found_in_user_products key to true.

Working example: https://eval.in/858063

For reference, to solve this in SQL would probably look something like:

   Select p.id, p.name, p.synonim, count(up.product_id)
     From products p
Left Join user_products up On p.id = up.product_id
 Group By p.id, p.name, p.synonim

Although obviously without seeing your schema it's hard to say exactly

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

2 Comments

Nice! I always forget about array_column - worth noting that requires PHP 5.5+ (everyone should be on that, but alas many still are not)
Better move the array_column out of the loop, because the no reason to do the same task each time. (-;
1

If a query LEFT JOIN truly won't solve the problem (which I believe it would, BTW), I would then recommend something like this:

// flatten the array
$ids = array_map( function( $row ) {
    return $row['product_id'];
}, $row );

// loop over products (by reference)
foreach( $products AS &$row ) {
    // if the product ID is in the user's IDs array, set flag
    if ( in_array($row['product_id'], $ids ) {
        $row['userHasProduct'] = TRUE;
    }
}

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.