How to fetch data of two array using array_walk in PHP

If you want to fetch value from more than two array in one loop using PHP

                    

<?php
$array1 = array(‘laptop’,’deskop’,’mobile’);
$array2 = array(‘Mouse’,’Keyboard’,’Printer’);
$array3 = array(‘Grapes’,’Banana’,’Papaya’);

array_walk( $array1, function ( $data, $val ) use ( $array2,$array3 ){
echo $data.”<br>”;         /* Fetch value from first array */
echo $array2[$val].”<br>”; /* Fetch value from second array */
echo $array3[$val].”<br>”; /* Fetch value from third array */
});

?>

 

If you want to fetch value from two array in one loop using PHP

                    

<?php

$array1 = array(‘Motorola’,’Redmi’,’Lenovo’);
$array2 = array(‘Leaf’,’Tree’,’Plant’);

foreach( array_combine( $array1, $array2 ) as $val1 => $val2 ){
echo $val1.”<br>”;  /* Fetch value from first array */
echo $val2.”<br>”;  /* Fetch value from second array */
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *