Function as array value
You can "reference" any function. A function reference is not a reference in the sense of "address in memory" or something. It's merely the name of the function.
<?php
$functions = array(
'regular' => 'strlen',
'class_function' => array('ClassName', 'functionName'),
'object_method' => array($object, 'methodName'),
'closure' => function($foo) {
return $foo;
},
);
// while this works
$functions['regular']();
// this doesn't
$functions['class_function']();
// to make this work across the board, you'll need either
call_user_func($functions['object_method'], $arg1, $arg2, $arg3);
// or
call_user_func_array($functions['object_method'], array($arg1, $arg2, $arg3));
Javascript Array of Functions
var array_of_functions = [
first_function,
second_function,
third_function,
forth_function
]
and then when you want to execute a given function in the array:array_of_functions[0]('a string');
Write a function that takes an array by calling another function to return a value and store it on a new array
You can simply push callback return into array and return that same array from your custom fucntion.
You can also modify original array using index.
const customMap = (arr, callback) => {
const finalArr = []
arr.forEach((item, index) => {
const res = callback(item)
finalArr.push(res)
arr[index] = res // This will modify original array
})
return finalArr
}
const numbers = [1, 2, 3, 4, 5];
const newNumbers = customMap(numbers, (number) => {
return number + 3;
});
console.log(newNumbers);
console.log(numbers);
Calling functions as dynamic function as array values in javascript
Use function names rather than using strings
function getId(arr) { for (let i in arr) { console.log("Function getId " + arr[i]); }}
function getMarker(arr) { for (let i in arr) { console.log("Function getMarker " + arr[i]); }}const PAGEAPP = { processes: [getId, getMarker], init: () => { for (let foo of PAGEAPP.processes) { foo([1, 2, 3]);
} },}
PAGEAPP.init()
Return array in a function
In this case, your array variable arr
can actually also be treated as a pointer to the beginning of your array's block in memory, by an implicit conversion. This syntax that you're using:
int fillarr(int arr[])
Is kind of just syntactic sugar. You could really replace it with this and it would still work:int fillarr(int* arr)
So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:int* fillarr(int arr[])
And you'll still be able to use it just like you would a normal array:int main()
{
int y[10];
int *a = fillarr(y);
cout << a[0] << endl;
}
Can you store a function in a PHP array?
The recommended way to do this is with an anonymous function:
$functions = [
'function1' => function ($echo) {
echo $echo;
}
];
If you want to store a function that has already been declared then you can simply refer to it by name as a string:function do_echo($echo) {
echo $echo;
}
$functions = [
'function1' => 'do_echo'
];
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function
(deprecated since PHP 7.2):$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);
All of these methods are listed in the documentation under the callable
pseudo-type.Whichever you choose, the function can either be called directly (PHP ≥5.4) or with call_user_func
/call_user_func_array
:
$functions['function1']('Hello world!');
call_user_func($functions['function1'], 'Hello world!');
Related Topics
Setting Up PHPmailer with Office365 Smtp
How to Implement Ws-Security 1.1 in PHP5
How to Use Session Variables in Wordpress
Uploading Image from Android to PHP Server
How to Add Custom Fields to Woocommerce Registration Form
Best Way to Escape and Create a Slug
.Htaccess Deny Access to Specific Files? More Than One
Php, How to Get Current Date in Certain Format
PHP Date Function Seven Days Previous
Encoding Byte Data into Digits
Regular Expression for French Characters
Phpstorm: How to Add Method Stubs from a Pecl Library That PHPstorm Doesn't Currently Support
How to Force Ssl in Codeigniter