Page 1 of 1

Super simple

Posted: Mon Feb 22, 2010 11:33 am
by z3r0
Or so I thought!

I'm trying to iterate through an array and test if any of the values are NULL or ""

and if they are I just want to insert XXX

I've tried many things and I'm so confused I'd appreciate any help at all.

Re: Super simple

Posted: Mon Feb 22, 2010 11:57 am
by pickle

Code: Select all

foreach($yourArray as $key=>$value)
{
  if(is_null($value) or $value === '')
    $yourArray[$key] = 'XXX';
}
foreach() makes a new copy of the array before it iterates, so you can't just overwrite $value, you have to explicitly state what you want to change in the original array.

Re: Super simple

Posted: Mon Feb 22, 2010 1:27 pm
by John Cartwright
If you also want to consider 0, false and empty arrays as "empty", then you can replace

Code: Select all

if(is_null($value) or $value === '')
with

Code: Select all

if(empty($value))

Re: Super simple

Posted: Tue Feb 23, 2010 10:02 am
by z3r0
I really appreciate the help thanks when I try and run this it gives me

"Fatal error: Can't use function return value in write context in C:\xampp\htdocs\madlib.php on line 36"

For you though that would be line 4 I think it's the line with "$ yourArray[$key] = 'XXX';"

Again it looks like it should work to me and I guess that's why I'm so frustrated.
I TAKE IT BACK! I should have copied and pasted I was doing "$ yourArray($key) = 'XXX';"

instead of "$ yourArray[$key] = 'XXX';" which does work so thank you.