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.
– RFL
I’ll change the question!
– Carlos Rocha
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
– Thalles Daniel
yes, they are the same! I deleted right? See question again. I will add them again!
– Carlos Rocha
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
– Thalles Daniel
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.
– Carlos Rocha
vc needs which array data to date the number or only two.
– Thalles Daniel
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
– Thalles Daniel
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!
– Carlos Rocha