Question about array

Reply

Join Date: Jun 2005
Posts: 92
Reputation: michael123 is an unknown quantity at this point 
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

Question about array

 
0
  #1
Dec 13th, 2005
If I have an array $abc=[0,0,0,1,0,2];
how to change it to $abc=[1,2]; (remove all 0), is there php function can handle that? thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 66
Reputation: guideseeq is an unknown quantity at this point 
Solved Threads: 0
guideseeq guideseeq is offline Offline
Junior Poster in Training

Re: Question about array

 
0
  #2
Dec 14th, 2005
$abc_ = array(0, 0, 0, 1, 0, 2);
while( list($key, $value) = each($abc_) )
if( !($value=='0') )
$abc[] = $value;
There may be more efficient (execution time?) code than above.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 11
Reputation: pc2forum is an unknown quantity at this point 
Solved Threads: 0
pc2forum pc2forum is offline Offline
Newbie Poster

Re: Question about array

 
0
  #3
Dec 22nd, 2005
here is the one of the simplest example...

$abc=[0,0,0,1,0,2];
$bcd=array();
for($i=0;$i<count($abc);$i++)
{
if($abc[$i]!="0")
{
array_push($bcd,$abc[$i])
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 66
Reputation: guideseeq is an unknown quantity at this point 
Solved Threads: 0
guideseeq guideseeq is offline Offline
Junior Poster in Training

Re: Question about array

 
0
  #4
Dec 22nd, 2005
However, code I've said is better (efficiency!) of two
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 11
Reputation: pc2forum is an unknown quantity at this point 
Solved Threads: 0
pc2forum pc2forum is offline Offline
Newbie Poster

Re: Question about array

 
0
  #5
Dec 22nd, 2005
i am not saying the urs is wrong but mine is more simpler and easier to understand in one look..
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Question about array

 
0
  #6
Dec 29th, 2005
Originally Posted by michael123
If I have an array $abc=[0,0,0,1,0,2];
how to change it to $abc=[1,2]; (remove all 0), is there php function can handle that? thanks.
Im wondering...

Is this any faster?

[PHP]<?php

function removeVal($array, $val) {

$string = implode(',', $array);
$new_string = str_replace($val.',', ',', $string);
$new_array = explode(',', $new_string);

return $new_array;

}

$abc=[0,0,0,1,0,2];
$new_abc = removeVal($abc, 0);

var_dump($new_abc); // should show new array withought 0s

?>[/PHP]

what do you guys think?...

btw: I think the for loop would be faster...
If you just took the count out of the for loop.

[PHP]$abc=[0,0,0,1,0,2];
$bcd=array();
$count = count($abc);
for($i=0;$i<$count;$i++)
{
if($abc[$i]!="0")
{
array_push($bcd,$abc[$i])
}
} [/PHP]

This way the count is only evaluated once during the loop.
Im not sure if its faster than the while loop though.

I've looked at some benchmark tests. And it seems foreach is fastest. If you just make sure you do not copy the array, but supply the original array ie: &$array.
Last edited by digital-ether; Dec 29th, 2005 at 7:23 am. Reason: forgot to highlight php code
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Question about array

 
0
  #7
Dec 29th, 2005
Originally Posted by michael123
If I have an array $abc=[0,0,0,1,0,2];
how to change it to $abc=[1,2]; (remove all 0), is there php function can handle that? thanks.
Heres a way of doing this that seems to be the most efficient.
The $new_array also retains its key values.

[PHP]function removeVals($var) {
return ($var != 0);
}

$new_array = array();
$array = array(0,0,0,1,0,2);
$new_array = array_filter($array, "removeVals");[/PHP]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC