Foreach with three add variables

I want to use the foreach function for 3 variables

I use this code on my page:

foreach (array_combine($online_order_name, $online_order_q) as $online_order_name1 => $online_order_q1) { mysql_query("insert into ....... } 

How can i do this:

 <? foreach (array_combine($online_order_name, $online_order_q,$third_variable) as $online_order_name1 => $online_order_q1 => $third_variable2) { ?> 

Thank you

+1
source share
4 answers

I think this will solve your need:

 <?php $A=array(1,2,3); $B=array('a','b','c'); $C=array('x','y','z'); foreach (array_map(NULL, $A, $B, $C) as $x) { list($a, $b, $c) = $x; echo "$a $b $c\n"; } 
+4
source

foreach only iterates over one iterator and offers one key and one value. How iterators are defined in PHP.

In your question, you write that you need several values ​​(but not keys, but I think it won’t hurt). This does not work out of the box.

However, you can create an iterator that allows you to iterate over multiple iterators at the same time. The base class comes with PHP called MultipleIterator . Then you can create your own ForEachMultipleArrayIterator , which allows you to easily specify several keys and values ​​for each iteration:

 $a = array(1,2,3); $b = array('b0' => 'a', 'b1' => 'b', 'b2' => 'c'); $c = array('c0' => 'x', 'c1' => 'y', 'c2' => 'z'); $it = new ForEachMultipleArrayIterator( // array keyvar valuevar array($a, 'akey' => 'avalue'), array($b, 'bkey' => 'bvalue'), array($c, 'ckey' => 'cvalue') ); foreach($it as $vars) { extract($vars); echo "$akey=$avalue, $bkey=$bvalue and $ckey=$cvalue \n"; } class ForEachMultipleArrayIterator extends MultipleIterator { private $vars; public function __construct() { parent::__construct(); $arrays = func_get_args(); foreach($arrays as $set) { if (count($set) != 2) throw new invalidArgumentException('Not well defined.'); $array = array_shift($set); if (!is_array($array)) throw new InvalidArgumentException('Not an array.'); $this->vars[key($set)] = current($set); parent::attachIterator(new ArrayIterator($array)); } } public function current() { return array_combine(array_keys($this->vars), parent::key()) + array_combine($this->vars, parent::current()); } } 

Demo - But if you have come this far, I'm sure it’s clear that what you want to do can be solved much easier by actually sorting out something else. The example above is basically the same:

 foreach(array_map(NULL, array_keys($a), $a, array_keys($b), $b, array_keys($c), $c) as $v) { list($akey, $avalue, $bkey, $bvalue, $ckey, $cvalue) = $v; echo "$akey=$avalue, $bkey=$bvalue and $ckey=$cvalue \n"; } 

It’s better to have your arrays in the correct order and then process them :)

See also: Multiple index variables in PHP foreach loop .

+1
source

I don't think this is possible with array_combine and foreach , because they work specifically with the keys / values ​​of a pair in an associative array. PHP does not have tuples as a built-in data structure.

0
source
 for ($i = 0, $length = count($array1); $i < $length; $i++) { list($a, $b, $c) = array($array1[$i], $array2[$i], $array3[$i]); ... } 

You should think about a better array structure, although this does not require you to retrieve information from three different arrays. Sort of:

 $orders = array( array('name' => 'Foo', 'q' => 'bar', 'third' => 'Baz'), ... ); 
0
source

All Articles