Jump to content

remove a certain row from results array after query.


jason310771

Recommended Posts

I am looking for a way to remove a certain row from a set of results.

I grab the product title and price and other details including the `product_id` (int)  I am then wanting to remove an item from the results that has a certain `product_id`

This can not be done easily from the query due to me using OpenCart, and the getProducts file is so much a mess in itself that I am lost as to how or where I do this.

So think the best way is to remove the item that I do not need from the results set, as this item is already shown elsewhere on the page and do not want to have duplicates on the page.

I echoed the results array to my page, this is that array, but based on only showing two products, one of which I would need to remove.

I realised that the product_id is not actually used for the array location but as a sub variable.

So in the example array below how would I remove say the first or second product
(the product i am wanting to remove will not be the first item, it could be anywhere in the array)

The product_id is shown in the first part of the array 1720 or 2057.  

array(2) { [1720]=> array(40) { ["product_id"]=> string(4) "1720" ["name"]=> string(26) "aaaaaaaaaaaaaaa" ["description"]=> string(71) "ssssssssssssss" ["meta_description"]=> string(0) "" ["meta_keyword"]=> string(0) "" ["tag"]=> string(0) "" ["model"]=> string(4) "r592" ["sku"]=> string(0) "" ["upc"]=> string(0) "" ["ean"]=> string(0) "" ["jan"]=> string(0) "" ["isbn"]=> string(0) "" ["mpn"]=> string(0) "" ["location"]=> string(0) "" ["quantity"]=> string(3) "100" ["stock_status"]=> string(9) "Pre-Order" ["image"]=> string(25) "data/r592x.jpg" ["manufacturer_id"]=> NULL ["manufacturer"]=> NULL ["price"]=> string(7) "38.9500" ["special"]=> NULL ["reward"]=> NULL ["points"]=> string(1) "0" ["tax_class_id"]=> string(1) "0" ["date_available"]=> string(10) "2013-07-06" ["weight"]=> string(10) "0.15000000" ["weight_class_id"]=> string(1) "1" ["length"]=> string(10) "0.00000000" ["width"]=> string(10) "0.00000000" ["height"]=> string(10) "0.00000000" ["length_class_id"]=> string(1) "0" ["subtract"]=> string(1) "0" ["rating"]=> float(0) ["reviews"]=> int(0) ["minimum"]=> string(1) "1" ["sort_order"]=> string(1) "1" ["status"]=> string(1) "1" ["date_added"]=> string(19) "2013-07-06 14:05:13" ["date_modified"]=> string(19) "2013-07-06 14:05:13" ["viewed"]=> string(1) "4" } [2057]=> array(40) { ["product_id"]=> string(4) "2057" ["name"]=> string(25) "ddddddddddddd" ["description"]=> string(73) "ffffffffffffffffffff" ["meta_description"]=> string(0) "" ["meta_keyword"]=> string(0) "" ["tag"]=> string(0) "" ["model"]=> string(6) "770400" ["sku"]=> string(0) "" ["upc"]=> string(0) "" ["ean"]=> string(0) "" ["jan"]=> string(0) "" ["isbn"]=> string(0) "" ["mpn"]=> string(0) "" ["location"]=> string(0) "" ["quantity"]=> string(3) "100" ["stock_status"]=> string(9) "Pre-Order" ["image"]=> string(27) "data/770400x.jpg" ["manufacturer_id"]=> NULL ["manufacturer"]=> NULL ["price"]=> string(6) "4.5000" ["special"]=> NULL ["reward"]=> NULL ["points"]=> string(1) "0" ["tax_class_id"]=> string(1) "0" ["date_available"]=> string(10) "2013-07-06" ["weight"]=> string(10) "0.10000000" ["weight_class_id"]=> string(1) "1" ["length"]=> string(10) "0.00000000" ["width"]=> string(10) "0.00000000" ["height"]=> string(10) "0.00000000" ["length_class_id"]=> string(1) "0" ["subtract"]=> string(1) "0" ["rating"]=> float(0) ["reviews"]=> int(0) ["minimum"]=> string(1) "1" ["sort_order"]=> string(1) "1" ["status"]=> string(1) "1" ["date_added"]=> string(19) "2013-07-06 14:05:13" ["date_modified"]=> string(19) "2013-07-06 14:05:13" ["viewed"]=> string(1) "4" } }
Link to comment
Share on other sites

This isn't tested at all, but should give the foundation of what you need.

 

foreach($results as $recKey => $product){
  foreach ($product as $prodElem => $prodVal)
    if($prodElem == "product_id" && $prodVal == $matchToRemove){
      $keyToRemove[] = $recKey;
    }
  }
}
foreach($keyToRemove as $key=>value){
  unset($results, $key);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.