Jump to content

I am passing an array and want to return and array. But failed


darkknightgaury

Recommended Posts

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);

 

   

?>

Link to comment
Share on other sites

<?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);

   
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
     }
   
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');
?> 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.