How to generate combinations two element of array in PHP [duplicate] - php

This question already has answers here:
PHP - Generate all combinations of items in array
(2 answers)
Closed 3 years ago.
I have an array() contains string F1, F2, F3, F4.........
<?php
$facts= array("F1", "F2", "F3", "F4);
?>
How can I generate combinations two elements of that array.
The output can be like that:
F1.F2
F1.F3
F1.F4
F2.F3
F2.F4
F3.F4
Please help me

Try this:
Solution
$facts= array("F1", "F2", "F3", "F4");
$new_array = array();
foreach($facts as $key => $val){
foreach($facts as $key2 => $val2){
if($key2 <= $key) continue;
$new_array[] = $val . '.' . $val2;
}
}
print_r($new_array);
Output
Array
(
[0] => F1.F2
[1] => F1.F3
[2] => F1.F4
[3] => F2.F3
[4] => F2.F4
[5] => F3.F4
)

Related

Array Echo Together Each Value Together [duplicate]

This question already has answers here:
How can I loop through two arrays at once? [duplicate]
(2 answers)
Closed 6 years ago.
I have 2 Arrays
$name = Array ( [1] => Potrait Color Correction [2] => Extraction )
$number = Array ( [1] => 060716113223-13555 [2] => 49101220160607-25222 )
I'm trying to print the index 1 of first array with index 1 of 2nd array and similary for index 2
This Is My code For Printing (think it's wrong)
foreach ($name as $abc => $val) {
foreach ($number as $xyz => $valu) {
if(!in_array($val, $arr)){
//echo $val." ";echo $valu;
$arr[]=$val;
}
}
}
Problem is my array number is printing only the first value Is Getting Repeated
for both
Potrait Color Correction 060716113223-13555
Extraction 060716113223-13555
im looiking for something like this TO Echo
Potrait Color Correction 060716113223-13555
Extraction 49101220160607-25222
Use for loop to access multiple array:
for($i=0;$i<count($name);$i++) {
echo $name[$i]." ".$number[$i]."<br />";
}
Output:
Potrait Color Correction 060716113223-13555
Extraction 49101220160607-25222
Simply use the index from the first and only foreach to reference the second array like this
$name = Array ( [1] => Potrait Color Correction [2] => Extraction )
$number = Array ( [1] => 060716113223-13555 [2] => 49101220160607-25222 )
The code
$arr = [];
foreach ($name as $idx => $val) {
if(!in_array($val, $arr)){
echo $val . ' ' . $number[$idx] . '<br>';
$arr[]=$val;
}
}
Or if this is a CLI script use PHP_EOF instead of <br>

Get PHP array value [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 9 years ago.
I have an array like the following:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
)
[2] => Array
(
[0] => 4
)
)
Now I want each array value into one single array. How do I do it?
Thanks in advance.
// PHP >= 5.3:
function array_value_recursive($key, array $arr){
$val = array();
array_walk_recursive($arr, function($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
});
return $val;
}
You can recursively parse the array with a function:
$multiDimArr = array(...);
function getSingleArray( $multiDimArr ) {
$singleArray = array();
foreach($multiDimArr as $row) {
if( is_array($row) ) {
getSingleArray($row); // recursive call -> row it cand be also multi dimensional
}
else {
$singleArray[] = $val;
}
}
return $singleArray;
}
I really hope this will help!!

how to replace value of one array to key of another array? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
i have given two array here.
Array-1
(
[0] => 6
[1] => 11
[2] => 13
)
Array-2
(
[0] => 13.339066309
[1] => 0
[2] => 100
)
I want to replace value of one array to key of another array. something like this:
Array
(
[6] => 13.339066309
[11] => 0
[13] => 100
)
Use array_combine:
$new_array = array_combine($array1, $array2);
take a look at array_combine()
$result = array_combine(array_values($firstArr), array_values($secondArr));
Try this:
$result = array();
foreach ($array1 as $key => $value) {
$result[$value] = $array2[$key];
}
Try something like this
$t = array();
$keys = array_keys($arr1);
for ($i = 0; $i < min(count($keys), count($arr2)); $i++) {
$t[$keys[$i]] = $arr2[$i];
}

Create array out of sub arrays with same Key? [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 11 months ago.
I have an array called $friends with names and ids, gotten from the Facebook graph API.
If I print it, it look something like this:
Array (
[0] => Array (
[name] => Tom
[id] => 21)
[1] => Array (
[name] => Bob
[id] => 22)
)
How do I retrieve all Keys (ids) and create a new array like this one?
Array ( [0] => 21 [1] => 22 )
$ids = array_map(function ($friend) { return $friend['id']; }, $friends);
Note, uses PHP 5.3+ anonymous function syntax.
You can use a simple foreach.
$ids = array();
foreach($friends as $friend)
$ids[] = $friend['id'];
foreach ($friends as $key => $value) {
$newFriends[$key] = $value['id'];
}
$arFinal = array();
foreach ($friends as $key => $val){
$arFinal[$key] = $val['id'];
}

How to change array key of this array? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how do you change the key of an array element?
This the array
Array
(
[0] => Array
(
[id] => 1
[due_date] => 2011-09-23
[due_amount] => 10600.00
)
[1] => Array
(
[id] => 2
[due_date] => 2011-10-23
[due_amount] => 10600.00
)
[2] => Array
(
[id] => 3
[due_date] => 2011-11-23
[due_amount] => 10600.00
)
)
how to change id to u_id in this array?
Regards
array_walk_recursive($array, 'changeIDkey');
function changeIDkey($item, &$key)
{
if ($key == 'id') $key = 'u_id';
}
PHP Manual: array_walk_recursive
EDIT
This will not work for the reason #salathe gave in the Comments below. Working on alternative.
ACTUAL ANSWER
function changeIDkey(&$value,$key)
{
if ($value === "id") $value = "u_id";
}
$new = array();
foreach ($array as $key => $value)
{
$keys = array_keys($value);
array_walk($keys,"changeIDkey");
$new[] = array_combine($keys,array_values($value));
}
var_dump($new); //verify
Where $array is your input array. Note that this will only work with your current array structure (two-dimensions, changes keys on only second dimension).
The loop iterates through the inner arrays changing "id" to "u_id" in the keys and then recombines the new keys with the old values.
foreach( $array as &$element ) {
$element['u_id'] = $element['id'];
unset( $element['id'] );
}

Categories