Foreach loop problem

Hello,

There is a problem with this cycle, can you help

$array = array();
$array = array(1,2,3,4,5,6);
foreach ($array AS $sira){
    $birinci['birinci'][] = "deneme";
    $ikinci['ikinci'][] = "deneme2";
}
$yeni_dizi = array_merge($birinci, $ikinci);
foreach ($yeni_dizi AS $dizi){
foreach ($dizi AS $key => $value){
    if($key == "birinci"){
        echo $value."<br>";
    }
    if($key == "ikinci"){
        echo $value."<br>";
    }
    }
}

print_r () output
Array
(
[birinci] => Array
(
[0] => deneme
[1] => deneme
[2] => deneme
[3] => deneme
[4] => deneme
[5] => deneme
)

[ikinci] => Array
    (
        [0] => deneme2
        [1] => deneme2
        [2] => deneme2
        [3] => deneme2
        [4] => deneme2
        [5] => deneme2
    )

)

foreach output
deneme
deneme
deneme2
deneme2

I guess you are asking why there are two of each value being echoed?

$key will be the numbers 0-5, 0-5. If you echo $key inside the loop, you will get output like -

[0]
deneme
deneme
[1]
[2]
[3]
[4]
[5]
[0]
deneme2
deneme2
[1]
[2]
[3]
[4]
[5]

When you compare the numbers in $key with the strings “birinci” and “ikinci”, the strings are cast to integers (numbers take precedence in loose comparisons in php.) Since both of those strings do not start with numerical characters, they end up being zeros. The two if() statements are both true when $key is a zero, so, the same $value will echo twice. Repeat for each of the sub-arrays in $yeni_dizi.

So, the question becomes what output do you want?

I will use it to send one or more mail messages

$new_array = array();
$array = array();
$array = array(1,2,3,4,5,6);
foreach ($array AS $key => $value){
    $emails[$key]['email'] = "Emails";
    $messages[$key]['message'] = "Messages";
}
$new_array = array_merge($emails, $messages);


// There phpmailer code out of the loop here
foreach ($new_array AS $newarray){
foreach ($newarray AS $key => $value){
    if($key == "email"){
         $email = $value;
    }
    if($key == "message"){
         $message = $value;
    }
    // Here is the PHPMailer code
echo $email."<br>";
echo $message."<br>"; //line 47

    }
}

The output is as follows
Emails
Notice: Undefined variable: message in test.php on line 47
Emails
Notice: Undefined variable: message in test.php on line 47
Emails
Notice: Undefined variable: message in test.php on line 47
Emails
Notice: Undefined variable: message in test.php on line 47
Emails
Notice: Undefined variable: message in test.php on line 47
Emails
Notice: Undefined variable: message in test.php on line 47

Emails
Messages
Emails
Messages
Emails
Messages
Emails
Messages
Emails
Messages
Emails
Messages

Repeat issue resolved

$array = array();
$array = array(1,2,3,4,5,6);
foreach ($array AS $key => $value){
    $emails[] = "Emails";
    $messages[] = "Messages";
}

for($i = 0; $i < max(count($emails),count($messages)); $i++) {
    if ($i < sizeof($emails))
        echo $emails[$i]."<br />";
    if ($i < sizeof($messages))
        echo $messages[$i]."<br />";
}
Sponsor our Newsletter | Privacy Policy | Terms of Service