Re: Re: Declaring arrays? Good practice?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In the examples I saw, when you used $new_array = $value (without the []), you weren't going to get a proper copy of the array, as multiple people stated.

I'm curious why I didn't see (or maybe I just missed it) something like this:

$old_array = array('one', 'two', 'three', 'four');

$new_array = $old_array;

?

If you use a foreach() loop, you definitely need to use []:

foreach ($old_array as $value) {
  $new_array[] = $value;
}


You'll loose all your key associations unless you do:

foreach ($old_array as $key => $value) {
  $new_array[$key] = $value;
}


If you want to destroy your keys, you can use the first example of a foreach() or you can also use:

$new_array = array_values($old_array);


I'm lazy so I don't tend to pre-define variable types although I should probably get into the practice when there's a security need to do so.

So what was the original question again?  hah

-TG



= = = Original message = = =

What I said was that arrays can be handled two different ways for loops.

1] By assigning the variable prior to the loop [i.e., $new_array= array();]

2] Or, by using inside the loop: $new_array[]= $value

Either one works.  I've found, when the loop has lots of fancy stuff involving the $new_array, it is useful to go ahead 
a assign it before starting the loop.




Kilbride, James wrote:
> <?php
>  
>  $old_array= array('one','two','three','four');
>  $new_array= array();
> 
>  foreach($old_array as $value)
> 
>     $new_array= $value;
>  
>  echo "$new_array";
> ?>
> 
> The result of the echo is "four". Doing the same above with the [] after
> new_array returns the right result, a new array.(outputs Array). So what
> exactly where you saying 'Taint so' Al? I'm confused since Jochem's
> email about how to properly copy an array seems correct.
> 
> James Kilbride
> 
> 
>>-----Original Message-----
>>From: Al [mailto:news@xxxxxxxxxxxxx] 
>>Sent: Wednesday, December 14, 2005 10:47 AM
>>To: php-general@xxxxxxxxxxxxx
>>Subject: Re:  Re: Declaring arrays? Good practice?
>>
>>Jochem Maas wrote:
>>
>>>Al wrote:
>>>
>>>
>>>>However....
>>>>
>>>>For loops:
>>>>
>>>>$new_array= array();
>>>>
>>>>foreach($old array as $value)
>>>>
>>>>   $new_array= $value:
>>>>
>>>>
>>>>Otherwise, all you'll get it the last assignment of 
>>
>>$new_array as a 
>>
>>>>variable, not an array.
>>>
>>>
>>>which is exactly what you get if yuou run the code above - 
>>
>>after the 
>>
>>>foreach loop $new_array will be set to the value of the 
>>
>>last item of 
>>
>>>$old_array.
>>>
>>>regardless of whether you init the var you still have to use the 
>>>square-bracket notation to add items to the array.
>>>
>>>
>>>>You can also use;
>>>>
>>>>foreach($old array as $value)
>>>>
>>>>  $new_array[]= $value:
>>>>
>>>>
>>>>But sometimes, if the expression is a bit fancy in the 
>>
>>loop and you 
>>
>>>>are using $keys, it is easy to make a mistake.
>>>>
>>>>Al..........
>>>>
>>>>Michael Hulse wrote:
>>>>
>>>>
>>>>>Sorry if this question sounds noobish:
>>>>>
>>>>>$foo = array(); // Declare first, good practice.
>>>>>$foo = array('one', 'two', 'three');
>>>>>
>>>>>Using above as example, is it good to always declare the 
>>
>>array first?
>>
>>>>>I usually/always declare my arrays first, but sometimes I 
>>
>>get a bit 
>>
>>>>>confused... for example:
>>>>>
>>>>>function give_foo() 
>>>>>    $the_foo = array(); // Declare first, good practice.
>>>>>    $the_foo = array('one', 'two', 'three');
>>>>>    // Do some stuff here...
>>>>>    return $the_foo;
>>>>>
>>>>>$foo = give_foo(); // Declare $foo also?
>>>>>
>>>>>So, should I declare $foo as array and then give it the 
>>
>>value of the 
>>
>>>>>function? Example:
>>>>>
>>>>>$foo = array(); // Declare first, good practice.
>>>>>$foo = give_foo();
>>>>>
>>>>>That seems kinda redundant. Thoughts?
>>>>>
>>>>>Sorry if this is a stupid question... I am self-taught, and I am 
>>>>>trying to shake all my bad coding habits.
>>>>
>>>>
>>>>
>>Taint so Jochem.  Try it.
>>
>>--
>>PHP General Mailing List (http://www.php.net/) To 
>>unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

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


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux