Finding a multidimensional array for a string and obtaining a key

I use Api PDO and using fetchAll() returns multidimensional size; This snippet below is just a test case; I just want to know if this is possible.

 $LeUsername = "BravoSlayer"; $sth = $dbh->prepare("SELECT * FROM users WHERE Username='$LeUsername'"); $sth->execute(); $result = $sth->fetchAll(); print_r($result); $ArraySearch = search_array($result, $LeUsername); 

The output is as follows:

 Array ( [0] => Array ( [ID] => 1 [0] => 1 [Username] => bravoslayer [1] => bravoslayer [Password] => thisisatest [2] => thisisatest ) ) 

I want to search through a multidimensional array in order to return the key. In this case, it will be 0, so I can just bind another variable variable for $ Array1 = $ Array1 ['0'], so from this I can do:

 $Username = $Array1['Username']; 
+4
source share
1 answer

Judging by your question. You can search for the primary array in the foreach and use in_array to return the correct array.

Use this as your link.

 function Search_Array($Array, $SearchDilema) { foreach ($Array AS $CheckKeys) { if (in_array($SearchDilema, $CheckKeys)) { return $CheckKeys; } else { $ErrorMsg = "No Results Found! Check Your Search Dilema"; return $ErrorMsg; } } } 
0
source

All Articles