Bonjour,
Je cherche à trouver le champ first_name dont le nom est "NICK" dans un array peuplé grace une requête mysql.
J'aimerais trouver le first_name grace à son ID (actor_id).
$arr = array(5) { [0]=> array(4) { ["actor_id"]=> int(1) ["first_name"]=> string(8) "PENELOPE" ["last_name"]=> string(7) "GUINESS" ["last_update"]=> string(19) "2006-02-15 03:34:33" } [1]=> array(4) { ["actor_id"]=> int(2) ["first_name"]=> string(4) "NICK" ["last_name"]=> string(8) "WAHLBERG" ["last_update"]=> string(19) "2006-02-15 03:34:33" } [2]=> array(4) { ["actor_id"]=> int(3) ["first_name"]=> string(2) "ED" ["last_name"]=> string(5) "CHASE" ["last_update"]=> string(19) "2006-02-15 03:34:33" } [3]=> array(4) { ["actor_id"]=> int(4) ["first_name"]=> string(8) "JENNIFER" ["last_name"]=> string(5) "DAVIS" ["last_update"]=> string(19) "2006-02-15 03:34:33" } [4]=> array(4) { ["actor_id"]=> int(5) ["first_name"]=> string(6) "JOHNNY" ["last_name"]=> string(12) "LOLLOBRIGIDA" ["last_update"]=> string(19) "2006-02-15 03:34:33" } }
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 $pdo = "mysql:host=localhost;dbname=sakila;charset=utf8mb4"; $options = [ PDO::ATTR_EMULATE_PREPARES => false, // turn off emulation mode for "real" prepared statements PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array ]; try { $conn = new PDO($pdo, "root", "", $options); } catch (Exception $e) { error_log($e->getMessage()); exit('Something weird happened'); //something a user can understand } $stmt = $conn->prepare("SELECT * FROM actor limit 5"); $stmt->execute(); $arr = $stmt->fetchAll(PDO::FETCH_ASSOC); if(!$arr) exit('No rows'); $key = array_search("NICK", array_column($arr, 'first_name')); print_r($key);
Merci pour vos lumières:
Partager