简体   繁体   中英

in_array does not seem to work while inside a function

i have this code that will check the array contains the specific string and outputs the data needed, but it is inside a function thus the output was done using a concatenate ".=", i did try the in_array function in its basic form ie

$show = array();
$show[] = "dog";
$show[] = "doga";
$show[] = "dogasd";
$show[] = "cat asd";
print_r($show);
if(in_array("cat asd", $show))
{
echo "found";
}
else
{
echo "not found";
}

and it is working, but in here:

public function personal(){

            $sql_field = "SELECT * FROM somewhere WHERE show_field = 1";
    $result = mysql_query($sql_field);
    $num = mysql_num_rows($result);

    $show = array();
    for($i=0;$i<$num;$i++)
    {
        $row = mysql_fetch_assoc($result);
        $show[] = $row['field_name']."<br>";
    }

            $block .= "<tr>";
    $block .= $show[0];

        if(in_array("firstName", $show))
        {
            $block .= "<td style=\"width: 250px;\"><div class=\"fields\"><input type=\"text\" title=\"First Name*\" value=\"{$this->detail("firstName")}\" name=\"first_name\" class=\"placeholder textbox-long\" /></div></td>";
        }
        if(in_array("lastName", $show))
        {
            $block .= "<td style=\"width: 250px;\"><div class=\"fields\"><input type=\"text\" title=\"Last Name*\" value=\"{$this->detail("lastName")}\" name=\"last_name\" class=\"placeholder textbox-long\" /></div></td>";
        }
    $block .= "</tr>";

    }

it always outputs false thus not displaying anything, i tried this with a non array variable and its working fine, so my guess is that array does not work great inside a function? correct me if i'm wrong here. And i need the array for this functionality to work any ideas guys?

Thank You :)

in_array() works just fine inside of functions, however you're expecting it to search for a substring, which it does not do. If you were to search for firstName<br> it would find what you're looking for since you added firstName<br> to the array, but firstName is not in the array.

At first glance all I can see is that you're .<br> on the end could be stopping it from finding the exact phrase.

Change:

for($i=0;$i<$num;$i++)
{
    $row = mysql_fetch_assoc($result);
    $show[] = $row['field_name']."<br>";
}

to:

for($i=0;$i<$num;$i++)
{
    $row = mysql_fetch_assoc($result);
    $show[] = $row['field_name'];
}

Then try it.

have you tried in_array('firstName<br>') ? While filling your array, your adding a break tag to the value here: $show[] = $row['field_name']."<br>"; ...

BTW: replace foreach with while, it's better... honest!

by request

Here's how I would write it:

$sql_field = "SELECT * FROM somewhere WHERE show_field = 1";//I'd only select the fields I needed
$result = mysql_query($sql_field);
//$result holds the resource, not the actual array
//That's why you can do this:
$show = array();
$firstName = $lastName = '';//in_array is slow, so store the values seperatly
while ($row = mysql_fetch_assoc($result))
{
     $show[] = $row['field_name'].'<br/>';
     //I'm a bit of a nitpicker, but should be <br/> :-)
     if ($row['field_name'] === 'firstName')
     {
         $firstName = '<td style="width: 250px;"><div class="fields"><input type="text" title="First Name*" value="'.$this->detail("firstName").'" name="first_name" class="placeholder textbox-long" /></div></td>';
         //use single quotes, a little faster, and less backslashes
     }
     if ($row['fieldName'] ==='lastName')
     {
         $lastName = '<td style="width: 250px;"><div class="fields"><input type="text" title="Last Name*" value="'.$this->detail("lastName").'" name="last_name" class="placeholder textbox-long" /></div></td>';
     }
}//while ends when all results have been fetched
$block .='<tr>'.$show[0].$firstName.$lastName.'</tr>';

This way, if first and last name wasn't returned, the strings are empty. The result is the same. An even better sollution would be making an array $temp=array('firstName'=>'','lastName'=>''); and just doing something like so

if($temp[$row['fieldName']])
{
    $temp[$row['fieldName']] = '<td style="width: 250px;"><div class="fields"><input type="text" title="'.ucfirst(substr($row['fieldName'],0,-4)).' Name*" value="'.$this->detail($row['fieldName']).'" name="'.substr($row['fieldName'],0,-4).'_name" class="placeholder textbox-long" /></div></td>';
}

Or you could use (v)sprintf... your options are quite limitless.

More info:

http://www.php.net/while

http://www.php.net/mysql_fetch_assoc (examples!)

On one of these pages you find the explanation why while is better in this context.

If you feel like C-style string formatting:

http://www.php.net/manual/en/function.vsprintf.php

http://www.functions-online.com/

This code has in now way been tested, nor do I know where you want to use a solution like the one I gave, so please, don't assume this is a full-proof, ready to use bit of code.

Remember:

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian Kernighan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM