note 38879 modified in function.array-intersect by vrana

From: Date: Tue, 17 Aug 2004 13:58:23 +0000
Subject: note 38879 modified in function.array-intersect by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79901@lists.php.net to get a copy of this message
This is also handy for testing an array for one of a series of acceptable elements. As a simple example, if you're expecting the query string to contain one of, say, user_id, order_id or item_id, to find out which one it is you could do this: <?php $valid_ids = array ('user_id', 'item_id', 'order_id'); if ($id = current (array_intersect ($valid_ids, array_keys ($_GET)))) { // do some stuff with it } else // error - invalid id passed, or none at all ?> ...which could be useful for constructing an SQL query, or some other situation where testing for them one by one might be too clumsy. --was-- This is also handy for testing an array for one of a series of acceptable elements. As a simple example, if you're expecting the query string to contain one of, say, user_id, order_id or item_id, to find out which one it is you could do this: $valid_ids = array ('user_id', 'item_id', 'order_id'); if ($id = current (array_intersect ($valid_ids, array_keys ($_GET)))) { // do some stuff with it } else // error - invalid id passed, or none at all ...which could be useful for constructing an SQL query, or some other situation where testing for them one by one might be too clumsy. http://php.net/manual/en/function.array-intersect.php

« previous php.notes (#79901) next »