This question already has answers here:
Cartesian Product of N arrays
(10 answers)
Closed 9 years ago.
Let's say I have several arrays:
$array1 = array( 'a','b','c');
$array2 = array( '1','2','3');
$array3 = array( '+','-');
As a result I'd like to have a array of all possible mixes of those arrays:
$result = array( 'a1+','a1-','a2+','a2-','b1+','b1-','b2+'...
SQL provides such operation in case of the following request:
SELECT * FROM `letters`,`digits`,`operations`
Ho can I do this in PHP?
$permute= array();
foreach($array1 as $x)
foreach($array2 as $y)
foreach ($array3 as $z)
$permute[]= $x.$y.$z;
Related
This question already has answers here:
How to create key value pair using two arrays in JavaScript?
(7 answers)
Closed 5 years ago.
I'm trying using PHP to bring the two JSON lists into one, as follows:
list1
[1,2,3,4,5]
list2
["a","b","c","d","e"]
How to join lists to get:
[[1,"a"],[2,"b"],[3,"c"],[4,"d"],[5,"e"]]
How to link two lists to one, as in a given example?
You need to loop through the arrays, and create a new one with the combined values:
<?php
$arr1 = [1,2,3,4,5];
$arr2 = ["a","b","c","d","e"];
$result = [];
foreach ($arr1 as $i => $a) {
$result[] = [$a, $arr2[$i] ?? ''];
}
?>
This question already has answers here:
PHP combine two associative arrays into one array
(8 answers)
Closed 5 years ago.
Whats the easiest way to convert array a to b
a= [['x'=>'a'], ['y'=>'b']]
b= ['x'=>'a', 'y'=>'b']
a and b are just two examples.
Using array_walk_recursive for arbitrary depth:
$b = [];
array_walk_recursive($a, function ($v, $k) use (&$b) { $new[$k] = $v; });
Using #splash58 trick with the spread operator if you have only one level deep:
$b = array_merge(...$a);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}
This question already has answers here:
How do I sort a multi-dimensional array by value?
(3 answers)
PHP: sorting a multidimentional array ($arr[$i]['v'])
(1 answer)
Closed 9 years ago.
I want to sort the array below by 'name'. I have tried several things but I can't figure it out. Any suggestions?
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
...
You can use usort() to sort an array using a custom criteria.
For instance:
function my_sort_by_name($a, $b) {
return strcmp($a['name'], $b['name']);
}
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
usort($data, 'my_sort_by_name');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
I have the sorted array
$A = array(
0=>EUR,
1=>GBP,
2=>USD
);
$B = array(
0=>'0.88',
1=>'0'
);
I want to map to be like this allways put 'EUR'=>'1':
$C = array(
'EUR'=>'1',
'GBP'=>'0.88',
'USD'=>'0'
);
Could anyone tell me please?
$C = array_combine($A, array_merge(array(1), $B));
$C = array();
foreach ($A as $key => $value)
{
$C[$value] = $B[$key];
}