Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP Foreach Loop กับสองอาร์เรย์และ If-condition หาค่าที่ตรงกัน

สมมติเรามีอาร์เรย์สองตัวดังนี้

$firstArray=array(10,20,30,40,50); $secondArray=array(100,80,30,40,90);

เราต้องการหาค่าที่ตรงกัน คือเอาต์พุทควรเป็น

30 40

ตัวอย่าง

โค้ด PHP ดังนี้

<!DOCTYPE html> <html> <body> <?php 
$firstArray=array(10,20,30,40,50); 
$secondArray=array(100,80,30,40,90); 
foreach($firstArray as $f){ 
    foreach($secondArray as $s){ 
        if($f==$s){ 
            echo "The matching result is=",$f,"
"; } } } ?> </body> </html>

ผลลัพธ์

นี่จะผลิตเอาต์พุตดังนี้

The matching result is=30 The matching result is=40