Help with function return!

Asked

Viewed 66 times

0

I have the function below that adds me an index on the second level of the array. Is working properly with for loop.

<?php
function comparaArray($array1, $array2) { 

   for ($i = 0; $i < count($array2); $i++) {
     for ($j = 0; $j < count($array1); $j++) {


        if( $array2[$i][0] == $array1[$j][0] ) {
           $array2[$i][2] = "s";
           break;
        }   

    }   

    if(!isset($array2[$i][2])) {
        $array2[$i][2] = "n";
    }

 }

 return $array2;

}
?>

I went to try to do the same thing with the foreach loop and it didn’t work!

<?php
function comparaArray1($array1, $array2) { 

   foreach ($array2 as $indice2) {

    foreach ($array1 as $indice1) {

       print $indice2[0]." == ".$indice1[0]."<br>";

       if($indice2[0] == $indice1[0]) {
            $indice2[2] = "s";
         break;
       }

    }

    if(!isset($indice2[2])) {
        $indice2[2] = "n";
    }

    echo "<br>";

   }

   print "<pre>";
   print_r($array2);
   print "</pre>";

   return $array2;


 }

?>

Some recourse?

Array1

Array
(
    [0] => Array
        (
            [0] => '2015-02'
            [1] => 8
        )

    [1] => Array
        (
            [0] => '2015-04'
            [1] => 8
        )

    [2] => Array
        (
            [0] => '2015-09'
            [1] => 8
        )

)

Array2

Array
(
    [0] => Array
        (
            [0] => '2015-02'
            [1] => 8
        )

    [1] => Array
        (
            [0] => '2015-03'
            [1] => 8
        )

    [2] => Array
        (
            [0] => '2015-04'
            [1] => 8
        )

    [3] => Array
        (
            [0] => '2015-05'
            [1] => 8
        )

    [4] => Array
        (
            [0] => '2015-06'
            [1] => 8
        )

    [5] => Array
        (
            [0] => '2015-07'
            [1] => 8
        )

    [6] => Array
        (
            [0] => '2015-08'
            [1] => 8
        )

    [7] => Array
        (
            [0] => '2015-09'
            [1] => 8
        )

)
  • It seems that nowhere in Voce code does it perform functions to insert values in the array.

  • I’ll change the question!

  • In your array it is two-dimensional? Pq if not $i and $j would be in the second house $array[0][$i]. Try a var dump in your array and see how ta is the indices if ta 0.0 0.1 0.2 or 0.0 0.1 0.2 after 1.0

  • yes, they are the same! I deleted right? See question again. I will add them again!

  • If he’s two-dimensional, the two houses walk ex 0.0 0.1 after 1.0 1 after 2.0 2.1 so I saw in his code the if only goes through the columns type 0.0 after 1.0 after 2,0 when I get confused qnd is two-dimensional array I draw it on paper only the indexes to help me make tbm may be that help you on the first line 0,0 0,1 0,2 on the second line 1,0 1,1,2 and so on

  • In the first function this inserting the indexes correctly. In the second, in the foreach loop, it is not this. I had printed the outputs to see.

  • vc needs which array data to date the number or only two.

  • in your array1 is 0,0 and 0,1 / 1,0 and 1,1 / 2,0 and 2,1 I think your code needs more for, because a two-dimensional array needs only two to go through it

  • Arrays has the date and number. I need to compare the dates. If you find some equivalence of array1 in array2, then create the Indice array1[][2] that does not exist and gives us’s' of found value. Otherwise, create the index and give value 'n' that you did not find. But you are not creating the index!

Show 4 more comments

1 answer

1

I got!

<?php
function comparaArray($array1, $array2) { 

   foreach ($array2 as $indice2=>$valor2) {

    foreach ($array1 as $indice1=>$valor1) {

       if($valor2[0] == $valor1[0]) {
            $array2[$indice2][2] = "s";
         break;
       }

    }

    if(!isset($array2[$indice2][2])) {
            $array2[$indice2][2] = "n";
    }

   }

   return $array2;   

 }

?>

Thank you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.