darkknightgaury Posted November 23, 2008 Share Posted November 23, 2008 I am getting an error Warning: Invalid argument supplied for foreach() why and how can I fix it? <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { $x_clean=$dirty_form; foreach($x_clean as $val) { $val =strip_tags($val); } return $val; } function display_array($xarray) { $array=$xarray; foreach($array as $val) print "$val"; } /* include/form.php variables sent*/ $dirty_form=array("<p>a</p>","B","c"); $clean_form=clean_html($dirty_form); display_array($clean_form); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 23, 2008 Share Posted November 23, 2008 <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { $x_clean=$dirty_form; foreach($x_clean as $val) { $val =strip_tags($val); } return $val; } function display_array($xarray) { for($i=0; $i<count($xarray); $i++){ print $xarray[$i]; } /* include/form.php variables sent*/ } $dirty_form=array("<p>a</p>","B","c"); $clean_form=clean_html($dirty_form); display_array($clean_form); ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 redarrow, your solution doesn't really fix the problem. <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { foreach($dirty_form as &$val) { $val=strip_tags($val); } return $dirty_form; } ?> if you're going to directly edit the values in an array in a foreach() loop you need to specifically instruct the loop to pass each element as a reference, or else it will just pass it by value. Quote Link to comment Share on other sites More sharing options...
darkknightgaury Posted November 23, 2008 Author Share Posted November 23, 2008 redarrow, your solution doesn't really fix the problem. <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { foreach($dirty_form as &$val) { $val=strip_tags($val); } return $dirty_form; } ?> if you're going to directly edit the values in an array in a foreach() loop you need to specifically instruct the loop to pass each element as a reference, or else it will just pass it by value. Why do you return dirty_form instead of $val;? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 returning $val would only return a reference to the last string element in the $dirty_form array. I suppose the logic may be easier to understand if I write it like this... (though it's a little less efficient) <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { $clean_form = array(); foreach($dirty_form as $val) { $clean_form[]=strip_tags($val); } return $clean_form; } ?> In this I actually build a second array with the content cleaned instead of just changing the original array. Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 23, 2008 Share Posted November 23, 2008 why this not working correctly <?php /*clean_html function checks all the values in the array and removes tags such as html, javascript, ect*/ function clean_html($dirty_form) { $x_clean=$dirty_form; for($a=0; $a<count($x_clean); $a++) { $val =strip_tags($x_clean[$a]); } return $val; } function display_array($xarray) { for($b=0; $b<count($xarray); $b++){ echo $xarray[$b]; } /* include/form.php variables sent*/ } $dirty_form=array("<p>a</p>","B","c"); for($i=0; $i<count($dirty_form); $i++){ $clean_form=clean_html($dirty_form[$i]); display_array($clean_form); } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 23, 2008 Share Posted November 23, 2008 why is this offset on my code please...... function display_array($xarray) { for($b=0; $b<count($xarray); $b++){ echo $xarray[$b]; } Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 it removes the error, redarrow, but that only echoes the last variable because you didn't fix the underlying problem of him only returning the last value in his original loop. The way I'd write the script (without combining the clean/display functions, which is what I'd really do) is this... <?php function clean(&$val) { $val = strip_tags($val); } function display(&$val) { echo $val; } $array = array("<p>a</p>","B","c"); array_walk($array, 'clean'); array_walk($array, 'display'); ?> but he didn't ask us to rewrite the script just fix the error. Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 23, 2008 Share Posted November 23, 2008 i agree with you mate one thing and thank you for your time what the &$val <<for is that to add the two functions together.. and array_walk that new to me whale it like a loop or what? sorry <?php function clean(&$val) { $val = strip_tags($val); } function display(&$val) { echo $val; } $array = array("<p>a</p>","B","c"); array_walk($array, 'clean'); array_walk($array, 'display'); ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 the & means to pass it as reference, so when you change its value it changes the original value. <?php $aaa = 'original value'; $bbb = &$aaa; $bbb = 'the value is changed'; echo $aaa; // outputs "the value is changed" ?> edit: as for array_walk, read about it here: http://www.php.net/array_walk Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 23, 2008 Share Posted November 23, 2008 Thank you for blowing my brain up, thort i new php but i dont now lol. cheers m8...... Quote Link to comment Share on other sites More sharing options...
darkknightgaury Posted November 23, 2008 Author Share Posted November 23, 2008 the & means to pass it as reference, so when you change its value it changes the original value. <?php $aaa = 'original value'; $bbb = &$aaa; $bbb = 'the value is changed'; echo $aaa; // outputs "the value is changed" ?> edit: as for array_walk, read about it here: http://www.php.net/array_walk ohhhhh! is a POINTER! Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 Well, a reference. Pointers are something different and don't exist in php (thankfully some might say). But yes, a reference variable in effect "points" to a different variable. EDIT: compare http://en.wikipedia.org/wiki/Reference_(computer_science) to http://en.wikipedia.org/wiki/Pointer_(computing) if you truly care about the difference, but it may just cause confusion Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.