Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php replaces a value in an array

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how php replaces a value in an array". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deeper to study and learn "how php replaces a value in an array" together!

Replacement methods: 1. Use "array_values($arr)" to convert the specified array into an index array;2. Use "array_search(" specified value ", index array)" to search for values and return corresponding index values;3. Replace with "array_splice($arr, index, 1," replacement value ")".

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

php replaces a value in an array

1. Use array_values() to convert the specified array to an indexed array

Use array_search() to search for the specified value and return the corresponding index value (key name)

The array_search() function searches an array for a key and returns the corresponding key name.

$index=array_search("green",$values);echo $index;

Use array_splice() to replace the array value with the index value.

array_splice($arr, $index, 1, "orange");var_dump($arr);

Description: ray_splice() function

The array_splice() function deletes a portion of an array; you can delete it directly or replace it with another value.

array_splice() syntax is as follows:

array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )

Parameter Description:

arr represents an array.

start indicates where deletion starts (subscript):

If start is positive, delete from front to back.

If start is negative, delete from back to front starting at-start from the end of arr. For example,-2 means starting with the penultimate element of the array.

length is an optional parameter indicating the number of elements to delete:

If length is a positive number, then length elements are deleted;

If length is negative, then all elements starting at start and ending at the end of the array are deleted.

If omitted, all elements from start to the end of the array are deleted.

Replacement is an optional parameter that indicates the value to replace. If replacement has multiple values, it needs to be set to an array, and if it has only one value, it can not be set to an array.

If the result of the combination of start and length is that no element is deleted, then the value contained in replacement is inserted at the position specified by start.

Note that replacing an array element with replacement does not preserve the original key name.

Return Value: Returns an array of deleted elements.

Thank you for reading, the above is "php how to replace a value in the array" content, after learning this article, I believe we have a deeper understanding of how php replaces a value in the array, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report