Jump to content

search in muti-array


jeeva

Recommended Posts

hi there,

 

I have a array like

$arr[0]=array("name"=>"jeeva","gender"=>"M","age"=>"26");
$arr[1]=array("name"=>"mercy","gender"=>"F","age"=>"16");
$arr[2]=array("name"=>"priya","gender"=>"F","age"=>"46");

and i need to search within the array and get the matching record like,

i will search like below

$name="jeeva";
$gend="M";

And it should give result like

 

$arr[0]=array("name"=>"jeeva","gender"=>"M","age"=>"26");

 

 

Could you please give me a solution?

 

 

Jeeva

 

Link to comment
https://forums.phpfreaks.com/topic/198998-search-in-muti-array/
Share on other sites

hi there,

 

I have a array like

$arr[0]=array("name"=>"jeeva","gender"=>"M","age"=>"26");
$arr[1]=array("name"=>"mercy","gender"=>"F","age"=>"16");
$arr[2]=array("name"=>"priya","gender"=>"F","age"=>"46");

and i need to search within the array and get the matching record like,

i will search like below

$name="jeeva";
$gend="M";

And it should give result like

 

$arr[0]=array("name"=>"jeeva","gender"=>"M","age"=>"26");

 

 

Could you please give me a solution?

 

 

Jeeva

 

You could use a foreach to go through each one and ask if the name and gender are the same as what you are searching for. Is this a serious question? If you need to make a more efficient search then you should ensure that the array is first sorted in some manner and then modify one of the existing search types to do what you need.

jeeva,

 

This works:

 


<?php

Header("Cache-control: private, no-cache");
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header("Pragma: no-cache");

$arr      = array();
$matchOut = array();
$match    = array('jeeva', 'M');

$arr[0]=array("name"=>"jeeva","gender"=>"M","age"=>"26");
$arr[1]=array("name"=>"mercy","gender"=>"F","age"=>"16");
$arr[2]=array("name"=>"priya","gender"=>"F","age"=>"46");
$arr[3]=array("name"=>"jeeva","gender"=>"M","age"=>"26");

foreach ($arr as $IDX1 => $outCandidate) {

	$result = findMatch($outCandidate, $match);

	if ($result) {

		$matchOut[$IDX1] = $result;
	}

}

$tempOutput = NULL;
$lineOut    = array();

if (!empty($matchOut)) {

	foreach ($matchOut as $IDX2 => $matchCandidate) {

		$output = NULL;

		foreach($matchCandidate as $itemOut) {

			foreach ($itemOut as $lineOutCandidate) {
				$output .= $lineOutCandidate . ', ';
			}

			$strLen = strlen($output);

			$last   = $strLen - 2; // Get rid of trailing ', '...

			$lineOut[$IDX2] = '$arr[' . $IDX2 . ']' . ' : ' . substr($output,0,$last) . '<br /><br />';

			$thisLine = $lineOut[$IDX2];

		}

	}

	$matchString = NULL;

	foreach ($match as $IDX3 => $inputParm) {

		$whichArrayItem = '$arr[' . $IDX1 .']';

		$matchString .= $inputParm . ', ';

	}

	$strlen = strlen($matchString);

	$last = $strlen - 2; // Get rid of trailing ', '

	$matchString = substr($matchString,0,$last);

	echo "Match Found for : '" . $matchString . "' in : <br /><br /><ul> \n";

	foreach($lineOut as $echoCandidate) {
		echo $echoCandidate;
	}

}
else {
	echo 'No Matches Found...';
}

function findMatch($outCandidate,$match) {

	$matchName   = $match[0];
	$matchGender = $match[1];
	$matchFound  = array();

	if ( (in_array($matchName, $outCandidate)) && (in_array($matchGender, $outCandidate))) {
		$matchFound[] = $outCandidate;
	}

	if (!empty($matchFound)) {
		return $matchFound;
	}
	else {
		return FALSE;
	}


}

?>

 

Scot L. Diddle, Richmond VA

 

Archived

This topic is now archived and is closed to further replies.

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