set reduction recursive algorithm - php

Given (A,B,C,D)
What is an algorithm that can reduce it to unique non duplicated sets of size n.
For example if n is 3.
[A,B,C]
[A,C,D]
[A,B,D]
[B,C,D]
As you'll notice A,A,A cannot be valid neither can A,A,B and [A,C,D] = [C,A,D] = [A,C,D] = [D,C,A] = etc..
is there a way to not generate the powerset and reduce it because a powerset of 7 elements is n^7, which quickly gets exhaustive.

Take a look at Math_Combinatorics.
<?php
require 'Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$result = $combinatorics->combinations( ['A','B','C','D'], 3 );
var_export($result);
prints
array (
0 =>
array (
0 => 'A',
1 => 'B',
2 => 'C',
),
1 =>
array (
0 => 'A',
1 => 'B',
3 => 'D',
),
2 =>
array (
0 => 'A',
2 => 'C',
3 => 'D',
),
3 =>
array (
1 => 'B',
2 => 'C',
3 => 'D',
),
)
It also has a permutations method (i.e. [A,B,C]!=[A,C,B] and therefore both are in the result set)

Related

Get keys in multidimensional array in sorting method

I'm posting this topic because I wanted to know if it was possible to retrieve the keys of a multidimensional array in a sort method.
The structure of the table is as follows :
$array = Array();
$array['Key-1'] = Array('Element-1' => 'a', 'Element-2' => 'b', 'Element-3' => 'c', 'Element-4' => 'd');
$array['Key-2'] = Array('Element-1' => 'e', 'Element-2' => 'f', 'Element-3' => 'g', 'Element-4' => 'h');
$array['Key-3'] = Array('Element-1' => 'i', 'Element-2' => 'j', 'Element-3' => 'k', 'Element-4' => 'l');
$array['Key-4'] = Array('Element-1' => 'm', 'Element-2' => 'n', 'Element-3' => 'o', 'Element-4' => 'p');
For example, when I define a sort method, I can access the items Element-1, Element-2, Element-3 and/or Element-4, in the variables $a and $b. What i wanted to know is if i could also access the upper keys : Key-1, Key-2, Key-3 or Key-4.
function sortMethod($a, $b){
=> get here index key based on the item retrieved (Key-1, Key-2, ...)
}
usort($array, 'sortMethod');
Indeed, I need it because the sorting method must be based on these keys (Key-1, Key-2, ...) and also on the values (Element-1, Element-2, ...).
Like what we could do in a foreach loop when we specify :
foreach ($array as $key => $value) {}
Thanks for your help
Best regards
You cas use uksort() and a closure to access to 1. array keys and 2. array values :
Eg. :
<?php
$array = [
'Key-1' => ['Element-1' => 'a', 'Element-2' => 'b', 'Element-3' => 'c', 'Element-4' => 'd'],
'Key-2' => ['Element-1' => 'e', 'Element-2' => 'f', 'Element-3' => 'g', 'Element-4' => 'h'],
'Key-3' => ['Element-1' => 'i', 'Element-2' => 'j', 'Element-3' => 'k', 'Element-4' => 'l'],
'Key-4' => ['Element-1' => 'm', 'Element-2' => 'n', 'Element-3' => 'o', 'Element-4' => 'p'],
];
uksort($array, function ($key_a, $key_b) use ($array) {
echo "===================\r\n";
echo "First key : {$key_a}, first element : ";
print_r($array[$key_a]);
echo "Second key : {$key_b}, second element : ";
print_r($array[$key_b]);
// ...
});
Gives :
===================
First key : Key-1, first element : Array
(
[Element-1] => a
[Element-2] => b
[Element-3] => c
[Element-4] => d
)
Second key : Key-2, second element : Array
(
[Element-1] => e
[Element-2] => f
[Element-3] => g
[Element-4] => h
)
===================
First key : Key-2, first element : Array
(
[Element-1] => e
[Element-2] => f
[Element-3] => g
[Element-4] => h
)
Second key : Key-3, second element : Array
(
[Element-1] => i
[Element-2] => j
[Element-3] => k
[Element-4] => l
)
===================
First key : Key-3, first element : Array
(
[Element-1] => i
[Element-2] => j
[Element-3] => k
[Element-4] => l
)
Second key : Key-4, second element : Array
(
[Element-1] => m
[Element-2] => n
[Element-3] => o
[Element-4] => p
)
https://www.php.net/uksort
https://www.php.net/manual/functions.anonymous.php

Echo the keys of a array on a single line ? PHP

How to get an echo of the keys of a array, on a single line ?
[The] => s
[revelation] => b
[that] => z
[the] => d
[Star] => e
[Wars] => h
Result :
<p>The revelation that the Star Wars.</p>
It's pretty simple:
$myArray = [
'The' => 's',
'revelation' => 'b',
'that' => 'z',
'the' => 'd',
'Star' => 'e',
'Wars' => 'h',
];
echo implode(' ', array_keys($myArray));
This basically gets an array of the keys, then implodes them with spaces.

