Status/Resolution/Reason: Closed/Withdrawn/Duplicate
Reporter/Name(from Bugbase): Adam Cameron / Adam Cameron (Adam Cameron)
Created: 09/29/2013
Components: Language
Versions: 10.0
Failure Type: Enhancement Request
Found In Build/Fixed In Build: Final /
Priority/Frequency: Trivial / Unknown
Locale/System: English / Platforms All
Vote Count: 0
Duplicate ID: CF-3595198
For observations see:
http://cfmlblog.adamcameron.me/2013/09/arrayeach-could-stand-some-improvement.html
Summary (extracted from the above article... it's better to read it in the above context):
JavaScript also provides the increasingly-industry-standard for altering collections in-place; map().[...]
rainbow = ["Whero","Karaka","Kowhai","Kakariki","Kikorangi","Tawatawa","Mawhero"];
RAINBOW = rainbow.map(
function(v,i,a){
return v.toUpperCase();
}
);
console.log(RAINBOW);
This outputs the same-ole.
Note that the callback takes the same three arguments: value, index and array, but this time returns a value. This value is then used to populate a new array sequentially.
One thing to note here is that JavaScript has the gumption to understand that arrays can be sparse - have index positions with no elements in them - and the callback is not called in that situation:
[...]
PHP has an array_map() function, thus:
$newRainbow = array_map(
function($v) {
return strtoupper($v);
},
$rainbow
);
[...]
array_map() also takes additional array arguments, and the callback will receive an element from each:
<?php
$maoriNumbers = ["Tahi","Rua","Toru","Wha","Rima","Ono","Whitu","Waru","Iwa","Tekau"];
$russianNumbers = ["????","???","???","??????","????","?????","????","??????","??????","??????"];
$digits = [1,2,3,4,5,6,7,8,9,10];
function translation($ma, $i, $ru){
return "$ma ($i) in Russian is $ru<br>";
}
$translation = array_map("translation", $maoriNumbers, $digits, $russianNumbers);
foreach($translation as $number) {
echo $number;
}
?>
And this results in:
Tahi (1) in Russian is ????
Rua (2) in Russian is ???
Toru (3) in Russian is ???
Wha (4) in Russian is ??????
Rima (5) in Russian is ????
Ono (6) in Russian is ?????
Whitu (7) in Russian is ????
Waru (8) in Russian is ??????
Iwa (9) in Russian is ??????
Tekau (10) in Russian is ??????
I think that's quite a good feature, actually!
Bottom line:
Implement this like JavaScript has, except also allow for the PHP idea of being able to map multiple arrays together in one hit: that'd be cool.
--
Adam
----------------------------- Additional Watson Details -----------------------------
Watson Bug ID: 3641351
External Customer Info:
External Company:
External Customer Name: Adam Cameron.
External Customer Email:
External Test Config: My Hardware and Environment details:
Attachments:
Comments: