current position:Home>Learn to use PHP to insert elements at the specified position and key of the array
Learn to use PHP to insert elements at the specified position and key of the array
2022-04-29 16:05:13【Lingering memory of Yufei】
Learn to use php Implement at the specified position in the array 、 Key insert element
Function analysis
array_flip — Swap keys and values in an array
array_slice — Take a segment from the array
Sample code
/** * @param $input [ The array that needs to be modified ] * @param $offset [ After the starting position or key name of the insert ] * @param $length [ Inserted length or before key name ] * @param $replacement [ Elements to insert (array、string....)] * @return array */
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array())
{
$replacement = (array)$replacement;
$key_indices = array_flip(array_keys($input));
if (isset($input [$offset]) & is_string($offset)) {
$offset = $key_indices [$offset];
}
if (isset($input[$length]) && is_string($length)) {
$length = $key_indices [$length] - $offset;
}
// First take out the element before the adding position and merge it with the element to be added , Then take the elements after the addition position and merge them
$result = array_slice($input, 0, $offset, TRUE)
+ $replacement
+ array_slice($input, $offset + $length, NULL, TRUE);
return $result;
}
$input_str = '{"member_name":" Wandering memory ","address":" Chaoyang District, Beijing Municipality ","create_time":"2022 year 03 month 03 Japan 13 when 12 branch 56 second ","sum_score":10,"284_answer_text":"A、 correct ( answer :√ have to 5 branch )","285_answer_text":" Sima Qian ( answer :√ have to 5 branch )","287_answer_text":"119( answer :× have to 0 branch )"}';
$input = json_decode($input_str, true);
echo " Original array <pre>";
print_r($input );
$replacement = ['283_answer_text' => ' Li Bai '];
$offset = '284_answer_text';
// Interception length
$length = 0;
$result = array_splice_assoc($input, $offset, $length, $replacement);
echo " New array <pre>";
print_r($result);
Print the results
copyright notice
author[Lingering memory of Yufei],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291425562098.html
The sidebar is recommended
- Rasa dialogue robot serial 7 lesson 122: rasa dialogue robot debugging project practical bank financial dialogue robot whole life cycle debugging practice - (III)
- CSS foundation-15-drop-down menu
- Only one message prompt pops up in the element UI at a time
- Leetcode 82. Delete duplicate elements in the sorting linked list II
- This beast was blessed with skills to test drive the DHT version of Harvard beast
- Vue Click to switch the background color randomly (small demo)
- In the era of man-machine war, how did Beijing magic cube and artificial intelligence produce chemical reaction
- About nginx + Nacos using domain name connection invalid things
- How strong is the giant wave hybrid blessing when GAC motor shadow cool makes its global debut?
- Layui framework application FAQ
guess what you like
Layui style optimization
Post request (http-c4e7.post)
Is low code a blessing or a challenge for programmers?
Use the pointer of the array to test the relationship between the two-dimensional elements and the column of the array in the order of "% 1-6", and then use the pointer of the array to output the data in the order of "% 1-6", and then use the pointer of t
6-2 pointer and the sum fraction of each column of array matrix 10 this problem requires the implementation of a function to find the sum of each column of a two-dimensional array with n (less than 10) rows and 7 columns. The columns and are stored in a o
7-1 find the specified element in the array
When using uniapp for e-commerce projects, vuex is used to realize the global sharing of data to make shopping cart pages
JQuery Basics
`JQuery ` advanced
Do not leave the market unless necessary! Zhongshan City issued specific requirements for campus epidemic prevention after the May Day holiday
Random recommended
- Software design pattern -- Chapter 3 structural pattern -- sharing element pattern
- Vue uses the online form of Excel in the front end of lucky sheet to import, display and export Excel files
- Vue uses echart to draw national maps and overlay charts
- Vue + element UI: Vue user-defined instruction monitors the scrolling event of El table to scroll the scroll bar to the bottom and load new data
- Vue + element when there is no paging at the back end, the front end completes the paging of El table independently - scrolling to the bottom to load new data
- [react] react routing concept
- Lenovo z475 disassembly and repair - plate No. kl6c
- Random array into an array, requiring that the elements cannot be repeated
- The belated Toyota bz4x, even with the blessing of e-tnga architecture, is still not worth starting
- In element plus, for example, how to change the checkbox status in the list by clicking on the header and selecting all
- Crawler reverse advanced, using ast technology to restore JavaScript obfuscated code
- Help, after changing the user name, the computer is useless and can't log in
- Drag the left column of Vue and keep the right width unchanged; The scroll bar appears
- HTML notes
- In depth analysis of headless single linked list -- dynamic diagram demonstration of C language
- Share 9 development skills related to vue3
- CSS box centered
- Used in Vue projects Sync modifier and $emit (update: XXX)
- Vue class & Style binding and computed
- Vue project uses this$ forceUpdate(); Force render page
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
- Loki HTTP API usage
- JavaScript - prototype, prototype chain
- Front end experience
- JavaScript -- Inheritance
- HTTP cache
- Filters usage of table in elementui
- A JavaScript pit encountered by a non front-end siege lion
- Grain College - image error when writing Vue with vscode
- Utility gadget - get the IP address in the HTTP request
- Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer