foreach with three variables add - php

i want to use foreach function for 3 variables
i use this code in 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 that like :
<?
foreach (array_combine($online_order_name, $online_order_q,$third_variable) as
$online_order_name1 => $online_order_q1 => $third_variable2) {
?>
thank you

I guess 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";
}

foreach does only the iteration over one iterator and offering one key and one value. That's how iterators are defined in PHP.
In your question you write that you need multiple values (but not keys, but I think this 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 once. A base class ships with PHP it's called MultipleIterator. You can then create your own ForEachMultipleArrayIterator that allows you to easily specify multiple keys and values per 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've come that far, I'm pretty sure it's clear that what you want to do can be solved much easier by actually iterating over something else. The example above is basically the same as:
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";
}
So better get your arrays into the right order and then process them :)
See also: Multiple index variables in PHP foreach loop.

I don't believe this is possible with array_combine and foreach, because they operate specifically on key/value pairs in an associative array. PHP doesn't have tuples as a builtin data structure.

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 though that doesn't require you to pull information from three different arrays. Something like:
$orders = array(
array('name' => 'Foo', 'q' => 'bar', 'third' => 'Baz'),
...
);

Related

list of values to array chain

$a = Array("one", "two", "three");
$b = "text"
I have been trying to transform the above array into something like this:
$a = Array("one" => Array("two" => Array("three" => "text")));
I am looking for a way to do it without improvising but so far no luck and googleing seems to turn up with everything but what I am looking for.
Use recursion
function make(array $array, $value) {
$first = array_shift($array);
if (count($array) === 0) {
return array($first => $value);
} else {
return array($first => make($array, $value);
}
}
It takes the first item of the array and places it in $first. When placed in $first it is removed from $array. Then it checks if the array has some items left. If so it coninues the loop otherwise it end the loop.
Hope it works for you
So you can call it like:
$a = Array("one", "two", "three");
$b = "text";
$array = make($a, $b);
$i=count($array)-1;
$b=array();
$a=$array[$i];
while($i>0) {
$b=array($array[$i-1]=>$a);
$a=$b;
$i--;
}
var_dump($a);

PHP Deep Extend Array

How can I do a deep extension of a multi dimensional associative array (for use with decoded JSON objects).
I need the php equivalent of jQuery's $.extend(true, array1, array2) with arrays instead of JSON and in PHP.
Here's an example of what I need (array_merge_recursive didn't seem to do the same thing)
$array1 = ('1'=> ('a'=>'array1a', 'b'=>'array1b'));
$array2 = ('1'=> ('a'=>'array2a', 'c'=>'array2b'));
$array3 = array_extend($array1, $array2);
//$array3 = ('1'=> ('a'=>'array2a', 'b'=>'array1b', 'c'=>'array2b'))
Notice how array2 overrides array1 if it has same value (like how extension of classes works)
If you have PHP 5.3.0+, you can use array_replace_recursive which does exactly what you need:
array_replace_recursive() replaces the values of array1 with the same
values from all the following arrays. If a key from the first array
exists in the second array, its value will be replaced by the value
from the second array. If the key exists in the second array, and not
the first, it will be created in the first array. If a key only exists
in the first array, it will be left as is. If several arrays are
passed for replacement, they will be processed in order, the later
array overwriting the previous values.
This might be what you're looking for:
function array_extend(&$result) {
if (!is_array($result)) {
$result = array();
}
$args = func_get_args();
for ($i = 1; $i < count($args); $i++) {
// we only work on array parameters:
if (!is_array($args[$i])) continue;
// extend current result:
foreach ($args[$i] as $k => $v) {
if (!isset($result[$k])) {
$result[$k] = $v;
}
else {
if (is_array($result[$k]) && is_array($v)) {
array_extend($result[$k], $v);
}
else {
$result[$k] = $v;
}
}
}
}
return $result;
}
Usage:
$arr1 = array('a' => 1, 'b' => 2, 'c' => 3);
$arr2 = array('b' => 'b', 'd' => 'd');
array_extend($arr1, $arr2);
print_r($arr1); // array('a' => 1, 'b' => 'b', 'c' => 3, 'd' => 'd')
// or, to create a new array and leave $arr1 unchanged use:
array_extend($arr3, $arr1, $arr2);
print_r($arr3); // array('a' => 1, 'b' => 'b', 'c' => 3, 'd' => 'd')
// or, use the return value:
print_r(array_extend($arr1, $arr2)); // but this will also modify $arr1
I use this in the same way I use angular.extend(dst, src) and jQuery.extend().
function extend($base = array(), $replacements = array()) {
$base = ! is_array($base) ? array() : $base;
$replacements = ! is_array($replacements) ? array() : $replacements;
return array_replace_recursive($base, $replacements);
}
Example:
si() is a utility sanitize function that grabs $_POST or $_GET and returns an array.
$s = extend(array(
'page' => 1,
'take' => 100,
'completed' => 1,
'incomplete' => 1,
), si());
Taken from array_merge docs:
function array_extend($a, $b) {
foreach($b as $k=>$v) {
if( is_array($v) ) {
if( !isset($a[$k]) ) {
$a[$k] = $v;
} else {
$a[$k] = array_extend($a[$k], $v);
}
} else {
$a[$k] = $v;
}
}
return $a;
}
You should use: https://github.com/appcia/webwork/blob/master/lib/Appcia/Webwork/Storage/Config.php#L64
/**
* Merge two arrays recursive
*
* Overwrite values with associative keys
* Append values with integer keys
*
* #param array $arr1 First array
* #param array $arr2 Second array
*
* #return array
*/
public static function merge(array $arr1, array $arr2)
{
if (empty($arr1)) {
return $arr2;
} else if (empty($arr2)) {
return $arr1;
}
foreach ($arr2 as $key => $value) {
if (is_int($key)) {
$arr1[] = $value;
} elseif (is_array($arr2[$key])) {
if (!isset($arr1[$key])) {
$arr1[$key] = array();
}
if (is_int($key)) {
$arr1[] = static::merge($arr1[$key], $value);
} else {
$arr1[$key] = static::merge($arr1[$key], $value);
}
} else {
$arr1[$key] = $value;
}
}
return $arr1;
}
With a little googling I found this:
/**
* jquery style extend, merges arrays (without errors if the passed values are not arrays)
*
* #return array $extended
**/
function extend() {
$args = func_get_args();
$extended = array();
if(is_array($args) && count($args)) {
foreach($args as $array) {
if(is_array($array)) {
$extended = array_merge($extended, $array);
}
}
}
return $extended;
}
extend($defaults, $new_options);
I guess here is the correct answer, because:
your answer have a bug with warning:
Warning: Cannot use a scalar value as an array in...
Because $a is not always an array and you use $a[$k].
array_merge_recursive does indeed merge arrays, but it converts values with duplicate keys to arrays rather than overwriting the value in the first array with the duplicate value in the second array, as array_merge does.
other aswers are not recursives or not simple.
So, here is my answer: your answer without bugs:
function array_extend(array $a, array $b) {
foreach($b as $k=>$v) {
if( is_array($v) ) {
if( !isset($a[$k]) ) {
$a[$k] = $v;
} else {
if( !is_array($a[$k]){
$a[$k]=array();
}
$a[$k] = array_extend($a[$k], $v);
}
} else {
$a[$k] = $v;
}
}
return $a;
}
And my answer with ternary operator:
function array_extend(array $a, array $b){
foreach($b as $k=>$v)
$a[$k] = is_array($v)&&isset($a[$k])?
array_extend(is_array($a[$k])?
$a[$k]:array(),$v):
$v;
return $a;
}
Edit: And a bonus one with as many arrays you want:
function array_extend(){
$args = func_get_args();
while($extended = array_shift($args))
if(is_array($extended))
break;
if(!is_array($extended))
return FALSE;
while($array = array_shift($args)){
if(is_array($array))
foreach($array as $k=>$v)
$extended[$k] = is_array($v)&&isset($extended[$k])?
array_extend(is_array($extended[$k])?
$extended[$k]:array(),$v):
$v;
}
return $extended;
}

Pass key of object via arguments

I have this code:
function cmp_asc($a, $b){
$ta = date_create_from_format('Y/m/d', $a['props']['t']); // Y/n/j si no tienen 0 inicial
$tb = date_create_from_format('Y/m/d', $b['props']['t']);
$interval = date_diff($ta, $tb);
if($interval->days != 0){
if($interval->invert == 1){
return 1;
}else{
return -1;
}
}else{
return 0;
}
}
$arr1 = array(
'props' => array('t' => '2012/05/20')
);
$arr2 = array(
'props' => array('t' => '2012/05/21')
);
$arr3 = array(
'props' => array('t' => '2012/04/14')
);
$arr = array($arr1, $arr2, $arr3);
uasort($arr, 'cmp_asc');
and I'd like to know if I can pass the ['props']['t'] via arguments.
So, it will end up like:
function cmp_asc($a, $b, $key){
$ta = date_create_from_format('Y/m/d', $a <-- $key -->);
...
...
I'm thinking about variable variables, but I'm not too sure this is the right way to do it.
Any other ideas?
Not really as argument, because you're not calling the function yourself, but you can use closures to pass other variables into the function:
$key1 = 'props';
$key2 = 't';
uasort($arr, function ($a, $b) use ($key1, $key2) {
$ta = date_create_from_format('Y/m/d', $a[$key1][$key2]);
...
});
That's just a simple example. If you need dynamic key depths, this'll need a bit more code, along these lines:
function getValue(array $value, $key) {
$keys = explode('.', $key);
foreach ($keys as $k) {
$value = $value[$k];
}
return $value;
}
$arr = array('foo' => array('bar' => 'baz'));
echo getValue($arr, 'foo.bar');
Another idea would be using a class, usage of which would look like this:
$cmp = new ComparisonClass;
$cmp->key = 'props.t';
uasort($arr, array($cmp, 'compare'));
I'll let you figure out the implementation of ComparisonClass.
A nicer solution may be to simply standardize the format of the array you're going to sort.

Is there a php function like python's zip?

Python has a nice zip() function. Is there a PHP equivalent?
As long as all the arrays are the same length, you can use array_map with null as the first argument.
array_map(null, $a, $b, $c, ...);
If some of the arrays are shorter, they will be padded with nulls to the length of the longest, unlike python where the returned result is the length of the shortest array.
array_combine comes close.
Otherwise nothing like coding it yourself:
function array_zip($a1, $a2) {
for($i = 0; $i < min(length($a1), length($a2)); $i++) {
$out[$i] = [$a1[$i], $a2[$i]];
}
return $out;
}
Try this function to create an array of arrays similar to Python’s zip:
function zip() {
$args = func_get_args();
$zipped = array();
$n = count($args);
for ($i=0; $i<$n; ++$i) {
reset($args[$i]);
}
while ($n) {
$tmp = array();
for ($i=0; $i<$n; ++$i) {
if (key($args[$i]) === null) {
break 2;
}
$tmp[] = current($args[$i]);
next($args[$i]);
}
$zipped[] = $tmp;
}
return $zipped;
}
You can pass this function as many array as you want with as many items as you want.
This works exactly as Python's zip() function, and is compatible also with PHP < 5.3:
function zip() {
$params = func_get_args();
if (count($params) === 1){ // this case could be probably cleaner
// single iterable passed
$result = array();
foreach ($params[0] as $item){
$result[] = array($item);
};
return $result;
};
$result = call_user_func_array('array_map',array_merge(array(null),$params));
$length = min(array_map('count', $params));
return array_slice($result, 0, $length);
};
It merges the arrays in the manner Python's zip() does and does not return elements found after reaching the end of the shortest array.
The following:
zip(array(1,2,3,4,5),array('a','b'));
gives the following result:
array(array(1,'a'), array(2,'b'))
and the following:
zip(array(1,2,3,4,5),array('a','b'),array('x','y','z'));
gives the following result:
array(array(1,'a','x'), array(2,'b','y'))
Check this demonstration for a proof of the above.
EDIT: Added support for receiving single argument (array_map behaves differently in that case; thanks Josiah).
Solution
The solution matching zip() very closely, and using builtin PHP functions at the same time, is:
array_slice(
array_map(null, $a, $b, $c), // zips values
0, // begins selection before first element
min(array_map('count', array($a, $b, $c))) // ends after shortest ends
);
Why not simple array_map(null, $a, $b, $c) call?
As I already mentioned in my comment, I tend to favor nabnabit's solution (array_map(null, $a, $b, ...)), but in a slightly modified way (shown above).
In general this:
array_map(null, $a, $b, $c);
is counterpart for Python's:
itertools.izip_longest(a, b, c, fillvalue=None)
(wrap it in list() if you want list instead of iterator). Because of this, it does not exactly fit the requirement to mimic zip()'s behaviour (unless all the arrays have the same length).
You can find zip as well as other Python functions in Non-standard PHP library. Including operator module and defaultarray.
use function nspl\a\zip;
$pairs = zip([1, 2, 3], ['a', 'b', 'c']);
I wrote a zip() functions for my PHP implementation of enum.
The code has been modified to allow for a Python-style zip() as well as Ruby-style. The difference is explained in the comments:
/*
* This is a Python/Ruby style zip()
*
* zip(array $a1, array $a2, ... array $an, [bool $python=true])
*
* The last argument is an optional bool that determines the how the function
* handles when the array arguments are different in length
*
* By default, it does it the Python way, that is, the returned array will
* be truncated to the length of the shortest argument
*
* If set to FALSE, it does it the Ruby way, and NULL values are used to
* fill the undefined entries
*
*/
function zip() {
$args = func_get_args();
$ruby = array_pop($args);
if (is_array($ruby))
$args[] = $ruby;
$counts = array_map('count', $args);
$count = ($ruby) ? min($counts) : max($counts);
$zipped = array();
for ($i = 0; $i < $count; $i++) {
for ($j = 0; $j < count($args); $j++) {
$val = (isset($args[$j][$i])) ? $args[$j][$i] : null;
$zipped[$i][$j] = $val;
}
}
return $zipped;
}
Example:
$pythonzip = zip(array(1,2,3), array(4,5), array(6,7,8));
$rubyzip = zip(array(1,2,3), array(4,5), array(6,7,8), false);
echo '<pre>';
print_r($pythonzip);
print_r($rubyzip);
echo '<pre>';
// create
$a = array("a", "c", "e", "g", "h", "i");
$b = array("b", "d", "f");
$zip_array = array();
// get length of the longest array
$count = count(max($a, $b));
// zip arrays
for($n=0;$n<$count;$n++){
if (array_key_exists($n,$a)){
$zip_array[] = $a[$n];
}
if (array_key_exists($n,$b)){
$zip_array[] = $b[$n];
}
}
// test result
echo '<pre>'; print_r($zip_array); echo '<pre>';
function zip() {
$zip = [];
$arrays = func_get_args();
if ($arrays) {
$count = min(array_map('count', $arrays));
for ($i = 0; $i < $count; $i++) {
foreach ($arrays as $array) {
$zip[$i][] = $array[$i];
}
}
}
return $zip;
}
This works like in Python
function zip(...$arrays) {
return array_filter(
array_map(null, ...(count($arrays) > 1 ? $arrays : array_merge($arrays, [[]]))),
fn($z) => count($z) === count(array_filter($z)) || count($arrays) === 1
);
}
/**
* Takes an arbitrary number of arrays and "zips" them together into a single
* array, taking one value from each array and putting them into a sub-array,
* before moving onto the next.
*
* If arrays are uneven lengths, will stop at the length of the shortest array.
*/
function array_zip(...$arrays) {
$result = [];
$args = array_map('array_values',$arrays);
$min = min(array_map('count',$args));
for($i=0; $i<$min; ++$i) {
$result[$i] = [];
foreach($args as $j=>$arr) {
$result[$i][$j] = $arr[$i];
}
}
return $result;
}
Usage:
print_r(array_zip(['a','b','c'],[1,2,3],['x','y']));
Output:
Array
(
[0] => Array
(
[0] => a
[1] => 1
[2] => x
)
[1] => Array
(
[0] => b
[1] => 2
[2] => y
)
)
To overcome the issues with passing a single array to map_array, you can pass this function...unfortunately you can't pass "array" as it's not a real function but a builtin thingy.
function make_array() { return func_get_args(); }
Dedicated to those that feel like it should be related to array_combine:
function array_zip($a, $b)
{
$b = array_combine(
$a,
$b
);
$a = array_combine(
$a,
$a
);
return array_values(array_merge_recursive($a,$b));
}
you can see array_map method:
$arr1 = ['get', 'method'];
$arr2 = ['post'];
$ret = array_map(null, $arr1, $arr2);
output:
[['get', 'method'], ['post', null]]
php function.array-map

Built-in PHP function to reset the indexes of an array?

Example:
$arr = array(1 => 'Foo', 5 => 'Bar', 6 => 'Foobar');
/*... do some function so $arr now equals:
array(0 => 'Foo', 1 => 'Bar', 2 => 'Foobar');
*/
Use array_values($arr). That will return a regular array of all the values (indexed numerically).
PHP docs for array_values
array_values($arr);
To add to the other answers, array_values() will not preserve string keys. If your array has a mix of string keys and numeric keys (which is probably an indication of bad design, but may happen nonetheless), you can use a function like:
function reset_numeric_keys($array = array(), $recurse = false) {
$returnArray = array();
foreach($array as $key => $value) {
if($recurse && is_array($value)) {
$value = reset_numeric_keys($value, true);
}
if(gettype($key) == 'integer') {
$returnArray[] = $value;
} else {
$returnArray[$key] = $value;
}
}
return $returnArray;
}
Not that I know of, you might have already checked functions here
but I can imagine writing a simple function myself
resetarray($oldarray)
{
for(int $i=0;$i<$oldarray.count;$i++)
$newarray.push(i,$oldarray[i])
return $newarray;
}
I am little edgy on syntax but I guess u got the idea.

Categories