Consider the following array as input:
$input = array('A', 'B', 'C', 'D');
I'm looking for a way to make loop though this array, writing down every possible pair of two values. In this example: AB AC AD BC BD CD. Please not that BA doesn't count as a pair, since AB is already mentioned:
$output = array(
'A' => 'B',
'A' => 'C',
'A' => 'D',
'B' => 'C',
'B' => 'D'
);
Any input on how to get this started is appreciated!
$output=array();
for ($i=0;$i<sizeof($input);$i++) {
$k=$input[$i];
for ($j=$i+1;$j<sizeof($input);$j++) {
$v=$input[$j];
$output[]=array($k=>$v);
}
}
Edit
As of your comment, the restructured output
$output=array();
//See below
for ($i=0;$i<sizeof($input);$i++) {
$k=$input[$i];
$v=array();
for ($j=$i+1;$j<sizeof($input);$j++) {
$v[]=$input[$j];
}
$output[]=array($k=>$v);
}
This will give you 'D'=>Array() as a last row, if you don't want hti you have to change
for ($i=0;$i<sizeof($input);$i++) {
to
for ($i=0;$i<sizeof($input)-1;$i++) {
something like this maybe;
$input = array('A', 'B', 'C', 'D');
$input_copy = $input;
$output = array();
$i = 0;
foreach($input as $val) {
$j = 0;
foreach($input_copy as $cval) {
if($j < $i) break;
$output[] = array($val => $cval);
$j++;
}
$i++;
}
$output = array(
0 => array('A' => 'A'),
1 => array('A' => 'B'),
2 => array('A' => 'C'),
...
);
Note that your output array is impossible, as the key is overwritten each time
This won't be possible in PHP, as PHP array can have only unique keys.
You can get output as
$output = array(
'A' => array('B','C','D'),
'B' => array('C','D')
);
$input = array('A', 'B', 'C', 'D');
foreach($input as $key => $value){
$tempKey = $key;
for($key +1 ; $key < count($input) ; $key++){
$result[$tempKey][] = $input[$key];
}
}
You can use this generic method:
function combine($inputArray, &$outputArray, $index, $combLen) {
global $outstr;
for ($i = $index; $i < count($inputArray); $i++) {
$outstr.=$inputArray[$i];
if(strlen($outstr) == $combLen) {
$outputArray[]= $outstr;
}
combine($inputArray, $outputArray, $i + 1, $combLen);
$outstr = substr($outstr, 0, strlen($outstr)-1);
}
}
See it on ideone
if you need to work with pairs as arrays
$pairs = [];
foreach($a as $k => $v) {
foreach(array_slice($a, $k + 1) as $k2 => $v2) {
$pairs[] = [$v, $v2];
}
}
Related
I'm trying to create a script to create pairs from 2 different arrays. For example I have two array given below:
<?php
//Array 1
$arr1 = array('A', 'B', 'C', 'D');
//Array 2
$arr2 = array('X', 'Y', 'Z');
And I need output in such a manner so that it match with each element and didn't repeat pair and sequence also. Here is expected output:
$output = array(
0 => array('A', 'X'),
1 => array('B', 'Y'),
2 => array('C', 'Z'),
3 => array('D', 'X'),
4 => array('A', 'Y'),
5 => array('B', 'Z'),
6 => array('C', 'X'),
7 => array('D', 'Y'),
8 => array('A', 'Z'),
9 => array('B', 'X'),
10 => array('C', 'Y'),
11 => array('D', 'Z')
)
**Note: 2 arrays can differ with number of count and values.
This is Cartesian Product. Could be achieved with
//Array 1
$arr1 = array('A', 'B', 'C', 'D');
//Array 2
$arr2 = array('X', 'Y', 'Z');
$output = array();
foreach($arr1 as $i1){
foreach ($arr2 as $i2) {
$output[] = array($i1, $i2);
}
}
echo json_encode($output);
If you want the output to be in the same order as you have mentioned above, following should do that.
$newoutput = array();
$x = 0;
$y = 0;
for($i = 1; $i <= (count($arr1) * count($arr2)); $i++){
$newoutput[] = array($arr1[($x % count($arr1))], $arr2[($y % count($arr2))]);
$x++;
$y++;
}
echo "<br />", json_encode($newoutput);
Output however, is a different order of the same output as previous.
EDIT
The second works only when (count($arrX) % count($arrY)) > 0.
I don't think there is a built in support for that.
You need to loop the arrays nested and build a result array.
$arr1 = array('A', 'B', 'C', 'D');
$arr2 = array('X', 'Y', 'Z');
foreach($arr1 as $item1){
foreach($arr2 as $item2){
$res[] = [$item1, $item2];
}
}
var_dump($res);
https://3v4l.org/CdE8Q
Try This out (This will sort out the same pair if found):
//Array 1
$arr1 = array('A', 'B', 'C', 'D', 'A', 'E'); // Same element will be sort out in pair
//Array 2
$arr2 = array('X', 'Y', 'Z' , 'X', 'Y', 'X'); // Same element will be sort out in pair
$output = array();
$used_pair = array(); // Keeping used pair
$i = 1;
foreach ($arr1 as $each1) {
foreach ($arr2 as $each2) {
if (!in_array($each1.$each2, $used_pair)) {
$output[] = array($each1, $each2);
$used_pair[] = $each1 . $each2;
}
}
}
print_r($used_pair); // array with pair string
print_r($output); // array with pair array
this is an example that ouputs unique values in the same order :
online eval : https://3v4l.org/H2pad
<?php
function cartesian(array $x,array $y): array
{
$result = [];
$size = count($x) * count($y);
$posX = 0;
$posY = 0;
for($i = 0; $i < $size; $i++) {
if (!isset($x[$posX])) {
$posX = 0;
} elseif(!isset($y[$posY])) {
$posY = 0;
}
$result[] = [$x[$posX],$y[$posY]];
$posX++; $posY++;
}
return $result;
}
$x = array('A', 'B', 'C', 'D');
$y = array('X', 'Y', 'Z');
print_r(cartesian($x,$y));
( tbh, it took me a lot of rubber duck debugging )
I have this numeric multidimensional array in php. It's Playfair matrix and I want to get the keys of a letter from the matrix.
Let's say I have the letter "P", which is on the third row(0 to 4) and second column(0 to 4). I have tried several things array_keys(not working as I want it to), functions to get the key of the first level of the array, making the array one dimensional and doing some magic tricks...Nothing works and I would like some help.
This is the function that gets the keys of the first level:
function array_search2($needle, $haystack){
$l = count($haystack);
for ($i=0; $i < $l; $i++) {
if (in_array($needle, $haystack[$i])) return $i;
}
return false;
}
I want to have this array with letters, to get two letters from it, to get these two letters their coordinates(from the PLayfair matrix) and to comprate their rows/columns.
Thank you and I'll post any code that is needed.
Edit : I'm posting all I have:
$keyword = str_replace(' ','', $_POST['keyword']);
$plaintext = str_replace(' ','', $_POST['plaintext']);
$key = str_split($keyword); //string to array
$plain = str_split($plaintext); //string to array
$alphabet = array(
0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f',
6 => 'g', 7=> 'h', 8 => 'i', 9 => 'j', 10 => 'k', 11 => 'l',
12 => 'm', 13 => 'n', 14 => 'o', 15 => 'p', 16 => 'q', 17 => 'r',
18 => 's', 19 => 't', 20 => 'u', 21 => 'v', 22 => 'w', 23 => 'x',
24 => 'y');
for ($i=0; $i < count($key); $i++) {
for ($j=0; $j < count($alphabet) ; $j++) {
if ($keyword[$i] == $alphabet[$j]) {
unset($alphabet[$j]);
}
}
}
$keywordFilpped = array_flip($key);
$alphabetFlipped = array_flip($alphabet);
$mergedFlipped = $keywordFilpped + $alphabetFlipped;
$i = 0;
foreach ($mergedFlipped as $key => $value) {
$mergedFlipped[$key] = $i;
$i++;
}
$merged = array_flip($mergedFlipped);
$index = 0;
for ($i=0; $i < 5; $i++) {
for ($j=0; $j < 5; $j++) {
$matrix[$i][$j] = $merged[$index];
$index++;
}
}
echo "PLAYFAIR MATRIX:<br>";
for ($i=0; $i < 5; $i++) {
for ($j=0; $j < 5; $j++) {
echo $matrix[$i][$j];
}
echo "<br>";
}
if you have a matrix should check with 2 for like following code:
for($i = 1; $i <= 11; $i++)
{
for($j = 1; $j <= 11; $j++)
{
if($array[$i][$j] == 'p') echo "row=$i and col = $j";
}
}
in the above example i check all rows and rows cols
use from this function
function array_search2($needle, $haystack){
$i = 0;
foreach($haystack as $key => $value)
{
$j = 0;
foreach($value as $key2 => $value2)
{
if($value2 == $needle)
return array($i, $j);
$j++;
}
$i++;
}
return false;
}
this function will return an array contain row and col of found status or false;
i tested with following data:
$array = array( 0 => array(0 => 'a', 1=> 'b'), 1 => array(0 => 'c', 1=> 'd'));
print_r(array_search2('d', $array));
// out put Array ( [0] => 1 [1] => 1 )
I've improved your function, which will return keys if match found, irrespective of depth, See example below:
function getArrayKeys($needle, $haystack, $depth = 0) {
$keys = array();
foreach($haystack as $key => $value) {
if(is_array($value)){
$result = getArrayKeys($needle, $value, $depth + 1);
if(!empty($result) || $result === 0) {
if (is_array($result)) {
$keys = array_merge($keys, $result);
} else {
$keys[] = $result;
}
$keys[] = $key;
}
if(!empty($keys) && $depth > 0)
return $keys;
} elseif($value == $needle) {
return $key;
}
}
return !empty($keys) ? array_reverse($keys) : false;
}
$arr = array(
array('A', 'B', 'C'),
array('D', 'F', 'E'),
array('Z', 'Y', 'X'),
array('f' => '44', 'g' => array('99'))
);
$keys = getArrayKeys('99', $arr);
Output:
Array
(
[0] => 3
[1] => g
[2] => 0
)
See it running: https://eval.in/571698
I have an array like :
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
)
And I want add semicolon(;) every 3 index value and it's read from end of array that result is string like "ab;cde;fgh";
Here's a fun, and kind of obnoxious, one-liner:
$str = ltrim(strrev(chunk_split(implode(array_reverse($arr)), 3, ';')), ';');
Example:
$arr = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$str = ltrim(strrev(chunk_split(implode(array_reverse($arr)), 3, ';')), ';');
echo $str; //ab;cde;fgh
// More sample output based on different input arrays:
$arr = array('a', 'b', 'c', 'd', 'e'); //ab;cde
$arr = array('a', 'b', 'c', 'd', 'e', 'f'); //abc;def
$arr = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'); //ab;cde;fgh;ijk
See demo
Its an odd way, but since you want it reversed you may need to use some function here:
$array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$array = array_reverse($array); // reverse it
$array = array_chunk($array, 3); // cut by threes
$string = '';
foreach ($array as $value) {
$string .= implode($value); // glue them
if(count($value) == 3) { // if still three, add a semi
$string .= ';';
}
}
$string = strrev($string); // then reverse them again
echo $string; // ab;cde;fgh
This works... there's a couple different ways to do this. This was the quickest way off the top of my head without using a second array.
$vars = array("a","b","c","d","e","f","g","h");
print_r(insert_char($vars));
function insert_char($Array, $Delimeter=";", $Count=3) {
for ($i = sizeOf($Array) - $Count; $i > 0; $i -= $Count)
array_splice($Array, $i, 0, $Delimeter);
return implode($Array);
}
Result
ab;cde;fgh
Here's my solution, although it feels inefficient it is pretty clear:
<?php
$myarr=Array('a','b','c','d','e','f','g','h');
$newarr=Array();
$arrsize = sizeof($myarr);
for ($x = 0, $i = 0; $x < $arrsize; $x++, $i++) {
$newarr[$i] = $myarr[$arrsize - $x - 1];
if (($arrsize - $x) % 3 == 0) {
$i++;
$newarr[$i] = ";";
}
}
$newarr = array_reverse($newarr);
print_r($newarr);
?>
Just traversing it from backward and joining the string in reverse too gives that result.
$vars = array("a","b","c","d","e","f","g");
$c = 0; $s = "";
for ($i = sizeof($vars)-1; $i >= 0; $i--)
{
$c++;
$s = $vars[$i].$s;
if ($c == 3) { $c = 0; $s = ";".$s; }
}
echo $s;
I would like to take an array like this and combine it into 1 single array.
array (size=2)
0 =>
array (size=10)
0 => string '1'
1 => string 'a'
2 => string '3'
3 => string 'c'
1 =>
array (size=5)
0 => string '2'
1 => string 'b'
However I want the array results to be interleaved.
So it would end up looking like
array
0 => '1'
1 => '2'
2 => 'a'
3 => 'b'
4 => '3'
5 => 'c'
I would like it so that it doesn't matter how many initial keys are passed in (this one has 2), it should work with 1, 2 or 5. Also, as you can see from my example the amount of elements most likely won't match.
Anyone know the best way to accomplish this?
$data = array(
0 => array(
0 => '1',
1 => 'a',
2 => '3',
3 => 'c',
),
1 => array(
0 => '2',
1 => 'b',
),
);
$newArray = array();
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$mi->attachIterator(new ArrayIterator($data[0]));
$mi->attachIterator(new ArrayIterator($data[1]));
foreach($mi as $details) {
$newArray = array_merge(
$newArray,
array_filter($details)
);
}
var_dump($newArray);
I had fun with this... So if you like it use it!
$arr1 = [1,'a',3,'c'];
$arr2 = ['2','b'];
$finarry = arrayInterweave($arr1,$arr2);
print_r($finarry);
function arrayInterweave($arr1,$arr2){
$count1 = count($arr1);
$count2 = count($arr2);
$length = (($count1 >= $count2) ? $count1 : $count2);
$fin = array();
for($i = 0;$i<$length;$i++){
if(!empty($arr1[$i])){
$fin[] = $arr1[$i];
}
if(!empty($arr2[$i])){
$fin[] = $arr2[$i];
}
}
return $fin;
}
Tried to think of a fun solution:
$array = [
["a","b","c"],
["d","e"]
];
$result = [];
while($array) {
array_walk(
$array,
function(&$subarray, $key) use (&$array, &$result) {
$result[] = array_shift($subarray);
if(empty($subarray)) unset ($array[$key]);
}
);
}
var_dump($result);
It destroys the original array though.
After determining which row contains the most elements, you can loop through known indexes and push columns of data into the result array.
The following technique is safe to use with a variable number of rows.
Code: (Demo)
$maxCount = max(array_map('count', $array));
$result = [];
for ($i = 0; $i < $maxCount; ++$i) {
array_push($result, ...array_column($array, $i));
}
var_export($result);
Input/Output:
$array
$result
[['b', 'e', 'd', 's'], ['l', 'n']]
['b', 'l', 'e', 'n', 'd', 's']
['f', 'g', 'n', 's'], ['r', 'm'], ['a', 'e', 't']
['f', 'r', 'a', 'g', 'm', 'e', 'n', 't' 's']
The above technique is perfectly capable of accommodating 3 or more input arrays as well.
p.s. For anyone running into technical limitations because their php version, this will do the same:
$maxCount = max(array_map('count', $array));
$result = [];
for ($i = 0; $i < $maxCount; ++$i) {
foreach (array_column($array, $i) as $found) {
$result[] = $found;
}
}
...if your php version doesn't accommodate the above snippet, you really, really need to upgrade your php version (sorry, not sorry).
To avoid the counting to determine the longest subarray, you can instead transpose the data with nested loops then flatten that result structure. (Demo)
$result = [];
foreach ($array as $i => $row) {
foreach ($row as $k => $v) {
$result[$k][$i] = $v;
}
}
var_export(array_merge(...$result));
EDIT: making it clear that I need it for multidimensional arrays (at any depth level)
I need to cut down the size of an array to get only a portion of it, but this needs to be done recursively. For example, take the following case:
$a = array(
'a',
'b' => array(
'x' => array(
'aleph',
'bet'
),
'y'),
'c',
'd',
'e'
);
what I need is that after copying only 4 elements I'll get the following resulted array:
$a = array(
'a',
'b' => array('x' => array(
'aleph'
),
),
);
and not...
$a = array(
'a',
'b' => array('x' => array(
'aleph',
'bet'
),
'y'),
'c',
'd',
);
How do I achieve this?
Thanks!
You can try **Note : dual-dimensional
$a = array("a","b" => array('x','y'),"c","d","e");
$new = __cut($a);
function __cut($array, $max = 4) {
$total = 0;
$new = array();
foreach ( $array as $key => $value ) {
if (is_array($value)) {
$total ++;
$diff = $max - $total;
$slice = array_slice($value, 0, $diff);
$total += count($slice);
$new[$key] = $slice;
} else {
$total ++;
$new[$key] = $value;
}
if ($total >= $max)
break;
}
return $new;
}
var_dump($new);
Output
array
0 => string 'a' (length=1)
'b' =>
array
0 => string 'x' (length=1)
1 => string 'y' (length=1)
function arrayTrim($array, $size, $finalArray = null){
global $count;
foreach ($array AS $key => $val){
if($size == $count)
return $finalArray;
$count++;
if(is_array($val)){
$finalArray[$key] = array();
$finalArray[$key] = arrayTrim ($val, $size, $finalArray[$key]);
}
else
$finalArray[$key] = $val;
}
return $finalArray;
}
$a = array( "a"=> array('xa', 'ya'), "b" => array('x', 'y'), "c", "d", "e" );
print_r(arrayTrim($a, 4));
should work fine