Home Backend Development PHP Tutorial 请教这个一维数组如何根据键名相关性转化成二维数组?多谢

请教这个一维数组如何根据键名相关性转化成二维数组?多谢

Jun 13, 2016 am 10:54 AM
100 200 300 array gt

请问这个一维数组怎么根据键名相关性转化成二维数组?谢谢
谢谢各位大哥!我的一维数组如下

PHP code

1

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->?Array ( [xm1] => 人工新砌管道井 [dj1] => 300 [sl1] => 100 [zj1] => 30000 [xm2] => 卫生间回填 [dj2] => 100 [sl2] => [zj2] => [xm3] => 电视背景墙 [dj3] => 300 [sl3] => [zj3] => [xm4] => 客厅酒柜电视背景酒柜 [dj4] => 100 [sl4] => [zj4] => [xm5] => 入户门鞋柜装饰(活动送鞋柜) [dj5] => 130 [sl5] => [zj5] => [xm6] => 书柜隔断(含静音棉夹层) [dj6] => 200 [sl6] => [zj6] => [xm8] => 厨房移动门头储藏柜 [dj8] => 200 [sl8] => [zj8] => [xm9] => 儿童房衣柜隔断(含静音棉夹层) [dj9] => 100 [sl9] => [zj9] => [xm10] => 厨房橱柜 [dj10] => 200 [sl10] => [zj10] => [xm11] => 卫生间洗手台 [dj11] => 300 [sl11] => [zj11] => [xm12] => 客厅、餐厅、过道、石膏板造型吊顶 [dj12] => 350 [sl12] => [zj12] => [xm13] => 卫生间、厨房集成吊顶 [dj13] => 250 [sl13] => [zj13] => [xm14] => 顶、墙面双飞粉乳胶漆 [dj14] => 200 [sl14] => [zj14] => [xm15] => 厨房、洗衣房、卫生间防滑地砖 [dj15] => 120 [sl15] => [zj15] => [xm16] => 厨房、卫生间墙面镶贴300*450墙砖 [dj16] => 200 [sl16] => [zj16] => [xm17] => 窗台石铺设 [dj17] => 200 [sl17] => [zj17] => [xm18] => 门槛石 [dj18] => 400 [sl18] => [zj18] => [xm19] => 防盗窗 [dj19] => 400 [sl19] => [zj19] => [xm20] => 书房门套 [dj20] => 30 [sl20] => [zj20] => [xm21] => 卫生间洗衣房门套 [dj21] => 60 [sl21] => [zj21] => [xm22] => 地、墙面防水、防潮处理 [dj22] => 70 [sl22] => [zj22] => [xm23] => 水、电路改造 [dj23] => 400 [sl23] => [zj23] => [xm24] => 完工保洁服务 [dj24] => 150 [sl24] => [zj24] => [xm25] => 成品保护费 [dj25] => 200 [sl25] => [zj25] => [xm26] => 材料搬运费 [dj26] => 5 [sl26] => [zj26] => [xm27] => 垃圾清运费 [dj27] => 5 [sl27] => [zj27] => [xm28] => 厨房地砖(活动7折优惠) [dj28] => 120 [sl28] => [zj28] => [xm29] => 厨房墙砖(活动7折优惠) [dj29] => 140 [sl29] => [zj29] => [xm30] => 卫生间地砖(活动7折优惠) [dj30] => 100 [sl30] => [zj30] => [xm31] => 卫生间墙砖(活动7折优惠) [dj31] => 80 [sl31] => [zj31] => [xm32] => 强化地板(活动买一送一) [dj32] => 170 [sl32] => [zj32] => [xm33] => 卧室套装门 [dj33] => 730 [sl33] => [zj33] => [xm34] => 卫生间门 [dj34] => 570 [sl34] => [zj34] => [xm35] => 厨房移动门 [dj35] => 300 [sl35] => [zj35] => [xm36] => 衣柜移动门 [dj36] => 300 [sl36] => [zj36] => [xm37] => 厨房、卫生间集吊顶 [dj37] => 260 [sl37] => [zj37] => [xm38] => 窗台石、橱柜台面石 [dj38] => 260 [sl38] => [zj38] => [xm39] => 门槛石 [dj39] => 140 [sl39] => [zj39] => )

Copy after login


我想转化成如下
PHP code

1

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Array ([0]=>Array([xm] => 人工新砌管道井[dj] => 300 [sl] => 100 [zj] => 30000[1]=>Array(......)))

Copy after login


------解决方案--------------------
PHP code

1

2

3

4

5

6

$res    = array();foreach($arr AS $k => $v){    $res[substr($k, 2)-1][substr($k, 0, 2)]    = $v;}/*Array(    [0] => Array        (            [xm] => 人工新砌管道井            [dj] => 300            [sl] => 100            [zj] => 30000        )    [1] => Array        (            [xm] => 卫生间回填            [dj] => 100            [sl] =>             [zj] =>         )*/<div class="clear">

                  

               

               

         

            </div>

Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

See all articles