Re: Does array_splice() fit for case ?

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

 



On 26 August 2010 15:15, Alfredo Palhares <masterkorp@xxxxxxxxx> wrote:
> Hello,
>
> This is kinda a noob question, but that's what i am :)
>
> I have a 2 dimensions array (the first dimension are "normal" keys ) and in
> the second dimension haves custom arrays but they all have a comon key that
> is *id.
> *The array comes ascending ordered by the numbers of *id* .
>
> When i receive this array i need to look for and array that haves the
> *id*== 0, so i do a for  loop looking for it  and i unset this values
> after
> that.
> But need i need that the unset array looks like this sub-array never existed
> ( eg. if the key that with id = 0  was "5", the "5" will the number "6" and
> "6" the "7" and so on).
> Here is the code:
>
> <?php
>
> // $data the name of the array
>
>        //Look for the main email
>        for ($i = 0; $i < count($data); $i++) {
>             if ($data[$i]['id'] === 0) {
>                 // It can't be empty
>                 if (!empty($data[$i])) {
>                     $this->email = $data[$i]['contact'];
>                     $main_email_found = true;
>                 }
>                 unset($data[$i]);
>             }
>        }
> ?>
>
> I have tree ways of doing this:
>
> - Use a method that i that looks from duplicate entru based on the *id *key
> as index and after and return the array ordered by this index.
> - Adding a new entry to another array in this loop that not haves the
> *id*== 0. and after that reverse the order
> - Use the array_splice native function.
>
> What do you recommend me ?
> Sorry by the bad English.
>
>
> --
> Regards,
> Alfredo Palhares
>

Once you've cleared out the array of the data you don't want, would
the following output the right result?


sort($data);
foreach($data as $id => $node) {
 $node['id'] = $id;
}
print_r($data);



-- 
Richard Quadling.

-- 
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