Jump to content

Search an array for needle


nhsal69

Recommended Posts

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...

 

 

 

 

Link to comment
Share on other sites

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..  :confused:

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

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 ??  :confused: ??

Link to comment
Share on other sites

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

 

 

 

 

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.