$assoc_arr = array_reduce($arr, function ($result, $item) { $result[$item['text']] = $item['id']; return $result; }, array());
Recommendation: If you are interested in fast PHP for your WordPress site, check out this great PHP/WP benchmark-post from Kinsta, where you can get great managed WordPress hosting with the best support and performance optimizing technical addons like Redis and ElasticSearch.
The snippets story
I just ran into a problem using array_map on associative arrays, like the following one:
$arr = array( array( 'id' => 22, 'text' => 'Lorem' ), array( 'id' => 25, 'text' => 'ipsum' ), );
From this array, I wanted to create another, associative array, with the following structure:
$assoc_arr = array( 'Lorem' => 22, 'ipsum' => 25 );
My first guess was the array_map function, but I had to realize that there is no way to manipulate the keys of the resulting array. After some googling, I discovered, that it is array_reduce you have to use to simulate array_map on associative arrays.
ps: I know, a simple foreach would have done the trick, but once on the path of functional programming, I didn’t want to leave it that fast 🙂
thaks~~it’s very useful~:D
array_flip does the trick
Thanks for sharing. How to get array key and replace with foreach?
As you mentioned “a simple foreach would have done the trick”, what’s the point using array_reduce then ?
It seems It puts complexity where there isn’t any. Do you have another example, where It is unavoidable ?
thanks
Niceee, I really tried using array_map and array_walk unsuccessful.
Been looking for this for a while. Like you say a foreach would suffice, but why make it easy. This is going straight on my (non-competng, private) snippets site. Thanks for spending the time
thx, very much! 🙂
Hey dude, this helped me alot, thanks!
Magnificent!! Thks
actually using array_column is much better.
$assoc_arr = array_column($arr, 'id', 'text') ;
PHP >= 5.5
array_column($arr,'text', 'id')
does the sameFirst of all thank you for sharing this informative blog.. This blog having more useful information that information explanation are step by step and very clear so easy and interesting to read.. After reading this blog i am strong in this topic which helpful to cracking the interview easily..
Nice one. Great information, thanks for sharing your ideas and valuable time, keep rocks.
Thank you very much!
This could help 🙂
22,
‘text’ => ‘Lorem’
),
array(
‘id’ => 25,
‘text’ => ‘ipsum’
),
);
function get_value($val, $key) {
global $master_arr;
$master_arr[$val[‘text’]] = $val[‘id’];
}
array_walk($arr, ‘get_value’);
print_r($master_arr);
?>
$assoc_arr = array_column($arr, ‘id’, ‘text’);
Much better.
// Example of a rough array_map.
$years = [2020, 2019];
$expected_output = [2020 => 2020, 2019 => 2019];
// Foreach way.
$output1 = [];
foreach ($years as $year) {
$output1[$year] = $year;
}
// Array reduce way.
$output2 = array_reduce(
$years,
function ($carry, $item) {
if (!$carry) {
return [$item => $item];
}
return $carry + [$item => $item];
}
);
echo “Input:\n”;
var_dump($years);
echo “Expected:\n”;
var_dump($expected_output);
echo “Foreach: \n”;
var_dump($output1);
echo “Array Reduce: \n”;
var_dump($output2);
echo “\n”;