Generating an efficient permutation without the single result [duplicate] - php

This question already has answers here:
Get all permutations of a PHP array?
(7 answers)
Closed 6 months ago.
Is there any way I could pull something like this when I call the function:
perm(array("one", "two", "tree"));
that would result to:
Array ( [0] => one [1] => two )
Array ( [0] => one [1] => tree )
Array ( [0] => two [1] => one )
Array ( [0] => two [1] => tree )
Array ( [0] => tree [1] => one )
Array ( [0] => tree [1] => two )
Array ( [0] => one [1] => two [2] => tree )
Array ( [0] => two [1] => one [2] => tree )
Array ( [0] => one [1] => tree [2] => two )
Array ( [0] => tree [1] => one [2] => two )
Array ( [0] => two [1] => tree [2] => one )
Array ( [0] => tree [1] => two [2] => one )
The problem is the array is dynamic and the value could become something like perm(array("one", "two", "tree", "five","8"));
I wanted to generate all the permutation even if the it becomes.
perm(array("one", "two")); or perm(array("two", "tree", "five","8"));
The problem of what I have below is that it starts from 1 permutation instead of 2 permutation above.
</php
function perm($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
perm($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
?>

This may not have the same order but this is the same result you are expecting. You just have to add another if condition. I am assuming someone smarter than me here could recreate your function to make it more efficient with lower benchmarks. Otherwise you would have to go for this:
$collect = array();
function permute($arr, $temp_string, &$collect){
for($i=0, $iMax = sizeof($arr); $i < $iMax; $i++){
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
$jval = $temp_string." ".$elem[0];
if(!empty($temp_string)){
$collect [] = explode(" ",$jval);
}
if(sizeof($arrcopy) > 0){
permute($arrcopy, $jval, $collect);
}
}
}
permute(array('one', 'two', 'tree'), "", $collect);
Would result to:
Array ( [0] => [1] => one [2] => two )
Array ( [0] => [1] => one [2] => two [3] => tree )
Array ( [0] => [1] => one [2] => tree )
Array ( [0] => [1] => one [2] => tree [3] => two )
Array ( [0] => [1] => two [2] => one )
Array ( [0] => [1] => two [2] => one [3] => tree )
Array ( [0] => [1] => two [2] => tree )
Array ( [0] => [1] => two [2] => tree [3] => one )
Array ( [0] => [1] => tree [2] => one )
Array ( [0] => [1] => tree [2] => one [3] => two )
Array ( [0] => [1] => tree [2] => two )
Array ( [0] => [1] => tree [2] => two [3] => one )

Related

How to merge two associative arrays

I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);

Split array base on result of checking element

I have an array in PHP as below:
`
Array (
[0] => Array
(
[0] => Some text
[1] => 6230.C3
)
[1] => Array
(
[0] => Some text
[1] => 6230.C3
)
[2] => Array
(
[0] =>
[1] =>
)
[3] => Array
(
[0] => Some text
[1] =>
)
[4] => Array
(
[0] => Some text
[1] =>
)
[5] => Array
(
[0] => Some text
[1] =>
)
[6] => Array
(
[0] =>
[1] =>
)
[7] => Array
(
[0] => Some text
[1] =>
)
[8] => Array
(
[0] => Some text
[1] =>
)
)
`
Array[2] and Array[6] are empty arrays. Please help me split my array to three arrays array1, array2, array3 as below (split at position of Array[2] and Array[6]).
array1 = [ Array[0], Array[1] ]
array2 = [ Array[3], Array[4], Array[5] ]
Aarray3 = [ Array[7], Array[8] ]
Thank you for your help.
This is a little more flexible than your original question because it is not limited to exactly 3 arrays as separate variables. The result of the following would be an multidimensional array, split in the way that you want but for an unlimited number of splits. I can rework it if necessary to fit exactly 3 variables if that is really what your input will always be.
<?php
$final = array();
$counter = 0;
$outerEmpty = false;
foreach($originalArray as $outer) {
$innerEmpty = true;
foreach($outer as $inner) {
if (!empty($inner)) {
$innerEmpty = false;
break;
}
}
if (!$innerEmpty)
$final[$counter][] = $outer;
else
$counter++;
}

Split an array into a MD array every nth line

bit of a question here. Ive got an array that contains data which has been parsed from a website using all our favourite php functions such as array_map.
The array is current 3 sub arrays deep.
Here is the code I am using:
for ($tcid = 1; $tcid <= count($categories); $tcid++) {
$catHeader[$tcid] = $categories[$tcid][0];
$event[$i]['tickets'] = $categories;
unset($categories[$tcid][0]);
$categories[$tcid] = array_map('trim', $categories[$tcid]);
$categories[$tcid] = array_values($categories[$tcid]);
$ab = 0;
for ($b = 0; $b <= count($categories[$tcid]); $b++) {
if ($categories[$tcid][$b] == "" || !$categories[$tcid][$b] || $categories[$tcid][$b] == null) {
unset($categories[$tcid][$b]);
}
}
}
and the array looks something like....
[1] => Array (
[data] => Array ( ...
)
[tickets] => Array (
[1] => Array (
[0] => xxx
[1] => etc
[3] => etc2
)
[2] => (
[0] => Std1
[1] => 10 / 10
[2] => £20.00
[3] => £200.00
[4] => Std2
[5] => 100 / 100
[6] => £13.00
[7] => £1,300.00
[8] => Std3
[9] => 10 / 320
[10] => £15.00
[11] => £150.00
)
)
)
My question to you today, is how on earth do I split the array every 4 \n's or array keys as they're known and explode each 4 into a further sub array?
So that Std1, Std2, Std3 will be their own sub array with its associated data of the 2nd key of tickets, but also doing this for every sub array of tickets that has more than 1 set of data (a set of data being 4 array keys).
I've tried all sorts but can't get it to work.
See below of how I want it to look.
[1] => Array (
[data] => Array ( ...
)
[tickets] => Array (
[1] => Array (
[0] => xxx
[1] => etc
[3] => etc2
)
[2] => (
[0] => Array (
[0] => Std1
[1] => 10 / 10
[2] => £20.00
[3] => £200.00
)
[1] => Array (
[0] => Std2
[1] => 100 / 100
[2] => £13.00
[3] => £1,300.00
)
[2] => Array (
[0] => Std3
[1] => 10 / 320
[2] => £15.00
[3] => £150.00
)
)
)
)
Thanks
As noted in the comments, you'd be best off handling your array by-reference to modify it's original contents somewhere within your loops
Provided the array groupings you want to chunk are in groups of 4, you could array_chunk() it:
$array['tickets'][2] = array_chunk($array['tickets'][2], 4);

Merging one-dimensional arrays

I want to merge those three arrays:
array (
[0] => zooey,
[1] => Robert,
[2] => james,
[3] => Alfred,
);
array (
[0] => city1,
[1] => city2,
[2] => city3,
[3] => city4,
);
array (
[1] => city1,
[2] => city2,
[3] => city3,
[4] => city4,
);
into this:
array (
[0] => array('id'=>user_id1,'name'=>username1, 'city'=>user city1),
[1] => array('id'=>user_id2,'name'=>username2, 'city'=>user city2),
// and so on
);
You need to write your own merge function ,if all 3 arrays are same size with correct positioning of relevant elements, the function would look like this
function combineUserArray($arrayNames, $arrayCities, $arrayIds){
$users = [];
for($i = 0; $i < count($arrayIds); $i++){
$users[$i] = [
'id'=>$arrayIds[$i],
'name'=>$arrayNames[$i],
'city'=>$arrayCities[$i],
];
}
return $users;
}

PHP: Combining 2 Array into 1 array with a common index

username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}

Categories