According php.net , array_map php returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()
But, in my opinion, is a function array_map in php, which is useful for the efficiency of our programming structure becomes shorter and effective, particularly in the use of arrays
Structure using array_map
array array_map ( callback $callback , array $array1 [, array $… ] )
callback : Callback function to run for each element in each array.
array1 : An array to run through the callback function.
as an example using array_map
1 2 3 4 5 6 7 8 9 10 | <?php function bot($x) { return($x * $x); } $a = array(1, 2, 3, 4, 5); $b = array_map("bot", $a); print_r($b); ?> |
Table of Contents
Implementation array_map() php frequently used are as follows
Input Form Filtering using array_map.
With array_map() we can filtering form input.
To clean form, we do stripslashes first like follows:
1 2 3 | $name = stripslashes($_POST['name']); $address = stripslashes($_POST['address']); $phone = stripslashes($_POST['$phone']); |
With array_map we can simplify as follows :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <form method="post"> <input type="text" name="text_1" /> <input type="text" name="text_2" /> <input type="text" name="text_3" /> <input type="submit" name="submit" value="submit" /> </form> <?php if(isset($_POST['submit'])) { $_POST = array_map('stripslashes', $_POST); echo $_POST['text_1']; echo $_POST['text_2']; echo $_POST['text_3']; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <form method="post"> <input type="text" name="text_1" /> <input type="text" name="text_2" /> <input type="text" name="text_3" /> <input type="submit" name="submit" value="submit" /> </form> <?php if(isset($_POST['submit'])) { function check($a) { if(empty($a)) { return 'empty'; }else { return $a; } } $_POST = array_map('check', $_POST); echo $_POST['text_1'].'<br/>'; echo $_POST['text_2'].'<br/>'; echo $_POST['text_3'].'<br/>'; } ?> |
Eliminate duplicate arrays in multidimensional array with php array_map.
1 2 3 4 5 6 7 8 9 | $data = array( array('apple', 'pumpkin', 'orange', 'juice'), array('Donald', 'Donald', 'Daisy', 'Mike'), array('one', 'two', 'two', 'four'), ); // we can eliminate duplicate arrays with array_map $data = array_map('array_unique', $data); print_r( $data); |
Resetting the multidimensional array with array_map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $product = array( array('B001E90','B001350','B0554OY','B0321OP'), array('Iphone 6', 'Samsung galaxy note', 'Xiomi redmi', 'Blackberry'), array(6000000, 5000000, 3000000, 2000000), ); $transaction = array_map(null, $product[0],$product[1],$product[2] ); $transaction = array_map(function($array){ return array( 'ASIN' => $array[0], 'Title' => $array[1], 'Price' => $array[2]); },$transaction); print_r($tmpTransaksi); ?> |
Thus the use of array_map on php, still a lot of php functions are very useful, I will discuss in other occasions
Leave a Reply