Re: Does array_splice() fit for case ?

From: Date: Fri, 27 Aug 2010 10:26:47 +0000
Subject: Re: Does array_splice() fit for case ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-307716@lists.php.net to get a copy of this message
On 26 August 2010 15:15, Alfredo Palhares <masterkorp@gmail.com> 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.

« previous php.general (#307716) next »