This question already has answers here:
PHP Find All (somewhat) Unique Combinations of an Array
(6 answers)
Closed 8 years ago.
I have list of 100 First Name and 100 Surname.
I want use mix of all the combinations without repetition.
Do you have some math solutions for this?
this is called "cartesian product", php man page on arrays http://php.net/manual/en/ref.array.php shows some implementations (in comments).
and here's yet another one:
function array_cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
$cross = array_cartesian(
array('A', 'B', 'C'),
array('1', '2')
);
print_r($cross);
Related
This question already has answers here:
Transpose and flatten two-dimensional indexed array where rows may not be of equal length
(4 answers)
Closed 5 months ago.
Is there a straightforward way in PHP to combine multiple arrays together but respect the relative order of the elements?
So for example if I had two arrays
$x = ('1','2','3','4');
and
$y = array('a','b','c','d','e');
and I wanted combine them into a single array
$z = ('1','a','2','b','3','c','4','d','e');
Is there a straightforward way of doing this? The ideal solution would also account for different lengths of arrays, behaving as my example does.
array_merge doesn't seem to achieve what I want as it just appends one array to the other.
My current solution loops through each array in order and pushes values to my $z array. This is not only inelegant, it also requires the input arrays to have the same number of values, which isn't ideal for my application.
Any insight would be appreciated
I think this should work -
$count = max(count($x), count($y));
$newArr = array();
for ($i = 0; $i < $count; $i++) {
// enter values to the new array based on key
foreach(array($x, $y) as $arr) {
if (isset($arr[$i])) {
$newArr[] = $arr[$i];
}
}
}
var_dump($newArr);
You could use this generic function, which can accept any number of arrays, and will output an array with the elements taken from the first "column" first, then from the second, etc:
function mix_merge(...$args) {
return call_user_func_array('array_merge',
array_map(function($i) use ($args) { return array_column($args, $i); },
range(0, max(array_map('count', $args))-1)));
}
// Sample data:
$x = array('1','2','3','4');
$y = array('a','b','c','d','e');
$z = array('A','B','C');
$res = mix_merge($x, $y, $z);
Result array will be:
['1', 'a', 'A', '2', 'b', 'B', '3', 'c', 'C', '4', 'd', 'e']
Iterate from 0 to the greater length of the two arrays.
At each step, if array $x contains item at index, push it to final array $z. Do the same for array $y.
You can also try this
$x = array('1','2','3','4');
$y = array('a','b','c','d','e');
foreach($y as $key=>$value){
if($x[$key]){
$z[] = $x[$key];
$z[] = $value;
}else {
$z[] = $value;
}
}
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I'm using a function called subval_sort to sort a multi-dimensional array.
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$songs = array(
'1' => array('artist'=>'Bing Crosby', 'songname'=>'White Christmas'),
'2' => array('artist'=>'Elvis Presley', 'songname'=>'White Christmas'),
'3' => array('artist'=>'Abba', 'songname' =>'Waterloo')
);
$songs = subval_sort($songs,'songname');
print_r($songs);
Works fine. Now I want to sort by songname as first and artist as second. So: if two (or more) songname-values are the same I want to sort by artist. Like in SQL: ORDER BY songname, artist.
Do you have any ideas how to solve it?
You can use usort where you can define the custom comparison function
function cmp($a, $b)
{
if(strcmp($a['songname'], $b['songname'])) {
return strcmp($a['songname'], $b['songname']);
}
return strcmp($a["artist"], $b["artist"]);
}
implementation:
usort($songs, "cmp");
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;
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];
}
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
This is really simple but I need a quick way to do this.
I have three arrays like
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
How do I combine them to make
array (
[0] => array ('a','p','x');
[1] => array ('b','q','y');
[2] => array ('c','r','z');
);
Wouldn't array_map(null, $a, $p, $x); be better?
See array_mapĀDocs.
<?php
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
$arr = array();
for($i=0; $i<count($a); $i++){
$arr[$i] = array($a[$i], $p[$i], $x[$i]);
}
?>
array_map is simpler, but for the sake of possibibility, a quickly typed code example to make use of the MultipleIterator to solve the issue:
$it = new MultipleIterator;
foreach(array($a, $p, $x) as $array) {
$it->attachIterator(new ArrayIterator($array));
}
$items = iterator_to_array($it, FALSE);
Might be handy in case it's more than an array.