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? Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/ 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'; } Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983011 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 Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983029 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"; } Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983031 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. Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983072 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- Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983079 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. Link to comment https://forums.phpfreaks.com/topic/186139-in_array-search-problem/#findComment-983097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.