RE: [PHP] Re: Is it *really* an associative array?

2001-09-18 Thread Boget, Chris

 what are you trying to do?
 why do you want to diffirentiate between 99 as a string or 
 as a number?

Because if it is a string, more than likely it means that the key
was user defined and is not PHP defined as an element number.

Again, consider the differences between these two arrays:

array( This, That, Other );

PHP more or less turns this into an associative arrays because,
as far as I know, all arrays are associative.  If you loop through
this array, you get the following key/value pairs

Key  Value
0   This
1That
2Other

The keys are numbers, integers.  They are also the element numbers.

Now, the second type of array:

array( 1 = this, 2 = that, few = other );

Looping through this array, you get the following key/value
pairs:

Key  Value
1   this
2   that
few   other

Funkiness exists that PHP also sees 1 and 2 as element
numbers.  So if you loop (pseudo)for( $i = 1 to 5 ) and print
out those elements, it will print out only this and that,
ignoring other.  But that is neither here nor there for the
most part, just something that I need to take into consideration
and something that prevents me from just checking to see
if the the key is the same as the element number because
in some cases, it legitimately can be.
Alas.

As for what I am trying to do, I am looking for a way to
determine if an array is a user defined associative array or
not.  I.E., I want to come up with an algorithm to differentiate
between the two arrays defined above - the normal array
and the associative array.

Again, any help or insight would be greatly appreciated.
Zeev?  Rasmus?

Chris



[PHP] Re: Is it *really* an associative array?

2001-09-14 Thread _lallous

what are you trying to do?
why do you want to diffirentiate between 99 as a string or as a number?

Chris Boget [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 Sample code:

 script language=php

   $array = array( one, two, three );
   while( list( $key, $val ) = each( $array )) {
 if( is_string( $key )) {
   echo Key is a string\n;

 }
 echo Key: $key = $val :Val\n;

   }
   echo \n\n;

   $array = array( SS = one, 15 = two, 19 = three );
   while( list( $key, $val ) = each( $array )) {
 if( is_string( $key )) {
   echo Key is a string\n;

 }
 echo Key: $key = $val :Val\n;

   }

 /script

 Because of the loose typing in PHP, essentially all arrays
 are associative arrays.  Running the script above produces
 the following results:

  BEGIN RESULTS
 Key: 0 = one :Val
 Key: 1 = two :Val
 Key: 2 = three :Val


 Key is a string
 Key: SS = one :Val
 Key: 15 = two :Val
 Key: 19 = three :Val
  END RESULTS

 Anyways, I want to be able to pass an array to a function.
 This array can be defined by me as associative (as the
 second array in the sample) or regular (as the first).  However,
 I need to be able to tell one from the other in my function.
 As you can see, I can't do it with the is_string() function
 because it doesn't realize that the 15 and the 19 I specify
 as keys in the second declaration are actually strings that I
 added and not actual element numbers.
 Is there some way that I can determine if the keys of an array
 are user defined (ie, a user defined associative array) and not
 the keys PHP defines due to the fact that they are the element
 numbers?

 Another, somewhat related issue, notice the funkiness that
 happens when you run the following, similar, script:

 script language=php

   $array = array( one, two, three );
   while( list( $key, $val ) = each( $array )) {
 if( is_string( $key )) {
   echo Key is a string\n;

 }
 echo Key: $key = $val :Val\n;

   }
   echo \n\n;

   for( $i = 0; $i  count( $array ); $i++ ) {
 echo Printing element: $i --  . $array[$i] . \n;

   }
   echo \n\n;

   $array = array( SS = one, 15 = two, three );
   while( list( $key, $val ) = each( $array )) {
 if( is_string( $key )) {
   echo Key is a string\n;

 }
 echo Key: $key = $val :Val\n;

   }
   echo \n\n;

   for( $i = 0; $i  20; $i++ ) {
 echo Printing element: $i --  . $array[$i] . \n;

   }

 /script

 Any help anyone can provide would be *greatly* appreciated!!

 Chris




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]