How To Find All Combinations From An Array In PHP [duplicate] - php

This question already has answers here:
PHP: How to get all possible combinations of 1D array? [duplicate]
(1 answer)
How To Find Words Combination In Php
(1 answer)
Closed 6 years ago.
I want to get all the possible combination from an array. I know this question has been asked several times but didnot worked for me correctly .
The array input should be single dimensional array as well as the output
for example:- input
$new_array=array('c','a','t','m','p');
possible combinations such as ca,ct,cm,cp,cat,cam,cap.... and soo on...
I have tried this but didnot worked correctly .
I have taken the code from PHP CookBook
function pc_array_power_set($array) {
// initialize by adding the empty set
$results = array();
foreach ($array as $element)
foreach ($results as $combination)
array_push($results, array_merge($eleme, $combination));
return $results;
}
$set = array('c', 'a', 't','m','p');
$power_set = pc_array_power_set($set);
print_r($power_set);
I need the output in single dimensional array

Related

Filter array by skipping every 2nd and 3rd element from array [duplicate]

This question already has answers here:
Selecting every nth item from an array
(9 answers)
Remove every 3rd and 4th element from array in php
(1 answer)
How to access N-th element of an array in PHP
(3 answers)
Remove every nth item from array
(6 answers)
foreach step 5 steps forward in an array
(3 answers)
Closed 4 months ago.
i have a array like this
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
what i wan to remove
$remove = "b,c,e,f,h,i,k,l";
then i need a new array from the remaining elements like below
$new_arr = [a,d,g,j,m];
Use array chunk to split by 3 and take out first element.
<?php
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
$chunked =array_chunk($names, 3);
$filtered = [];
foreach($chunked as $chunk){
$filtered[] = $chunk[0];
}
var_dump($filtered);
?>
Instead of removing number 2 and number 3 from each 3 elements, the task is simply written like this:
Keep the first of every 3 items.
This can be determined using the index and the modulo operator %.
Using array_filter saves the foreach loop. This allows the solution to be implemented as a one-liner.
$names = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];
$result = array_filter($names,fn($k) => $k%3 == 0, ARRAY_FILTER_USE_KEY );
var_dump($result);
Demo: https://3v4l.org/sKTSQ
The $names array needs to be noted as in the code above. A notation without quotes like in the question produces error messages.

How do I extracting the multidimensional array value in php? [duplicate]

This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}

How to add an array together without using array_sum [duplicate]

This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 3 years ago.
I'm tryng to learn PHP and I'm trying to do it by doing things the long way round.
So I came across array_sum() and I was wondering if there is a way to calculate an array without using it.
for example
$my_array = array(10, 80, 30);
I've only got as far as this and I'm stumped. I have looked on google but I haven't found anything.
$implode = implode(",", $my_array );
$explode = explode(",", $implode);
foreach($explode as $test)
{
}
I will always go for native functions as they are optimized as well as tested.[so basically i want to say that use array_sum()],
Apart from above, below is foreach() process to sum all values of a single-dimensional array[just for knowledge sake]
<?php
$result = 0; //create a variable with 0 value
foreach($explode as $test)
{
$result += $test; //add array value to the newly created value
}
echo $result; //print final sum of all array values
Output:-https://3v4l.org/0qMJu
Note:- please read #Markus Zeller comment under question

PHP - Two JSON lists in one (two-dimensional) [duplicate]

This question already has answers here:
How to create key value pair using two arrays in JavaScript?
(7 answers)
Closed 5 years ago.
I'm trying using PHP to bring the two JSON lists into one, as follows:
list1
[1,2,3,4,5]
list2
["a","b","c","d","e"]
How to join lists to get:
[[1,"a"],[2,"b"],[3,"c"],[4,"d"],[5,"e"]]
How to link two lists to one, as in a given example?
You need to loop through the arrays, and create a new one with the combined values:
<?php
$arr1 = [1,2,3,4,5];
$arr2 = ["a","b","c","d","e"];
$result = [];
foreach ($arr1 as $i => $a) {
$result[] = [$a, $arr2[$i] ?? ''];
}
?>

Grab value from complex array and output as simple associative array [duplicate]

This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');

Categories