Re: [PHP] test for associative or numerically indexed array

2003-03-20 Thread cpaul

> So much for the "theory" - what are you really trying to achieve? Maybe
> there's something you can redesign so you're not relying on the fact if an
> array is "enumerated" or not.

thanks very much for your help - i understand now that no matter what
kind of array i think i'm making, it is being morphed into an associative
array.  i half-understood this from the docs, but was puzzled.. i expected
the behaviour of an array in other languages, which even if i were to
define an array in this sequence:

$arr[2] = "z";
$arr[0] = "x";
$arr[1] = "y";

would shuffle itself internally into a sequence like: x, y, z.

but in php this obviously isn't the case, and it remains: z, x, y.

thanks again for your time.




-- 
chris paul


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] test for associative or numerically indexed array

2003-03-20 Thread Ernest E Vogelsinger
At 09:57 20.03.2003, cpaul said:
[snip]
>> as associative array. Take this example:
>> $a = array('one','two','three');
>> $b = array(); $b[0] = 'one'; $b[1] = 'two'; $b[2] = 'three';
>> $c = array(0 => 'one', 1 => 'two', 2 => 'three');
>> 
>> Which one would you believe is enumerated, and which one is associative?
>
>they'd all be enumerated, except perhaps $c -- but i've grown :) and now
>understand that $c winds up being an enumerated array.. or is it?

Well, all these 3 arrays are _exactly_ the same. If you print_r() them
you'd see
Array {
0 => one
1 => two
2 => three
}
The key to this issue seems to be understanding that it makes no difference
how the array keys are constructed... an array is an association of key to
value, being a hash  always if you want to see it that way.

>in my code
>
> $q = mysql_query ( "SELECT production_id, title FROM productions ORDER 
>BY title;" );
> $this->all_productions[""] = "";
> while ( $row = mysql_fetch_array( $q ) ) {  
>   $this->all_productions[$row["production_id"]] = $row["title"];
> }
>
>the array is kept in order when i foreach the array - wouldn't they sort
>themselves into 0,1,2,3,4,5,etc if my while loop was populating an 
>enumerated array?   or are all arrays in php actually a keyed hash?

You're chosing your own keys here - so this makes a perfect associative
array, even if the keys ($row['production_id']) would be a sequenced
number. If you would just add the titles up ($this->all_productions[] =
$row['title']), PHP would choose an index key for you - the most general
thing it can do is to enumerate it. Basically what it does is
 array[count(array)] = new_element

So much for the "theory" - what are you really trying to achieve? Maybe
there's something you can redesign so you're not relying on the fact if an
array is "enumerated" or not.


-- 
   >O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] test for associative or numerically indexed array

2003-03-20 Thread cpaul



> At 08:52 20.03.2003, cpaul said:
> [snip]
> >ok thanks - that makes sense.  sort of doesn't solve my problem, because
> >if my function receives an enumerated array, i want it to treat it as an
> >associative array, using the value as the key.
> [snip] 


ernest wrote:

> What would be the value then?
> 
> If I get you correctly, you would treat an array that comes like
> [0] => entry 0
> [1] => entry 1
> [2] => entry 2

like this:

  'entry 0' => 'entry 0'
  'entry 1' => 'entry 1'
  'entry 2' => 'entry 2'


> What happens when there are duplicate values in the source array? You will
> loose entries on duplicate values.

the source array is based on a directory listing, so i don't think there's
a possibility of that happening?



> You can't tell with absolute certainty if an array is enumerated, or built
> as associative array. Take this example:
> $a = array('one','two','three');
> $b = array(); $b[0] = 'one'; $b[1] = 'two'; $b[2] = 'three';
> $c = array(0 => 'one', 1 => 'two', 2 => 'three');
> 
> Which one would you believe is enumerated, and which one is associative?

they'd all be enumerated, except perhaps $c -- but i've grown :) and now
understand that $c winds up being an enumerated array.. or is it?

in my code

 $q = mysql_query ( "SELECT production_id, title FROM productions ORDER BY title;" 
);
 $this->all_productions[""] = "";
 while ( $row = mysql_fetch_array( $q ) ) {  
   $this->all_productions[$row["production_id"]] = $row["title"];
 }

the array is kept in order when i foreach the array - wouldn't they sort
themselves into 0,1,2,3,4,5,etc if my while loop was populating an 
enumerated array?   or are all arrays in php actually a keyed hash?



> What you can do is walk the array keys and check if there is at least a
> single non-numeric key. If you found one the array is associative. If you
> found none it may be likely that the array is enumerated, but you can't be
> sure in a general way, except your application is designed in a way that
> uses always non-numeric keys for associative arrays.

thanks so much for all your input on this.





regards



-- 
chris paul

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] test for associative or numerically indexed array

2003-03-20 Thread Ernest E Vogelsinger
At 08:52 20.03.2003, cpaul said:
[snip]
>ok thanks - that makes sense.  sort of doesn't solve my problem, because
>if my function receives an enumerated array, i want it to treat it as an
>associative array, using the value as the key.
[snip] 

What would be the value then?

If I get you correctly, you would treat an array that comes like
[0] => entry 0
[1] => entry 1
[2] => entry 2
as
[entry 0] => ??
[entry 1] => ??
[entry 2] => ??

What happens when there are duplicate values in the source array? You will
loose entries on duplicate values.

If I got you right here have a look at array_flip()
(http://www.php.net/manual/en/function.array-flip.php) to exchange array sides.

You can't tell with absolute certainty if an array is enumerated, or built
as associative array. Take this example:
$a = array('one','two','three');
$b = array(); $b[0] = 'one'; $b[1] = 'two'; $b[2] = 'three';
$c = array(0 => 'one', 1 => 'two', 2 => 'three');

Which one would you believe is enumerated, and which one is associative?

What you can do is walk the array keys and check if there is at least a
single non-numeric key. If you found one the array is associative. If you
found none it may be likely that the array is enumerated, but you can't be
sure in a general way, except your application is designed in a way that
uses always non-numeric keys for associative arrays.



-- 
   >O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] test for associative or numerically indexed array

2003-03-19 Thread cpaul

> --- cpaul <[EMAIL PROTECTED]> wrote:
> > Is there a method to test whether or not an array is associative?


[EMAIL PROTECTED] wrote:

> It's all the same. An enumerated array is really an associative array where
> every key is an integer. Just treat them all like associative arrays, and
> you'll be fine.
> 

ok thanks - that makes sense.  sort of doesn't solve my problem, because
if my function receives an enumerated array, i want it to treat it as an
associative array, using the value as the key.

the array index is not useful to me in this situation.

is there really no way to tell how an array was defined?


thanks for your time


-- 
chris paul

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] test for associative or numerically indexed array

2003-03-19 Thread Chris Shiflett
--- cpaul <[EMAIL PROTECTED]> wrote:
> Is there a method to test whether or not an array is associative?
> 
> I'm trying to make a function that can deal with whatever type of
> array (associative or numeric) that is thrown at it.

It's all the same. An enumerated array is really an associative array where
every key is an integer. Just treat them all like associative arrays, and
you'll be fine.

Chris

=
Become a better Web developer with the HTTP Developer's Handbook
http://httphandbook.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php