Prev: Variable in variable.
Next: Upload using FTP commands
From: Alfredo Palhares on 26 Aug 2010 10:15 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
From: viraj on 26 Aug 2010 23:44 hi alfredo.. i really love to help.. but could you please explain a bit clear. may be with sample input arrays and the expected out put array. ~viraj On Thu, Aug 26, 2010 at 7:45 PM, Alfredo Palhares <masterkorp(a)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 >
From: Richard Quadling on 27 Aug 2010 06:26 On 26 August 2010 15:15, Alfredo Palhares <masterkorp(a)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.
|
Pages: 1 Prev: Variable in variable. Next: Upload using FTP commands |