I have the function below that adds me an index on the second level of the array. It is working correctly with the 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 tried to do the same thing with the foreach loop and it did not 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;
}
?>
Any feature?
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
)
)