Array ( [rowid] => a87ff679a2f3e71d9181a67b7542122c [id] => 4 [qty] => 1 [price] => 12.95" name="description" /> Array ( [rowid] => a87ff679a2f3e71d9181a67b7542122c [id] => 4 [qty] => 1 [price] => 12.95" />
38210

If Key Value pair exists in multidimensional array.. How to?

Question:

I have a codeigniter shopping cart going and its "cart" array is the following:

Array ( [a87ff679a2f3e71d9181a67b7542122c] => Array ( [rowid] => a87ff679a2f3e71d9181a67b7542122c [id] => 4 [qty] => 1 [price] => 12.95 [name] => Maroon Choir Stole [image] => 2353463627maroon_3.jpg [custprod] => 0 [subtotal] => 12.95 ) [8f14e45fceea167a5a36dedd4bea2543] => Array ( [rowid] => 8f14e45fceea167a5a36dedd4bea2543 [id] => 7 [qty] => 1 [price] => 12.95 [name] => Shiny Red Choir Stole [image] => 2899638984red_vstole_1.jpg [custprod] => 0 [subtotal] => 12.95 ) [eccbc87e4b5ce2fe28308fd9f2a7baf3] => Array ( [rowid] => eccbc87e4b5ce2fe28308fd9f2a7baf3 [id] => 3 [qty] => 1 [price] => 14.95 [name] => Royal Blue Choir Stole [image] => 1270984005royal_vstole.jpg [custprod] => 1 [subtotal] => 14.95 )

)

My goal is to loop through this multidimensional array some how and if ANY product with the key value pair "custprod == 1" exists, then my checkout page will display one thing, and if no custom products are in the cart it displays another thing. Any help is appreciated. Thanks.

Answer1:

Rather than looping over it, you can check for the custprod key using <a href="http://www.php.net/manual/en/function.array-key-exists.php" rel="nofollow">array_key_exists</a>. Or simply check to see if arr['custprod'] <a href="http://php.net/manual/en/function.isset.php" rel="nofollow">isset</a> (both functions handle null differently).

$key = "custprod"; $arr = Array( "custprod" => 1, "someprop" => 23 ); if (array_key_exists($key, $arr) && 1 == $arr[$key]) { // 'custprod' exists and is 1 }

Answer2:

function item_exists($cart, $custprod) { foreach($cart as $item) { if(array_key_exists("custprod", $item) && $item["custprod"] == $custprod) { return true; } } return false; }

Now, you can use this function to check if product exist in stack:

if(item_exists($cart, 1)) { // true } else { // false }

Answer3:

You still need to loop the array to check it:

$cust_prod_found = false; foreach($this->cart->contents() as $item){ if (array_key_exists("custprod", $item) && 1 == $item["custprod"]) { $cust_prod_found = true; break; } } if ($cust_prod_found) { // display one thing } else { // display another thing }

Recommend

  • Keyboard shortcuts to trigger reactive flows in R Shiny?
  • R shinydashboard: display progress bar while loading data (fread)
  • Using glob() to display images from a directory while echo'ing a unique first image
  • How to target a single image map area when multiple image maps are on the same page with jQuery
  • reload the page using ajax in order to create a live chat
  • Read csv from zip into shiny app without relying on local directories
  • How to use OpenCV in python 3.4 on windows 7 x64? [duplicate]
  • How To Understand dynamic HTML in Visual Studio 2008?
  • Check for file existence if Laravel's Blade template
  • Android WebView loading GoogleMap Data Rendering
  • Can I used mod_rewrite to change file extensions? .jpeg to .jpg for example
  • how to parse HTTP POST(file upload) stream?
  • How do I get sikuli to wait until the mouse pointer changes from “busy” to “not busy?”
  • How do I extract xml properties using shell scripts?
  • Andengine loading graphics: why is my background texture small and upside down
  • How to make image control button for each image inside the div using css and js?
  • R shiny - last clicked button id
  • boxplot won't display with ggvis
  • Directory structure and labeling in Caffe
  • Convert base64 image to a file in Node Js
  • Python Pillow: Make gradient for transparency
  • Problems using HOGDescriptor
  • Different SSL-certificates for different parts of site
  • How can I stop .htaccess redirecting a subdomain to the main site?
  • Httpclient multipart/form-data post image and json same time
  • Intent Image Capture data==null
  • Selenium c#: WaitForCondition (how to find when ajax page is fully loaded)
  • C# Remove URL from String
  • PHP Copy function not working
  • How to set download location via chrome api
  • PHP file_exists() anomaly
  • MVVM: Image Bind Source from FileOpenPicker
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Highlight and Bold text in JTextPane
  • Syntax for setting draggablecursor property in google maps api
  • Xamarin Forms - UWP Fonts
  • Resize panoramic image to fixed size
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Android Google Maps API OnLocationChanged only called once