php method to replace array elements: 1. Replace the key of the array with the KEY value, the code is [$arr[$i][name] = $array[$i][shop_name]]; 2. Use To recursively replace the contents of the array, replace [] in the array with [{ }].
php method to replace array elements:
Although there are many functions for processing arrays in php, some The function also needs to be encapsulated by ourselves, for example, replacing the elements in the array:
The following is to replace the key of the array with the KEY value:
<?php $arr = array(); $array = array( 0 => array(shop_name=>1,shop_id=>2), 1 => array(shop_name => 2, shop_id=>3) ); for($i=0; $i<count($array); $i++) { $arr[$i][name] = $array[$i][shop_name]; $arr[$i][id]=$array[$i][shop_id]; } echo '<pre class="brush:php;toolbar:false">'; print_r ($arr); echo ''; ?>
The following example is a method of encapsulation:
Develop a small trick example program and use recursion to replace the contents of the array.
Replace in the array with
{ }
.
You can use this function according to the actual situation. It is very convenient to replace elements in the array;
<?php $arr = array("<小刚>","<小晓>","<小飞>","<小李>","<小红>"); function arrContentReplact($array) { if(is_array($array)) { foreach($array as $k => $v) { $array[$k] = arrContentReplact($array[$k]); } }else { $array = str_replace(array('<', '>'), array('{', '}'), $array); } return $array; } $arr3 = arrContentReplact($arr); echo "<pre class="brush:php;toolbar:false">"; print_r($arr3); echo ""; ?>
The output result of the above example is:
Array ( [0] => {小刚} [1] => {小晓} [2] => {小飞} [3] => {小李} [4] => {小红} )
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to replace array elements in php?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
