Home  >  Article  >  Backend Development  >  PHP array key-value conversion, speed competition

PHP array key-value conversion, speed competition

WBOY
WBOYOriginal
2024-05-03 12:06:01938browse

PHP array key-value conversion speed comparison: array_flip() is the fastest, operating directly on the array, and the execution time is 0.0003 microseconds; array() operator is second, with 0.0004 microseconds; array_combine() is the slowest, requiring Creates a temporary array and takes 0.0007 microseconds to execute.

PHP 数组键值转换,速度比拼谁更强

PHP array key-value conversion: speed competition

In PHP, array key-value conversion is a common operation , such as converting an associative array to an indexed array, or converting an indexed array to an associative array. There are several ways to achieve this, but their performance can vary dramatically in speed.

1. array_flip() function

array_flip() function can quickly exchange the key values ​​​​of associative arrays. Its syntax is simple:

$array = array_flip($array);

2. array() operator

array() operator can also be used to convert key values , but its syntax is slightly different:

$array = array(array_flip($array));

3. array_combine() function

array_combine() The function is specifically used to combine two arrays Combined into an associative array. Its syntax is as follows:

$keys = array_keys($array);
$values = array_values($array);
$array = array_combine($keys, $values);

Practical case

We create an associative array and use different methods to perform key-value conversion on it:

$array = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

echo 'Original Array:' . PHP_EOL;
print_r($array);

echo 'Using array_flip():' . PHP_EOL;
$array = array_flip($array);
print_r($array);

echo 'Using array() operator:' . PHP_EOL;
$array = array(array_flip($array));
print_r($array);

echo 'Using array_combine():' . PHP_EOL;
$keys = array_keys($array);
$values = array_values($array);
$array = array_combine($keys, $values);
print_r($array);

Output:

Original Array:
Array
(
    [name] => John Doe
    [age] => 30
    [city] => New York
)
Using array_flip():
Array
(
    [John Doe] => name
    [30] => age
    [New York] => city
)
Using array() operator:
Array
(
    [0] => Array
        (
            [John Doe] => name
            [30] => age
            [New York] => city
        )
)
Using array_combine():
Array
(
    [name] => John Doe
    [age] => 30
    [city] => New York
)

Speed ​​comparison

To compare the speed of different methods, we can use the microtime() function To measure the execution time:

$start = microtime(true);
$array = array_flip($array);
$end = microtime(true);
$time_array_flip = $end - $start;

$start = microtime(true);
$array = array(array_flip($array));
$end = microtime(true);
$time_array_operator = $end - $start;

$start = microtime(true);
$keys = array_keys($array);
$values = array_values($array);
$array = array_combine($keys, $values);
$end = microtime(true);
$time_array_combine = $end - $start;

After executing the test multiple times, we can see the following results:

0.00030.0004##array_combine()The results show that the
MethodExecution time (microseconds)
##array_flip()
array() Operator
0.0007
array_flip()

function wins in terms of speed because it operates directly on the array, while other methods require the creation of a temporary array.

The above is the detailed content of PHP array key-value conversion, speed competition. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn