Foreach loop + recursion - php

I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
$combinations = array();
foreach($sets[0] as $s1)
foreach($sets[1] as $s2)
foreach($sets[2] as $s3)
foreach($sets[3] as $s4)
$combinations[] = array($s1, $s2, $s3, $s4);
print_r($combinations);

You can do it in recursion like this. The output is identical to your loops
<?php
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
function get_combinations($sets, &$combinations = array(), &$row = array()) {
if (count($sets) == 0) {
$combinations[] = $row;
return $combinations;
}
foreach ($sets[0] as $s) {
$row[] = $s;
get_combinations(array_slice($sets, 1), $combinations, $row);
array_pop($row);
}
return $combinations;
}
$combinations = get_combinations($sets);
print_r($combinations);

Related

How to combine 2 arrays with the condition that 2 and 5 values will be from the second array?

There are two arrays:
$arrOne = [1, 3, 4];
$arrTwo = [2, 5,];
$newArr = [];
How to merge to get like this;
$newArr = [1, 2, 3, 4, 5];
Now displays through one, this option is not suitable.
foreach ($arrOne as $k => $v) {
$newArr[] = $v;
$newArr[] = $arrTwo[$k];
}
Here is another example. Tthe values can be different in the array.
$arrOne = [154, 32, 15];
$arrTwo = [682, 124,];
Again for this example it is necessary that the values from the second array always be at 2 and 5 positions in the new array:
$newArr = [154, 682, 32, 15, 124];
You can use array_splice to merge the arrays in the intended way to the specific indices:
array_splice( $arrOne, 1, 0, $arrTwo[0]);
array_splice( $arrOne, 4, 0, $arrTwo[1]);
var_dump($arrOne);
Demo 1:
Using original data:
$arrOne = [1, 3, 4];
$arrTwo = [2, 5,];
https://3v4l.org/dAMav
Demo 2:
Using second set of data:
$arrOne = [154, 32, 15];
$arrTwo = [682, 124,];
https://3v4l.org/Xhl3K
You can use array_merge and sort functions to achieve this as following:
$newArr = array_merge($arrOne, $arrTwo); // it will be [1, 3, 4, 2, 5]
sort($newArr); // it will sort array [1, 2, 3, 4, 5]
You can use $newArr now which have sorted data
$arrOne = [1, 3, 4];
$arrTwo = [2, 5];
$newArr = [];
foreach($arrOne as $key => $one) {
switch($key) {
case 1:
$newArr[] = $arrTwo[0];
break;
case 4:
$newArr[] = $arrTwo[1];
break;
}
$newArr[] = $one;
}
if(count($newArr) < 5) {
$newArr[] = $arrTwo[1];
}
Demo link
Here we are considering there will be always two elements in second array
$arrayOne = [12,23,45,56,65,78,99];
$arrayTwo = [2,5];
$mergedArray = [];
foreach($arrayOne as $key => $value) {
$position = $key + 1;
if ($position === 2) {
$mergedArray[] = $arrayTwo[0];
} else if ($position === 4) {
$mergedArray[] = $arrayTwo[1];
}
$mergedArray[] = $value;
}

Given two arrays combine common values

