Jump to content

foreach loop with list / array


WesleyA

Recommended Posts

My goal is to loop with foreach through two variables.

 

So I found this script;

<?php
    $abc = array('Mark','Laura');
    $add = array('cocktails','champagne');
    $array = array($abc,$add);

    foreach ($array as list($arr1, $arr2)) {
		// echo '<br>';
	
		echo '<br>';
         echo $arr1;
		 echo '<br>';
         echo $arr2;
    }

But the output is not wat is desired. It says:

 

 

 

Mark
Laura
cocktails
champagne

 

And I would rather have:

 

 

Mark cocktails

Laura Champagne

 

Is there any function / command in the php array reference to do this or should other code be used?

Link to comment
Share on other sites

You would need to associate the two arrays by their index, so the best way to do that would be an ordinary for loop.

$abc = array('Mark','Laura');
$add = array('cocktails','champagne');
$length = count($abc);
for($index = 0; $index < $length; $index++) {
  echo $abc[$index] . ' ' . $add[$index];
  echo '<br>';
}

But the ideal data structure would be this:

$data = array(
  array(
    'name' => 'Mark',
    'drink' => 'cocktails'
  ),
  array(
    'name' => 'Laura',
    'drink' => 'champagne'
  )
);

foreach($data as $row) {
  echo $row['name'] . ' ' . $row['drink'];
  echo '<br>';
}
Link to comment
Share on other sites

OkI think the last option provides me of the output I need but I have a range of names and drinks.

 

What way can it be inserted?

 

Is this correct?

$data = array( array( 'name' => '$names','drink' => '$drinks'))

or maybe $data[]?

 

..................

 

This does not work for instance:

<?php
		$naam = array('Mark', 'Lauren', 'Baasgozer');
		$drinks = array('cocktail', 'champaign', 'Wodka');
	$data = array( array( 'name2' => $naam ,'drink2' => $drinks));
			       


foreach($data as $row) {
	
	
  echo $row['name2'] . ' ' . $row['drink2'];
  echo '<br>';
}
	?>
Edited by WesleyA
Link to comment
Share on other sites

If you have equal number of array names to array drinks in correct order, you can use first array index for the second in foreach

        $abc = array('Mark', 'Laura');
        $add = array('cocktails', 'champagne');
        
        foreach ($abc as $key => $arrayOneValue) {
            echo $arrayOneValue . ' ' . $add[$key] . '<br>';
        }
Link to comment
Share on other sites

Like Ingolme said the 2nd array option is an more ideal option as its easier to see what name belongs to what drink making more easy to manage, but! if its to difficult for you!, you can create an loop that save two arrays combined to produce an array of same structure.

 

Either by index ref of items

        $abc = array('Mark', 'Laura');
        $add = array('cocktails', 'champagne');
        $array = [];

        foreach ($abc as $key => $arrayOneValue) {
            $array[] = [$arrayOneValue, $add[$key]];
        }

        foreach ($array as $row) {

            echo $row[0] . ' ' . $row[1] . '<br>';
        }

or by adding actual name ref of each item

        $array = [];

        foreach ($abc as $key => $arrayOneValue) {
            $array[] = ['name' => $arrayOneValue, 'drink' => $add[$key]];
        }

        foreach ($array as $row) {

            echo $row['name'] . ' ' . $row['drink'] . '<br>';
        }
        
Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...