In PHP, negotiating arrays is a very common operation. When we get data from a database or other data source, we usually get an associative array or object. However, in some cases, we need to convert it to an indexed array for easy manipulation or passing to other functions. This article explains how to cast a PHP associative array to an indexed array.
First, let’s look at an example. Suppose we obtain some user data from the database, as follows:
$users = array( array('id' => 1, 'name' => 'John', 'age' => 22), array('id' => 2, 'name' => 'Jane', 'age' => 30), array('id' => 3, 'name' => 'Bob', 'age' => 25), );
This is a two-dimensional associative array containing three user data. If we want to convert it to a simple one-dimensional indexed array, we can use the array_values()
function.
$users = array_values($users);
This will return the following result:
array( array('id' => 1, 'name' => 'John', 'age' => 22), array('id' => 2, 'name' => 'Jane', 'age' => 30), array('id' => 3, 'name' => 'Bob', 'age' => 25), )
This does not have the effect we want, because the same two-dimensional array is actually returned. This is because the array_values()
function can only convert one-dimensional arrays and cannot recursively process multi-dimensional arrays. Therefore, we need to use a recursive function to achieve this.
The following is a recursive function that converts an associative array of arbitrary depth into an indexed array:
function array_to_index($arr) { if (!is_array($arr)) { return $arr; } $new_arr = array(); foreach ($arr as $key => $value) { $new_arr[] = array_to_index($value); } return $new_arr; }
This function accepts an array of arbitrary depth as a parameter and returns an indexed array.
We can use this function to convert the above $users
array into a one-dimensional indexed array:
$users = array_to_index($users);
This will return the following result:
array( array('id' => 1, 'name' => 'John', 'age' => 22), array('id' => 2, 'name' => 'Jane', 'age' => 30), array('id' => 3, 'name' => 'Bob', 'age' => 25), )
This seems to be no different from the previous result, but it is actually a one-dimensional array because each element is an array.
To access the data of a single user, we can use subscripts:
$user1 = $users[0]; $user2 = $users[1]; $user3 = $users[2]; echo $user1['name']; // 输出 John echo $user2['age']; // 输出 30 echo $user3['id']; // 输出 3
This is a way to cast an associative array to an indexed array. It can be used recursively on arrays of any depth without changing the keys in the array.
Of course, this is just a simple example. In actual development, we may need to convert other types of associative arrays (such as objects) into indexed arrays, or need to further process the array to meet specific needs. However, this recursive function provides a convenient blueprint that can help us better understand and manipulate PHP arrays.
The above is the detailed content of How to force conversion to indexed array in php. For more information, please follow other related articles on the PHP Chinese website!