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
extract — Import variables into the current symbol table from an array ... A simple trick that can help you to guess what diff/intersect or sort function does by name. [suffix] assoc - additional index check. Compares both value and index. Example: array_diff_assoc, array_intersect_assoc.
🌐
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.
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) => $o->getId();
$bookIds = array_map($extractIds, $books);

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

$someArray = [2,3,1];
$sort = [
    SORT_ASC => fn($a, $b) => $a <=> $b,
    SORT_DESC => fn($a, $b) => $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

🌐
Reddit
reddit.com › r/php › php arrays and array functions for beginners
r/PHP on Reddit: PHP Arrays and Array Functions for Beginners
April 14, 2019 - array() is not a function, but a language construct. ... You are absolutely correct. I'll change it soon. Thank you. ... I'd really recommend describing the difference between php arrays and c arrays, anyone who learns what an array is in computer science courses is going to be confused when they get to php arrays in my opinion.
🌐
Wordpress
vpmpce.wordpress.com › wp-content › uploads › 2018 › 07 › 3350702_unit-3-working-with-php-arrays-and-functions.pdf pdf
WORKING WITH PHP ARRAYS AND FUNCTIONS Unit - III 1
In PHP, the array() function is used to create an · array: array( ); In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index · Associative arrays - Arrays with named keys · Multidimensional arrays - Arrays containing one ·
Find elsewhere
🌐
Medium
medium.com › @philippebeck › mastering-php-array-functions-0038dd2a076d
Mastering PHP Array Functions by Philippe Beck | Medium
December 5, 2024 - PHP provides functions for adding elements to the end of an array (array_push()) & removing elements from the end of an array (array_pop()), offering convenient ways to manipulate array contents.
🌐
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", ...
🌐
Medium
medium.com › @rampukar › php-array-functions-1a72e13d1c3c
PHP Array Functions. In PHP, an array is a powerful data… | by Ram Pukar | Medium
May 13, 2025 - array() - creates a new array. count() - returns the number of elements in an array. in_array() - checks if a value exists in an array. array_push() - adds one or more elements to the end of an array. array_pop() - removes and returns the last ...
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › php-array
PHP Array Explained: Types, Syntax, and Functions
January 1, 2009 - The array_merge() function combines both arrays. Since both arrays have the key "b", the value from $array2 ("blueberry") overwrites the value from $array1. The merged array contains all keys, with duplicates replaced by the latest value. Level up your skills with Swift Training today! When working with PHP Arrays, follow these best practices:
🌐
Tutorialspoint
tutorialspoint.com › php › php_array_functions.htm
PHP Array Functions
You can use these functions to modify, sort, add, remove or search elements in an array. There is no installation needed to use PHP array functions; they are part of the PHP core and comes along with standard PHP installation.
🌐
Medium
medium.com › @arifhossen.dev › php-8-4s-new-array-functions-a-beginner-s-complete-guide-2025-50fda9719548
PHP 8.4’s New Array Functions: A Beginner’s Complete Guide (2025) | by Arif Hossen | Medium
January 5, 2025 - Whether a novice programmer or an experienced developer, these new functions will streamline your array operations. ... PHP 8.4 introduces four powerful array functions that simplify common array operations.
🌐
Eternal Web Pvt Ltd
eternalsoftsolutions.com › home › the 17 most useful php array functions you need to know!
The 17 Most Useful PHP Array Functions You Need To Know!
April 11, 2026 - This Blog outlines some of the more useful functions in the PHP array toolkit, and features detailed explanations and usage examples.
🌐
The Man in the Arena
carlalexander.ca › php-array-functions-instead-loops
How to use PHP array functions instead of loops | The Man in the Arena
September 11, 2016 - The array functions that we’ll see today are all higher-order functions. Meanwhile, anonymous functions only exist to serve these higher-order functions. Like we mentioned earlier, higher-order functions can take them as arguments. But they can also return them as a result. In functional programming, they’re not any different from strings or any other data types. And this brings us to the PHP concept of “callables“. Callables are a PHP data type used to define a function or method that PHP can call.
🌐
DEV Community
dev.to › thecodeliner › most-useful-php-array-functions-you-should-know-3o4g
Most Useful PHP Array Functions You Should Know - DEV Community
June 1, 2025 - Working with arrays is a core part of PHP development, and mastering these built-in functions can significantly boost your productivity. Functions like array_map(), array_filter(), and array_column() not only simplify complex logic but also ...
🌐
DEV Community
dev.to › karleb › 10-php-array-functions-and-how-to-use-them-462n
10 PHP Array Functions And How To Use Them - DEV Community
March 6, 2023 - The handiest array functions I use are array_filter, array_map and usort. The hardest thing to do with them in PHP is to remember which ones take their arguments in which order, and which ones mutate the original array, which is pretty random.
🌐
Codecademy
codecademy.com › learn › learn-php › modules › learn-php-arrays › cheatsheet
Learn PHP: Learn PHP Arrays Cheatsheet | Codecademy
To use the function place an array or a variable with an array as its value in between the parentheses. ... The built-in PHP count() function takes an array as its argument and returns the number of elements in that array.