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';

call_user_func($functions['function1'], 'Hello world!');
Answer from Alex Barrett on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ ref.array.php
PHP: Array Functions - Manual
Ignores value of array, compares only indexes. Example: array_diff_key, array_intersect_key. [suffix] **empty** - no "key" or "assoc" word in suffix. Compares values only. Ignores indexes of array. Example: array_diff, array_intersect. [prefix] u - will do comparison with user defined function.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_ref_array.asp
PHP Array Functions
The array functions are part of the PHP core. There is no installation needed to use these functions.
Discussions

Can you store a function in a PHP array? - Stack Overflow
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: More on stackoverflow.com
๐ŸŒ stackoverflow.com
What kind of array does PHP use? - Stack Overflow
If you're coming from a C++ background it's best to view the PHP array as a generic vector that can store everything. ... Sign up to request clarification or add additional context in comments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Do yourself a favor and learn all of the array functions
For years I have said that one of my superpowers has been that I've read the manual. I've gone through most parts (skipping things like LDAP and Oracle that I had no interest in), but making sure that I know what is available. I might not know how to use things - but looking them up is easy enough. Knowing that something exists is the hardest part of using a library, be in built into a language, or as part of a framework or just a library. More on reddit.com
๐ŸŒ r/PHP
51
66
June 30, 2017
Is there a built-in PHP function that can take any multidimensional array and replace specific values?
Is there a built-in PHP function that can take any multidimensional array and replace specific values? Not that I am aware of, but its pretty easy to write a recursive function to do what you want. https://onlinephp.io/c/b3a1d $value) { if (!is_array($value)) { if ($key == $search) { $parsed[$key] = $replace; } } else { $parsed[$key] = recursive_replace($value, $search, $replace); } } return $parsed; }; $result = recursive_replace($parsed, 'foo', 'baz'); var_dump($result); More on reddit.com
๐ŸŒ r/PHPhelp
9
5
November 10, 2022
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_array_in_array.asp
PHP in_array() Function
Loops While Loop Do While Loop For Loop Foreach Loop Break Statement Continue Statement PHP Functions PHP Arrays ยท Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Functions PHP Superglobals
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-array-functions-complete-reference
PHP Array Functions - GeeksforGeeks
July 12, 2025 - Simplifies Array Tasks: Built-in functions make operations like sorting, filtering, and merging arrays easier. Improves Code Readability: Functions reduce complex code, making it cleaner and more maintainable. Boosts Performance: PHPโ€™s array functions are optimized for faster execution compared to custom code.
๐ŸŒ
Medium
medium.com โ€บ @philippebeck โ€บ mastering-php-array-functions-0038dd2a076d
Mastering PHP Array Functions by Philippe Beck | Medium
December 5, 2024 - The first array uses the array() function, while the second array uses the shorthand square bracket syntax. Arrays in PHP can be modified by adding or removing elements, or by modifying existing elements.
๐ŸŒ
Kalpesh Kathane
kalpeshkathane.wordpress.com โ€บ 2016 โ€บ 09 โ€บ 11 โ€บ php-array-functions
PHP Array Functions โ€“ Kalpesh Kathane
September 4, 2016 - It can take input as argument list and return value. There are thousands of built-in functions in PHP. In PHP, we can define Conditional function, Function within Function and Recursive function also. Advantage of PHP Array Functions Code Reusability: PHP functions are defined only once and ...
Find elsewhere
Top answer
1 of 7
167

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';

call_user_func($functions['function1'], 'Hello world!');
2 of 7
12

Since PHP "5.3.0 Anonymous functions become available", example of usage:

note that this is much faster than using old create_function...

//store anonymous function in an array variable e.g. $a["my_func"]
$a = array(
    "my_func" => function($param = "no parameter"){ 
        echo "In my function. Parameter: ".$param;
    }
);

//check if there is some function or method
if( is_callable( $a["my_func"] ) ) $a["my_func"](); 
    else echo "is not callable";
