nhsal69 Posted May 5, 2010 Share Posted May 5, 2010 Hi All, I'm pretty new to PHP and am having an issue with searching an array for each occurrence of a specific value and then outputting the all entries within that key... so here is the array, it's scraped from a web site using Table Extractor, a print_r ($haystack) gives you this: Array ( [1] => Array ( [Name ] => Aberdeen [status ] => Ready [Location ] => [Jobs ] => 0 [Model ] => HPUniversalPrintingPCL6 [Comment ] => PRINT_EARL ) [2] => Array ( [Name ] => Alabama [status ] => Ready [Location ] => GroundFloorArea012 [Jobs ] => 0 [Model ] => RICOHAficioSP4100NPCL6 [Comment ] => PRINT_Grd_F ) [3] => Array ( [Name ] => Almond [status ] => Ready [Location ] => [Jobs ] => 0 [Model ] => HPUniversalPrintingPCL6 [Comment ] => PRINT_EARL ) ) You can see it's a multidimensional array and some of the values are blank (this might be the issue?!) Now that code I have is below, but it doesn't return any values and I can't see why not, as I said I'm new to this and having basically been winging it... $haystack = $tx->extractTable(); $needle = "PRINT_EARL"; echo $needle; print_r ($haystack); multidimArrayLocate($haystack,$needle); function multidimArrayLocate($haystack, $needle){ foreach($haystack as $key => $arrayValue){ if (is_array($arrayValue)){ if ($key == $needle) $arrayResult[$key] = $arrayValue; $temp[$key] = multidimArrayLocate($arrayValue, $needle); if ($temp[$key]) $arrayResult[$key] = $temp[$key]; } else{ if ($key == $needle) $arrayResult[$key] = $arrayValue; } } return $arrayResult; } Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/ Share on other sites More sharing options...
litebearer Posted May 5, 2010 Share Posted May 5, 2010 just a thought... the entry from "giulio dot provasi at gmail dot com" at http://php.net/manual/en/function.array-search.php Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1053601 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 Well the function only returns the array produced, so you shouldn't see any output. print_r(multidimArrayLocate($haystack,$needle)); Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1053603 Share on other sites More sharing options...
nhsal69 Posted May 6, 2010 Author Share Posted May 6, 2010 thanks for your thoughts but quite clearly I'm being dense here... The function "multidimArrayLocate($haystack, $needle)" should return keys [1] and [3] in $arrayvaule theses should be displayed with print_r($arrayvalue), shouldn't they?! Or am I misising something obvious? As I said, completely new to this PHP stuff.. Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1053978 Share on other sites More sharing options...
sasa Posted May 6, 2010 Share Posted May 6, 2010 try <?php $haystack = Array ( 1 => Array ( 'Name' => 'Aberdeen', 'Status' => 'Ready', 'Location' =>'', 'Jobs' => 0, 'Model' => 'HPUniversalPrintingPCL6', 'Comment' => 'PRINT_EARL' ), 2 => Array ( 'Name' => 'Alabama', 'Status' => 'Ready', 'Location' => 'GroundFloorArea012', 'Jobs' => 0, 'Model' => 'RICOHAficioSP4100NPCL6', 'Comment' => 'PRINT_Grd_F' ), 3 => Array ( 'Name' => 'Almond', 'Status' => 'Ready', 'Location' => '', 'Jobs' => 0, 'Model' => 'HPUniversalPrintingPCL6', 'Comment' => 'PRINT_EARL' ), 'PRINT_EARL' => array(1,2,3) ); $needle = "PRINT_EARL"; echo $needle; //print_r ($haystack); $x = my_multidimArrayLocate($haystack,$needle); print_r($x); function my_multidimArrayLocate($haystack, $needle){ $arrayResult = array(); if (!is_array($haystack)) return false; foreach($haystack as $key => $arrayValue){ if ((string) $key == (string) $needle) $arrayResult[$key] = $arrayValue; else { if (is_array($arrayValue)){ if ($x = my_multidimArrayLocate($arrayValue, $needle)) $arrayResult[$key] = $x; } else if ((string) $needle == (string) $arrayValue) $arrayResult[$key] = $arrayValue; } } return $arrayResult; } $x = 'as' == 0; var_dump($x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1054191 Share on other sites More sharing options...
nhsal69 Posted May 7, 2010 Author Share Posted May 7, 2010 Thanks for that.. It works on the array supplied above and produces: PRINT_EARLArray ( [1] => Array ( [Comment] => PRINT_EARL ) [3] => Array ( [Comment] => PRINT_EARL ) [PRINT_EARL] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) bool(true) I want it to return all the vaules in the array that has "PRINT_Earl" as an element, so in this case all elements of [1] and [3], but I'm sure I can figure that out.. However, if I run you code on the original "table extracted" array, it returns only: Array ( ) bool(true) Any ideas why?? Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1054468 Share on other sites More sharing options...
nhsal69 Posted May 7, 2010 Author Share Posted May 7, 2010 However, if I run you code on the original "table extracted" array, it returns only: Array ( ) bool(true) Any ideas why?? I have just noticed the "Table Extract" returns and array formed with []'s [1] => Array ( [Name ] => Aberdeen etc etc.... Is this why I don't get any output from your code with the extracted array, but I do when I used your formatted array 1 => Array ( 'Name' => 'Aberdeen', etc etc.... Are the square brackets the problem ?? ?? Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1054655 Share on other sites More sharing options...
sasa Posted May 8, 2010 Share Posted May 8, 2010 try to add $arrayValue=trim($arrayValue); to the begin of foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1054917 Share on other sites More sharing options...
nhsal69 Posted May 10, 2010 Author Share Posted May 10, 2010 Thanks for your thoughts.... After putting $arrayValue=trim($arrayValue); I get the below notice for each iteration of the foreach loop and then it returns "Array ( ) bool(true)" but no data, any other thoughts?? Notice: Array to string conversion in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\extract15.php on line 50 Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1055696 Share on other sites More sharing options...
sasa Posted May 11, 2010 Share Posted May 11, 2010 ups i mean $arrayValue = is_array($arrayValue) ? $arrayValue : trim($arrayValue); Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1056339 Share on other sites More sharing options...
nhsal69 Posted May 13, 2010 Author Share Posted May 13, 2010 Thanks very much for that.. now sorted... Quote Link to comment https://forums.phpfreaks.com/topic/200796-search-an-array-for-needle/#findComment-1057619 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.