Array_Map Function in PHP with Parameter

Array_map function in php with parameter

Apart from creating a mapper object, there isn't much you can do. For example:

class customMapper {
private $customMap = NULL;
public function __construct($customMap){
$this->customMap = $customMap;
}
public function map($data){
return $data[$this->customMap];
}
}

And then inside your function, instead of creating your own mapper, use the new class:

$ids = array_map(array(new customMapper('param2'), 'map'), $data['student_teacher']);

This will allow you to create a custom mapper that can return any kind of information... And you can complexify your customMapper to accept more fields or configuration easily.

array_map and pass 2 arguments to the mapped function - array_map(): Argument #3 should be an array

Please always read docs:

http://php.net/manual/en/function.array-map.php

array array_map ( callable $callback , array $array1 [, array $... ] )

and yet you pass bool $format as argument

"How do you pass 2 or more arguments when you call array_map function
within a class?

I would create anonymous function with use() syntax

public function transformCollection(array $items, $format)
{
return array_map(function($item) use ($format) {
return $this->transform($item, $format);
}, $items);
}

How to pass standard php functions to array_map and array_filter?

You need to pass the function (user-defined OR builtin) name as a string in the parameter of array_map function.

Do the following:

$result = array_map('boolval', $elements);

How to use array_map with a function that takes more than one parameter?

You can try like this

<?php
function replace($array)
{
$replace_res = str_replace('_', ' ', $array);
return $replace_res;
}

$array = array("12_3","a_bc");
$result = array_map("replace", $array);
print_r($result);
?>

How do I pass 2 arguments to an array_map function in php?

Use a callback function instead

function array_map_callback($a)
{
global $con;
return mysqli_real_escape_string($con, $a);
}

array_map('array_map_callback', $_SESSION['question_attempt']);

where $con is the connection variable.

So your $sql variable would be:

$sql="select * from question_test where test_id='".$test_id."' and  difficulty_level BETWEEN  ".$_SESSION['difficulty_start']." and  ".$_SESSION['difficulty_end']." and question_id NOT IN ('".implode("', '", array_map('array_map_callback', $_SESSION['question_attempt']))."')  order by rand()*favourability_level  desc";

or you can go with array_walk

array_walk($_SESSION['question_attempt'], function(&$string) use ($con) { 
$string = mysqli_real_escape_string($con, $string);
});

How can I use parameters/arguments for my callback function in array_map()?

If you need to attach additional parameters to a function that requires a callable the easiest way it to just pass in a wrapper function with your parameters pre-defined

$array = array_map(function($d) {
return str_getcsv($d, "\t");
}, file("file.csv"));

Alternatively you can pass in parameters using the use() syntax with the closure.

$delimiter = "|";
$array = array_map(function($d) use ($delimiter) {
return str_getcsv($d, $delimiter);
}, file("file.csv"));

Another fun thing that can be done with this technique is create a function that returns functions with predefined values built in.

function getDelimitedStringParser($delimiter, $enclosure, $escapeChar){
return function ($str) use ($delimiter, $enclosure, $escapeChar) {
return str_getcsv($str, $delimiter, $enclosure, $escapeChar);
};
}

$fileData = array_map("trim", file("myfile.csv"));
$csv = array_map(getDelimitedStringParser(",", '"', "\\"), $fileData);

Passing two arguments to the function inside of array_map

You can set delimiter in array_map but:

<?php

$string = [
"this;is;a;string;aaa",
"this2;is;a;string;aaa",
"this3;is;a;string;aaa",
"this4;is;a;string;aaa",
];

$output = array_map('str_getcsv', $string, [";", ";", ";", ";", ]);

in [";"] You must put as many delimiters as rows in input array like in eg.

Other parameters from str_getcsv can be done same way.



Related Topics



Leave a reply



Submit