// OUTPUTS: "In my function. Parameter: no parameter"

echo "\n<br>"; //new line

if( is_callable( $a["my_func"] ) ) $a"my_func"; 
    else echo "is not callable";
// OUTPUTS: "In my function. Parameter: Hi friend!"

echo "\n<br>"; //new line

if( is_callable( $a["somethingElse"] ) ) $a"somethingElse"; 
    else echo "is not callable";
// OUTPUTS: "is not callable",(there is no function/method stored in $a["somethingElse"])

Since PHP 7.4 there are also "Arrow functions"

Arrow functions provide a shorthand syntax for defining functions with implicit by-value scope binding.

It's very similar to the "classic" anonymous function, however it supports only 1 expression and not a block of commands - often used for quick filtering and comparing:

E.g. to get a list of IDs from a book object list:

$extractIds = fn (object o->getId();
$bookIds = array_map($extractIds, $books);

A great use with spaceship (<=>) operator:

$someArray = [2,3,1];
$sort = [
    SORT_ASC => fn(b) => b,
    SORT_DESC => fn(b) => a,
];
usort($someArray, $sort[SORT_ASC]); // someArray will contain: 1, 2, 3
usort($someArray, $sort[SORT_DESC]); // someArray will contain:  3, 2, 1

Constants note: So far (PHP8.2, 2023) PHP doesn't allow storing anonymous functions (nor arrow versions) as constants.

REFERENCES:

  • Anonymous function: https://www.php.net/manual/en/functions.anonymous.php

  • Arrow function: https://www.php.net/manual/en/functions.arrow.php

  • Arrow function (release notes): https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.arrow-functions

  • Test for callable: https://www.php.net/is_callable

๐ŸŒ
Codeguage
codeguage.com โ€บ v1 โ€บ courses โ€บ php โ€บ arrays-array-functions
PHP Array Functions - The Most Frequently Used
You shouldn't need to spend tons and tons of hours in learning from just one course ยท A language used to style webpages in a declarative manner
๐ŸŒ
The Knowledge Academy
theknowledgeacademy.com โ€บ blog โ€บ php-array
What are Arrays in PHP? Everything You Should Know
March 7, 2026 - PHP Array is a data structure that stores multiple values of any type, accessed using integer indexes or string keys in associative arrays. Arrays can hold mixed data types, support dynamic resizing, and are used for storing lists, maps, or ...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays.asp
PHP Arrays
In PHP, an array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or a name. An array stores multiple values in one single variable: $cars = array("Volvo", "BMW", ...
๐ŸŒ
PHPTPOINT
phpwebdevelop.weebly.com โ€บ blog โ€บ php-array-functions
PHP Array Functions - PHPTPOINT - Web Developing - Designing and Programming Training
February 5, 2020 - โ€‹ PHP Array is a type of data structure used to store several elements of similar types of data in a single variable. They are useful for creating a list of similar types of elements that can be...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php-tutorial โ€บ php arrays
PHP Arrays - Scaler Topics
April 13, 2024 - There are various methods to create arrays in php we will now read about each of them with proper explanation. ... In the above code, we first declare an array using the array() function.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_array_functions.htm
PHP Array Functions
PHP supports simple and multi-dimensional arrays and can be either user created or created by another function. You can use these functions to modify, sort, add, remove or search elements in an array.
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 13.x โ€บ blade
Blade Templates | Laravel 13.x - The clean stack for Artisans and agents
The directive accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list: ...
๐ŸŒ
Codewithjayesh
codewithjayesh.com โ€บ home โ€บ php โ€บ php array functions made easy for beginners
PHP Array Functions Made Easy for Beginners
June 15, 2025 - This function removes the last element of an array and returns it. Itโ€™s useful when youโ€™re working with a stack-like structure (LIFO โ€“ Last In, First Out). ... After using array_pop(), the $fruits array will now contain apple and banana.