NaGGi Posted December 23, 2009 Share Posted December 23, 2009 Looked all search results but didnt quite found what I was looking for and my noob php brain can't figure the php manual out so I'll just ask. I have an array which have another array inside and Id like to searh a value from that array My array: print_r($product_rows); gives: Array ( [0] => Array ( [row_color] => sectiontableentry1 [product_name] => Lattaaa pew [product_attributes] => [product_sku] => xxxx [product_price] => 45.00 € ) Now I have modifer: $db_sku = xxxx Now I'd like to search if that xxxx value is in that array How that if sentence should be written? Atm my code looks like this but its not working if (in_array(array($product_sku, $db_sku ),$product_rows) ) { echo found; } else { echo not_found; } Any hints? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 this should work if (in_array(array($db_sku ,$product_rows[0]) ) { echo 'found'; } else { echo 'not_found'; } Quote Link to comment Share on other sites More sharing options...
NaGGi Posted December 23, 2009 Author Share Posted December 23, 2009 That gives an warning and not_found result Warning: Wrong parameter count for in_array() in ... not_found Quote Link to comment Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 if (in_array('xxxx', $product_rows[0])) { echo "xxx found"; } Quote Link to comment Share on other sites More sharing options...
NaGGi Posted December 23, 2009 Author Share Posted December 23, 2009 if (in_array('xxxx', $product_rows[0])) { echo "xxx found"; } Oh this simple thing actually works. That [0] tells that there is array in array or why it works? Problem comes when there is bigger array like [1],[2], etc though. Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 23, 2009 Share Posted December 23, 2009 You need a recursive loop/function. <?php function recur_in_array($array,$string){ for($i=0;$i<count($array);$i++){ if(is_array($array[$i])){ $found = recur_in_array($array[$i],$string); if($found === true){ return $found; } }else{ if (in_array($string, $product_rows[0])) { return true; } } } } if(recur_in_array($array_var) === true){ echo("xxx found"); } ?> -CB- Quote Link to comment Share on other sites More sharing options...
NaGGi Posted December 23, 2009 Author Share Posted December 23, 2009 Ok thanks a lot! Gotta check that loop out if I need it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.