I have an array:
$input = array(1,2,3,4,6,5,3,6)
and I want the key/value pairs to be flipped.
This can be done by using the array_flip() function.
$flipped = array_flip($input)
If in the original array has 2 or more same values (in this case number 6)how can I return it in an array?
array= ([1]=0,[2]=>1,[4]=>2,[6]=>array(3,6),[5]=>4,[3]=>5)
I tried to use array_count_values() but can't figure out how to do it?
You cannot do that using the array_flip() function. Probably you look for something like that:
<?php
function array_flip_and_collect($input) {
$output = [];
foreach ($input as $key=>$val) {
$output[$val][] = $key;
}
return $output;
}
$input = array(1,2,3,4,6,5,3,6);
print_r(array_flip_and_collect($input));
The output:
Array
(
[1] => Array
(
[0] => 0
)
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 2
[1] => 6
)
[4] => Array
(
[0] => 3
)
[6] => Array
(
[0] => 4
[1] => 7
)
[5] => Array
(
[0] => 5
)
)
Note that the output differs slightly from what you suggested in your question. That is by purpose because this appears more logical to me. If you really want that keys with only one element really are scalars and not arrays with one element, then you have to add an additional conversion step to the code.
Related
How can I do the below change in PHP?
Input:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479,14485,14486,14487
)
Output should be like this:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
A possible solution:
$input = array('14477,14478,14479,14485,14486,14487');
$output = array_map(
function (array $a){
return implode(',', $a);
},
array_chunk(
explode(',', $input[0]),
3
)
);
Read it from inside out:
explode() splits the string $input[0] using comma (,) as delimiter and returns an array;
array_chunk() splits the array into chunks of size 3; it returns an array of arrays, each inner array contains 3 elements (apart from the last one that can contain less);
array_map() applies the function it receives as its first argument to each value of the array it gets as its second argument (the array of arrays returned by array_chunk()); it returns an array whose values are the values returned by the function;
the anonymous function passed to array_map() gets an array (of size 3 or less) and uses implode() to join its elements into a string, using comma (,) to separate the values and returns the string;
array_map() puts together all the values returned by the anonymous function (one for each chunk of 3 elements of the array) into a new array it returns.
The output (print_r($output)) looks like this:
Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
try this as a boilerplate
function chunker($arr, $l) {
return array_chunk($arr, $l);
}
print_r(chunker($hap, 3));
/*
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
*/
UPDATE
php > $h = [ "14477,14478,14479,14485,14486,14487" ];
php > $hap = explode(",", $h[0]);
php > print_r($hap);
Array
(
[0] => 14477
[1] => 14478
[2] => 14479
[3] => 14485
[4] => 14486
[5] => 14487
)
php > print_r(chunker($hap, 3));
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
php >
For example i have an array named $slice like this :
Array
(
[0] => Array
(
[0] => 12
[1] => 4
[2] => 2
[3] => 8
[4] => 20
)
[1] => Array
(
[0] => 9
[1] => 7
[2] => 1
[3] => 10
[4] => 23
)
)
I want to sort array above so the output will be like this :
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 8
[3] => 12
[4] => 20
)
[1] => Array
(
[0] => 1
[1] => 7
[2] => 9
[3] => 10
[4] => 23
)
)
Then i tried to use foreach and array_multisort, and when i use print_r the result is 1 for each col :
foreach ($slice1 as $col) {
$slicesort[] = array_multisort($col);
}
output :
Array
(
[0] => 1
[1] => 1
)
array_multisort sorts the array in place, it does not return the sorted array. You need to use it like this:
foreach ($slice1 as $col) {
array_multisort($col);
$slicesort[] = $col;
}
Having said this, array_multisort is somewhat overkill here, and I'm not sure that you really need to create a copy of the array. This will do just fine:
foreach ($slice1 as &$col) {
sort($col);
}
This applies sort to each array within $slice1 by reference, and thereby orders $slice1 in place.
PHP array_multisort, as per the documentation, is for sorting multiple or multi-dimensional arrays, in your case you don't really need it.
In your case you just need sort, you can find the documentation here
$slicesort = array();
foreach ($slice1 as $col) {
sort($col);
$slicesort[] = $col;
}
$slice = array(
array(12,4,8,2,10),
array(9,7,1,10,13)
);
foreach ($slice as &$arr) {
sort($arr);
}
print_r($slice);
array_multisort return a boolean value, true for success, false otherwise.
Change your code this way:
foreach ($slice1 as $col) {
if (array_multisort($col)) {
$slicesort[] = $col;
}
}
I would like to the most effective way to get the position of repeated elements element in array. For example, if i have:
$example = array('a','b','c','a','a','d');
So in this case 'a' is at position 0 - 3 - 4.
I know we can loop through but i think in it is ineffective. I would be grateful if you could show me a better way to do this.
Thank you very much
array_keys() with the optional search_value parameter
$example = array('a','b','c','a','a','d');
print_r(array_keys($example, "a"));
output:
Array ( [0] => 0 [1] => 3 [2] => 4 )
If you don't have a particular vlaue in mind and want the positions of all the values:
$example = array('a','b','c','a','a','d');
$out=array();
foreach($example as $k=>$v){
$out[$v][]=$k;
}
echo '<pre>';
print_r($out);
returns:
Array
(
[a] => Array
(
[0] => 0
[1] => 3
[2] => 4
)
[b] => Array
(
[0] => 1
)
[c] => Array
(
[0] => 2
)
[d] => Array
(
[0] => 5
)
)
Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);
This question already has answers here:
PHP Change Array Keys
(12 answers)
Closed 8 years ago.
I have an array of arrays, something like this:
Array
(
[0] => Array
(
[0] => DC1F180E-FE57-622C-28AE-8194843B4D84
[1] => First Choice
[2] => 1
)
[1] => Array
(
[0] => EB877F3C-7A3B-98A7-9240-580FB797030A
[1] => Second Choice
[2] => 0
)
[2] => Array
(
[0] => D3C0EA56-73D2-C7E3-8236-EEA2400DFA9C
[1] => Third Choice
[2] => 0
)
)
How do I can rename all the keys in "nested" arrays to get something like this in all "nested" arrays:
...
[1] => Array
(
[id] => EB877F3C-7A3B-98A7-9240-580FB797030A
[name] => Second Choice
[status] => 0
)
...
This way you can change keys in your array:
for ($i=0, $c = count($array); $i<$c; ++$i) {
$array[$i]['id'] = $array[$i][0];
$array[$i]['name'] = $array[$i][1];
$array[$i]['status'] = $array[$i][2];
unset($array[$i][0];
unset($array[$i][1];
unset($array[$i][2];
}
You have to use syntax $array[$key1][$key2] to use multidimensional array.
You could use array_walk and array_combine like this:
$a = array(array('foo','bar'),array('foo','bar'));
print_r($a);
$keys = array('first','second');
$new_array = array();
array_walk($a,function($x) use (&$new_array,$keys) {
$new_array[] = array_combine($keys,$x);
});
print_r($new_array);
array_walk goes through each element of your array, applying a callback function. array_combine combines an array of keys with an array of values to produce a new array.
output:
Array ( [0] => Array ( [0] => foo [1] => bar )
[1] => Array ( [0] => foo [1] => bar ) )
Array ( [0] => Array ( [first] => foo [second] => bar )
[1] => Array ( [first] => foo [second] => bar ) )