Jump to content
WesleyA

foreach loop with list / array

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?

Share this post


Link to post
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>';
}

Share this post


Link to post
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

Share this post


Link to post
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>';
        }

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...