array_diff not always correct

I show my example of diff between values in arrays, sometimes doesn't work corectly.
$fields = array(
'1x1' => 'k',
'1x2' => 'B',
'1x3' => 'c',
'2x1' => 'd',
'2x2' => 'x',
'2x3' => 'Y',
'3x1' => 'b',
'3x2' => 'e',
'3x3' => 'f'
);
print_r($fields);
$answer = array(
'a',
'b',
'c',
'd',
'x',
'y',
'z',
'e',
'f'
);
print_r($answer);
echo '<hr />DIFF:<br />';
print_r(array_diff($fields, $answer));
?>
Results is:
(
[1x1] => k
[1x2] => B
[2x3] => Y
)
But should be:
(
[1x1] => k
[1x2] => B
[2x3] => Y
[3x1] => b
)
Why for PHP b is equal with z?
How to repair this?
This is working correct. According to array_diff() documentation:
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
Another important info from documentation:
Two elements are considered equal if and only if (string) $elem1 ===
(string) $elem2. In words: when the string representation is the same.
So in $answers array there are no k, B, Y elements of $fields array.
The method isn't wrong, compare the 2 lists, they both contain b
see you put 'b' into both the $answer and the $fields array .
so that's why it gives u such output .

How to iterate through array and pass each element as parameter to a constructor in php

I want to loop through an array and get each element to pass as parameters to a constructor.
here is my example
[user_by_province] => Array
(
[label] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
[count] => Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
[4] => 7
[5] => 1
)
)
Here is the constructor:
$pc = new C_PhpChartX(array(array('a','2'),
array('b','1'),
array('c','1'),
array('d','1'),
array('e','7'),
array('f','1')));
so how can i do that with php thanks for any help, may we can do that with array_map or no? thanks for any help
As you suggested array_map with multiple arguments could be a good solution.
<?php
$userByProvince = array(
'label' => array('a', 'b', 'c', 'd', 'e', 'f'),
'count' => array('1', '2', '3', '4', '5', '6'),
);
function combine($arg1, $arg2)
{
return array($arg1, $arg2);
}
$arguments = array_map('combine', $userByProvince['label'], $userByProvince['count']);
$pc = new C_PhpChartX($arguments);
If you are using PHP 5.3 you can even substitute the function with a lambda expression to make the code more compact (see docs).
It's easier to use array_combine.
you can do something like this:
$my_array=array("user_by_province"=>array("label"=>array('a','b','c','d','e','f'),"count"=>array(2,1,1,1,7,1)));
$new_array=array();
for($i=0;$i< count($my_array['user_by_province']['label']);$i++){
$new_values=array();
array_push($new_values,$my_array['user_by_province']['label'][$i],$my_array['user_by_province']['count'][$i]);
array_push($new_array,$new_values);
}
supposed your given array is ordered and has the same amount of data, i.e. pairs:
loop over your data:
$input = array("user_by_province"
=> array("label" => array('a', 'b', 'c'),
"count" => array(1, 2, 3)));
$combined = array();
for($i = 0; $i < count($input['user_by_province']['label'] && $i < count($input['user_by_province']['count']; $i++)
{
$combined[] = array($input['user_by_province']['label'][$i], $input['user_by_province']['count'][$i]);
}
$pc = new C_PhpChartX($combined);

PHP finding the first matching value in 2 arrays

I have two arrays and want to find the first match for either of arrayTwos values in arrayOne.
arrayOne ( [0] = C [1] = A [2] = B [3] = D [4] = B [5] = C)
and
arrayTwo ( [0] = A [1] = B [2] = C )
With these values I would want to return the value "C" as it is the first value in arrayTwo to appear in arrayOne.
I'm thinking I could use for loops and if statements to run through but re there any functions in PHP I could use to simplify this?
Use array_search
$keys = array_search($second_array, $first_array);
Ref : http://in3.php.net/array_search
array_search
$valuekeys = array_search($secondarray, $arrayone);
use array_intersect
$arrayOne = array('C', 'A', 'B', 'D', 'B', 'C');
$arrayTwo = array('A', 'C');
$result = array_intersect($arrayOne , $arrayTwo);
echo $result[0];
Use array_intersect. This will do the job. http://www.php.net/manual/en/function.array-intersect.php Note the difference between using array_intersect($array1, $array2) and array_intersect($array2, $array1)
You can use array_intersect():
$arr1 = array( 0 => 'C', 1 => 'A', 2 => 'B', 3 => 'D', 4 => 'B', 5 => 'C');
$arr2 = array( 0 => 'A', 1 => 'B', '2' => 'C' );
$arr3 = array_intersect($arr1,$arr2);
var_dump($arr3[0]);
string(1) "C"

Categories