Jump to content

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


Nuv

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 .

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.