note 30385 added to function.array-search

From: Date: Sun, 16 Mar 2003 10:53:28 +0000
Subject: note 30385 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-45384@lists.php.net to get a copy of this message
I wanted to search a multidimensional array for a value & assign another element of array CONTAINING the searched value. In the array I am using ($Projects), each item in $Projects contains a "Text_ID" & "Text_Value" field from a database result. I want to search for a "Text_ID" value (456 in this example) & get it's "Text_Value" value assigned to a variable called $Text_Value. Here's how I did it: ============================================ <?PHP //Array I am going to look in: $Projects[0] = array(123, "Text 1"); $Projects[1] = array(456, "Text 2"); $Projects[2] = array(789, "Text 3"); // This loop goes through every element in $Projects foreach ($Projects as $key => $ArrayRow) { // Now: $ArrayRow[0] = $Projects[x][0] and $ArrayRow[1] = $Projects[x][1] // Look for the value "456". Assign to variable & stop looking if found. if ($ArrayRow[0] == "456") {$Text_Value= $ArrayRow[1]; break; } } ?> ============================================ Hope this helps someone. -Darkive -- http://www.php.net/manual/en/function.array-search.php http://master.php.net/manage/user-notes.php?action=edit+30385 http://master.php.net/manage/user-notes.php?action=delete+30385 http://master.php.net/manage/user-notes.php?action=reject+30385

« previous php.notes (#45384) next »