Discussion:
replacing a null value using str_replace
Daniel Leighton
2002-05-08 01:15:48 UTC
Permalink
Hi,

I was attempting to replace null values in an array with 0 using str_replace, but was unsuccessful. The values in question were coming from a MySQL query which returned an empty set. Here is what I tried:

$result = str_replace('', 0, $result);

Here is what I ended up doing instead (which did work):
//set all null values to 0
while(list($key, $value) = each($result))
if(!$result[$key]) $result[$key] = 0;

Is there any way to do this str_replace? Is there another way I should be doing this? Any insights are greatly appreciated.
--
Daniel Leighton
Chief Technology Officer
Webolution
http://www.webolution.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Austin Marshall
2002-05-08 02:03:34 UTC
Permalink
Post by Daniel Leighton
Hi,
$result = str_replace('', 0, $result);
//set all null values to 0
while(list($key, $value) = each($result))
if(!$result[$key]) $result[$key] = 0;
Is there any way to do this str_replace? Is there another way I should be doing this? Any insights are greatly appreciated.
Are you dealing with strings or arrays. If arrays, it looks like what
you did will work... but if you are really dealing with strings, then i
think the function you are looking for is str_pad, not str_replace....

Hope that brings some insight.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dan Hardiker
2002-05-08 08:49:49 UTC
Permalink
Post by Daniel Leighton
I was attempting to replace null values in an array with 0 using
str_replace, but was unsuccessful. The values in question were coming
from a MySQL query which returned an empty set.
If your using string replace functions, then use chr(0) - which is the null
byte.
Post by Daniel Leighton
$result = str_replace('', 0, $result);
Try: $result = str_replace(chr(0), 0, $result);
--
Dan Hardiker [***@staff.firstcreative.net]
ADAM Software & Systems Engineer
First Creative Ltd
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Leighton
2002-05-08 17:17:57 UTC
Permalink
Hi Dan - It looked good and I tried it, but it still didn't work.

Adam - to answer your questions it is an array and the while code does work.

Any other ideas? I can stick with the while loop, I'm just curious if I can do it with str_replace and if that would be faster.

Thank you both for your replies.

Daniel
Post by Dan Hardiker
Post by Daniel Leighton
I was attempting to replace null values in an array with 0 using
str_replace, but was unsuccessful. The values in question were coming
from a MySQL query which returned an empty set.
If your using string replace functions, then use chr(0) - which is the null
byte.
Post by Daniel Leighton
$result = str_replace('', 0, $result);
Try: $result = str_replace(chr(0), 0, $result);
--
ADAM Software & Systems Engineer
First Creative Ltd
--
Daniel Leighton
Chief Technology Officer
Webolution
http://www.webolution.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
CC Zona
2002-05-08 06:22:58 UTC
Permalink
Post by Daniel Leighton
I was attempting to replace null values in an array with 0 using str_replace,
but was unsuccessful. The values in question were coming from a MySQL query
which returned an empty set.
FWIW, you can do these replacements in the query itself. The syntax may be
slightly off here because I'm overtired, but it should be something like:

select if(isnull(fieldname),0,fieldname) as fieldname from ....

See the MySQL manual for more info.
--
CC
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...