RE: [PHP] Arrays : Key in Array, True/False

From: Date: Fri, 30 Mar 2001 10:40:25 +0000
Subject: RE: [PHP] Arrays : Key in Array, True/False
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46295@lists.php.net to get a copy of this message
You need a 2 step approach. 1. extract the keys from your array into another array using array_keys() 2. check the result using in_array() to see if you key exists. -----Original Message----- From: Knut H. Hassel Nielsen [mailto:Knut.H.Hassel.Nielsen@idi.ntnu.no] Sent: 30 March 2001 10:56 To: php-general@lists.php.net Subject: [PHP] Arrays : Key in Array, True/False Hi there I have a script with a function that builds a more comprehensive kind of array for parsing arguments given a script in PHP (this is a backup-script I'm currently working on). My problem is in "THE PARSING OF ARGS PART" when I'm asking the array if the key exists in the array it returns 0. The key exists, I know because of the output from the function ( var_dump($array) ), but the function 'in_array()' only returns true if I ask for a value. Does anyone know if I can use anything else to give me the possibility to also ask for the keys, or do I have to write a 'in_array()'-kind of function myself ?? #! /local/bin/php -q <?PHP // -*- C++ -*- function parse_arg( $argv, $argc ) { $array = Array(); for ( $i = 0 ; $i < $argc ; $i++ ) { if ( ereg( "-", $argv[ $i ] ) ) { if ( ereg( "-", $argv[ $i + 1 ] ) ) { $array[ "$argv[$i]" ] = ""; } else { $array[ "$argv[$i]" ] = $argv[ $i + 1 ]; $i = $i + 1; } } } var_dump ($array); return $array; } // THE PARSING OF ARGS PART $arr = parse_arg( $argv, $argc ); if ( in_array( "-h", $arr ) ) { // This doesnt work print $usage; exit; } elseif ( in_array( "-b", $arr ) ) { // This doesnt work $backup = 1; } elseif ( in_array( "-d", $arr ) ) { // This doesnt work while ( list ( $key, $val ) = each ( $arr ) ) { if ( $key == "-d" ) { if ( $val == "" ) { print "No PATH to switch \"-d\"\nExiting\n"; exit; } else { $dir = $val; if ( !is_dir( $dir ) ) { print "This directory [$dir] doesnt exist\n"; exit; } else { $run = 1; } } } } } ?> -- Knut ------ Knut H. Hassel Nielsen Principal Engineer / Avdelingsingeniør Norwegian University of Science and Technology / NTNU Department of Computer and Information Science / IDI N-7491 Trondheim, Norway Phone Office / Telefon jobb : (+47) 73 59 18 46 Fax Office / Telefax jobb : (+47) 73 59 17 33 Cell. Phone / Mobiltelefon : 91 59 86 06 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#46295) next »