$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
The indexes of $names and $money correspond to each other.
I need to find the most efficient way to add common $money to each $names and print only the even values.
For example, john appears two times, with 2 and 3 money. So johnhas 5 money.
Desired output:
mark: 20
brian: 8
paul: 6
I am thinking of looping through each array, but this is O(n^2) Is there an easier way?
Make associative array (hash) for result:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
for ($i = 0; $i < count($names); $i++) {
if (!isset($result[$names[$i]])) {
$result[$names[$i]] = 0;
}
$result[$names[$i]] += $money[$i];
}
arsort($result); // reverse (big first) sort by money
print_r($result);
just loop through names and use it keys for money array:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$res = [];
foreach ($names as $key => $value) {
$res[$value] = isset($res[$value]) ? $res[$value] + $money[$key] : $money[$key];
}
var_dump($res);
You just have to loop through $names once and set name as key in a result- variable.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$data = [];
foreach ($names as $key => $name) {
if (!isset($data[$name])) {
$data[$name] = 0;
}
$data[$name] += $money[$key];
}
print_r($data);
Maybe you'll need a check, if both arrays have the same size.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
foreach ($names as $index => $name) {
if (empty($result[$name]) {
$result[$name] = $money[$index];
} else {
$result[$name] += $money[$index];
}
}
print_r($result);

Find to closest higher and lower number in an array in loop

I have an array. Given a number X (that must not be contained in the array), I want to search for both the next greater and next lower number of X within the array in a single loop. My code is:
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
sort($a);
$nearestGreater = null;
$nearestLower = null;
foreach ($a as $key => $val) {
if ( $v<=$val) {
$nearestGreater = (isset($a[$key + 1])) ? $a[$key + 1]: $nearestGreater;
$nearestLower = (isset($a[$key - 1])) ? $a[$key - 1]: $nearestLower;
break;
}
}
var_dump($nearestLower);
echo "<br/>".$v."<br/>";
var_dump($nearestGreater);
unset($a);
?>
Write your code in clean and readable way using Code Block
$nearestGreater=null;
$nearestLower = null;
$a = array(1, 8, 23, 25, 40,41,22,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
foreach( $a as $val)
{
if($val < $v)
{
if(isset($nearestLower))
{ if($nearestLower < $val)
$nearestLower=$val;
}
else
{
$nearestLower=$val;
}
}
if($val > $v)
{
if(isset($nearestGreater))
{ if($nearestGreater > $val)
$nearestGreater=$val;
}
else
{
$nearestGreater=$val;
}
}
}
Try this:
?>
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($a);
$v = 58;
$lesser = null;
$greater = null;
foreach($a as $key => $current){
if($current <= $v){
$lesser = $current;
$greater = $a[($key+1)];
}else{
}
}
echo "<pre>";
print_r(array(
"lesser" => $lesser,
"greater" => $greater,
));
echo "</pre>";
?>
Edited TO HAVE TWO LOWER AND TWO HIGHER EVEN WHEN MATCH
<?php
$allNumbers = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($allNumbers);
$input = 55;
$index = array_search($input, $allNumbers);
if(empty($index)){
foreach($allNumbers as $key => $value){
if($value < $input){
$index = ($key + 0.5);
}else{
break;
}
}
}
$below = array(
$allNumbers[ceil($index - 2)],
$allNumbers[ceil($index - 1)],
);
$above = array(
$allNumbers[floor($index + 1)],
$allNumbers[floor($index + 2)],
);
echo "<pre>";
print_r(array(
"input" => $input,
"index" => $index,
"below" => $below,
"above" => $above,
));
echo "</pre>";
?>

Multiply two array

I have an two array like this:
$wij = array(0.25, 0.30, 0.25. 0.15, 0.5);
$nij = array(
array(3, 3, 2, 1, 2),
array(2, 2, 3, 2, 1),
array(1, 3, 2, 2, 1));
$rij = array();
I want to multiplying a value from wij array-variable into each nij array and join the result into rij array-variable, because $nij array always have more than 3 array than from the example. I dont have any clue just using for-loops in 1 looping. Please give me an example
if you want just add the values to $rij array use the code below:
$wij = array(0.25, 0.30, 0.25, 0.15, 0.5);
$nij = array( array(3, 3, 2, 1, 2), array(2, 2, 3, 2, 1), array(1, 3, 2, 2, 1));
$rij = array();
foreach($nij as $arr) {
foreach($arr as $val) {
foreach($wij as $multiplier) {
$rij[] = $val * $multiplier;
}
}
}
print_r($rij);

MAX & MIN values on array using PHP

Does anybody knows how can I get the max and min value of the 2nd and 3rd columns in PHP?
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
Output should be like:
max(12.9)
min(10)
Another option
<?php
function array_rotate( $array )
{
$rotated = array();
foreach ( $array as $rowIndex => $col )
{
foreach ( $col as $colIndex => $value )
{
$rotated[$colIndex][$rowIndex] = $value;
}
}
return $rotated;
}
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
$ar = array_rotate( $ar );
echo max( $ar[2] ), "\n", min( $ar[1] );
<?php
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
function col($tbl,$col){
$ret = array();
foreach ($tbl as $row){
$ret[count($ret)+1] = $row[$col];
}
return $ret;
}
print (max(col($ar,2))."\n");
print (min(col($ar,1))."\n");
?>
is this what you look for? I guess its not the most efficient way.

Categories