| | |
Question about array
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2005
Posts: 66
Reputation:
Solved Threads: 0
•
•
•
•
$abc_ = array(0, 0, 0, 1, 0, 2);
while( list($key, $value) = each($abc_) )
if( !($value=='0') )
$abc[] = $value;
•
•
Join Date: Nov 2005
Posts: 66
Reputation:
Solved Threads: 0
However, code I've said is better (efficiency!) of two
•
•
•
•
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
•
•
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Array Question (C++)
- C++ Array Question (C++)
- pointer and array arithmetic (C)
- Class with dynamic array how? (C++)
- array problems (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- Array Values not found in Methods (Java)
- Function[Array] in combination with cin>> (C++)
- sorting an array of string (C)
Other Threads in the PHP Forum
- Previous Thread: Need A Php Master
- Next Thread: help about IP address
| Thread Tools | Search this Thread |
ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl database date delete display dynamic echo email error external file files filter folder form forms function functions gc_maxlifetime google host href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm multiple mysql navigation oop parsing paypal pdf php problem query question radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube






