Home > Backend Development > PHP Problem > How to replace the value of a two-dimensional array with a key in php

How to replace the value of a two-dimensional array with a key in php

PHPz
Release: 2023-04-18 15:35:04
Original
1087 people have browsed it

PHP is a widely used open source high-level programming language and is widely used in Web development. Its flexibility and ease of use make it the first choice for many website frameworks, CMS, blog systems, etc. In PHP, we often need to deal with arrays. This article will show you how to replace the keys of a two-dimensional array with the values ​​of a two-dimensional array using PHP.

1. The concept of two-dimensional array

A two-dimensional array is an array, and each element of it is also an array. This sub-array can contain any data type, such as strings, numbers, Boolean values, objects, etc.

$users = array(
array('id' => 1001, 'name' => 'Tom', 'age' => 23),
array('id ' => 1002, 'name' => 'Jerry', 'age' => 28),
array('id' => 1003, 'name' => 'Alice', 'age ' => 20)
);

The above code is a two-dimensional array containing three elements. Each element contains three key-value pairs, representing the user's ID, name and age respectively. .

2. Replace the value of the array with the key

Sometimes, we need to use the value of a key-value pair in the array as the key of the new array, so that we can use the value to perform Query, sort and other operations. You can loop through a two-dimensional array and then convert the key-value pair of each element into an array whose value is the key.

For example, we need to make a new array key based on the id value of each element in the two-dimensional array.

$users = array(
array('id' => 1001, 'name' => 'Tom', 'age' => 23),
array('id ' => 1002, 'name' => 'Jerry', 'age' => 28),
array('id' => 1003, 'name' => 'Alice', 'age ' => 20)
);

$newArr = array();
foreach ($users as $user) {
$newArr[$user['id']] = $user;
}

In the above code, we iterate through the $users array and set the new array key to the id value of each element. Finally, we get a new array containing elements keyed by id.

3. Use PHP built-in functions to achieve

In addition to manual traversal, we can also use PHP built-in function array_column() to implement a key value of a two-dimensional array as a key.

$users = array(
array('id' => 1001, 'name' => 'Tom', 'age' => 23),
array('id ' => 1002, 'name' => 'Jerry', 'age' => 28),
array('id' => 1003, 'name' => 'Alice', 'age ' => 20)
);

$newArr = array_column($users, null, 'id');

In the above code, the first one of the array_column() function The parameter is the two-dimensional array that needs to be operated, and the second parameter is the field that needs to be converted. Use null here to convert the entire element. The third parameter is the field corresponding to the new key value.

4. Practical use cases

The above implementation methods are very useful in actual development. For example, when developing an e-commerce website, we need to recommend related products based on user browsing history. Suppose we have a product table where the ID, name, price, etc. of each product are stored as an associative array. In order to generate a list of product recommendations, we need to look up this array based on the customer's browsing history.

$products = array(
array('id' => 1, 'name' => 'Apple iPhone XS', 'price' => 1059),
array( 'id' => 2, 'name' => 'Samsung Galaxy S9', 'price' => 939),
array('id' => 3, 'name' => 'Google Pixel 3', 'price' => 799),
array('id' => 4, 'name' => 'Sony Xperia XZ3', 'price' => 875),
array('id' => 5, 'name' => 'Xiaomi Mi Mix 3', 'price' => 569)
);

$history = array(3, 1 , 2, 5); //Customer browsing history, stored in array

$recommendations = array();
foreach ($history as $productId) {
$product = $products[ array_search($productId, array_column($products, 'id'))];
$recommendations[$product['name']] = $product['price'];
}

In the above code, we first define the $products array, where each element is an associative array containing product information. We also define a $history array that contains the product IDs viewed by each user.

We loop through the $history array, use the array_column() function to find the index of the corresponding element in the $products array, and then take out the name and price information of the element, using the product name as the key and the price as the value, and store in the $recommendations array. Finally, we have a recommendation list for recommending more products to our customers.

Summarize

In PHP, through loop traversal and the built-in function array_column(), we can set the value corresponding to a key value of the two-dimensional array as the new key value of the array. This method is very practical in actual development and can greatly improve development efficiency. It is also one of the powerful functions of PHP.

The above is the detailed content of How to replace the value of a two-dimensional array with a key in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template