Jump to content

in_array() [function.in-array]: Wrong datatype for second argument


Recommended Posts

Im getting a warning with in_array.I have used if() inside for loop to check if value has already been printed or not. If not it will print and save the value in $done[].Can someone please point me out my mistake that i am getting a warning.

 

<?php
             for($i=0;$i<=$count;$i++)
                {   
                    $count_final = explode("_", $result[$i]);
                   if (in_array($count_final[0],$done)) 
                    {  //Line 480
                    echo "<tr>";
                    echo"<td>$count_final[0]</td>";
                    $templ = $count_final[0]."_left";
                    $tempr = $count_final[0]."_right";
                    $cleft = $count_middle[$templ];
                    $cright = $count_middle[$tempr];
                    echo"<td>$cright</td>";  
                    echo"<td>$cleft</td>";
                    echo "</tr>"; 
                    $done[] = $count_final[0];
                   }
                }    
?>

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Users\\Desktop\yyy.php on line 480

 

Yeah i sorted that out. However i got another problem.In the above code explode won't work. I wonder why.

 

Part code from above -

$count_final = explode("_", $result[$i]);

 

$result[$i] prints

Array
(
    [0] => 2011-05-01_right
    [1] => 2011-05-02_left
    [3] => 2011-05-02_right
)

 

$count_final prints

Array
(
    [0] => Array
)

Array
(
    [0] => 
)

 

I just want the date without _right or _left thus i used explode however it wont work.Why ?

 

It is is because $result[$i] returns an array, Explode requires a string as the second parameter not an array.

 

if $result echo's

Array
(
    [0] => 2011-05-01_right
    [1] => 2011-05-02_left
    [3] => 2011-05-02_right
)

 

shouldnt $result[0] echo 2011-05-01_right ?

I assume you're outputting the array with print_r().

 

$result[$i] prints

Array
(
    [0] => 2011-05-01_right
    [1] => 2011-05-02_left
    [3] => 2011-05-02_right
)

To get that output are you passing $result[$i] to print_r() or just $result?

I am using  print_r($result); outside the for loop.

 

$result[] = array_unique($count_begin);  
                $count = count($result);
                $done[] = '';
                for($i=0;$i<=$count;$i++)
                {   
                    $string = $result[$i];
                    $count_final = explode("_", $result[$i]);
                   if (!(in_array("$count_final[0]",$done))) 
                    {
                    echo "<tr>";
                    echo"<td>$count_final[0]</td>";
                    $templ = $count_final[0]."_left";
                    $tempr = $count_final[0]."_right";
                    $cleft = $count_middle[$templ];
                    $cright = $count_middle[$tempr];
                    echo"<td>$cright</td>";  
                    echo"<td>$cleft</td>";
                    echo "</tr>"; 
                    $done[] = $count_final[0];
                   }
                 
                
                } 
                 echo "<pre>";
                 print_r($result);
                 echo "</pre>";

This line

$result[] = array_unique($count_begin);  

Will make $result a multidimensional array, as array_unqiue returns an array of unique values from the $count_begin array . Remove the [] after $result.

This line

$result[] = array_unique($count_begin);  

Will make $result a multidimensional array, as array_unqiue returns an array of unique values from the $count_begin array . Remove the [] after $result.

 

Ah. Thank you for the help. Works perfectly now .

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.