Hello,

I'm trying to use in_array for the first time but I'm doing something wrong and I can't figure it out. I want to pass the string data in a variable, obtained from a multi level array, in to another multi level array. But if the string already exists in the new array I want to change the string to 0.

$stringData = $old_array['name'];
$array_to_check = $new_array['name'];
if(!in_array($stringData , $array_to check)){
$array_to_check[] = $stringData;
} else {
$array_to_check[]  = 0;
}

This is what I came up with but it fails with error "Wrong datatype for second argument ". I'd really appreciate some help with this!

Thanks! Chris

    The second argument must be an array, but you're passing a scalar (single value). The array is $new_array, the scalar is $new_array['name'].

    $stringData = $old_array['name']; 
    $array_to_check = $new_array; 
    if(!in_array($stringData , $array_to check)){ 

    BTW, neither array appears to be "multi-level"; they are single-dimension arrays.

      Here is an example of a multi-dimensional array so you will know how they look and how to manipulate them or use them. This is not my work but an example that I reproduced for your study and added some comments.

      <?php
      //  Multidimensional Array
      $cds = array(
        'Christina Aguilera'=>array(//comment, an associative array within an array $cds
          'Impossible',
          'Beautiful',
          'Dirrty'
        ),
        'Pink'=>array(//comment, an associative array within an array $cds
          'Just like a pill',
          'Family potrait',
          'Missundaztood'
        ),
        'Kelly Rowland'=>array(//comment, an associative array within an array $cds
          'Stole',
          'Obsession',
          'Past 12'
        )
      );
      //  $cds is multidimensional array which is actually:
      /*
      - 3 numerically indexed VALUES containing
      - an associative array for singer and songs
        in each and
      - the list of song titles for each singer
        (in a numerically indexed array!).
      */
      echo '<p><b>How many CDs in our collection?</b><br />
      There are '.sizeof($cds)." CDs in our collection.</p>\n";
      
      echo "<ol>\n";
      foreach( $cds as $singer=>$songs )
      {
        echo '  <li><b>'.$singer."</b>.<br />\n";
        echo "  <em>Songs</em>: </li>\n";
        echo "  <ul>\n";
        asort( $songs ); // sorts the list of songs
        foreach( $songs as $song )
        {
          echo '    <li>'.$song.".</li>\n";
        }
        echo "  </ul>\n";
      }
      echo "</ol>\n";
      
      ?>

        Thanks for the help and explanation of multi level arrays. I changed my script so that the second value is an array and not a scaler. This eliminated the error but the script no longer filters $name.

        do { 	
        			$name = $row_ws_data['workshop_name'];
        			$workshop['id'][] = $row_ws_data['workshop_id'];
        				if(!in_array($name, $workshop)){
        					$workshop['name'][] = $row_ws_data['workshop_name'];
        				} else {
        					$workshop['name'][] = "foo";
        				}
        			$workshop['date'][]	= $row_ws_data['workshop_date'];
        			$workshop['iPath'][]= $row_ws_data['workshop_imagePath'];
        			$workshop['price'][]= $row_ws_data['workshop_price'];
        			$workshop['description'][] = $row_ws_data['workshop_description'];
        			$workshop['pdf'][]	= $row_ws_data['workshop_pdfPath'];
        			$workshop['dateINT'][]	= $row_ws_data['workshop_dateINT'];
        
        		array_multisort($workshop['id'],$workshop['name'],$workshop['date'],$workshop['iPath'],
        		$workshop['price'],$workshop['description'],$workshop['pdf'],$workshop['dateINT']);
        		}
        	while($row_ws_data = mysql_fetch_assoc($ws_data));

        Returns:
        [name] => Array
        (
        [0] => test1
        [1] => test1
        [2] => test1
        [3] => test2
        [4] => test2
        [5] => test2
        [6] => test3
        [7] => test3
        )
        The return above is not what I want. But If I change to

        if(!in_array($name, $workshop['name'])){

        The script returns the following which is what I want but I also get the "Wrong datatype for second argument"
        [name] => Array
        (
        [0] => test1
        [1] => foo
        [2] => foo
        [3] => foo
        [4] => test2
        [5] => foo
        [6] => test3
        [7] => foo
        )

        Thanks again! Chris

          Write a Reply...