php的array
这段时间做数据统计,产品那边要求数据能按高到低排序,想了很多方法,都不行。最后找到PHP的array_multisort()方法。虽然实现了效果,但对这个方法还是半董半董 下边就是使用的实例,手册上的例子不列了,就我自己工作中的几个。 工作中碰到最多的可能就是
这段时间做数据统计,产品那边要求数据能按高到低排序,想了很多方法,都不行。最后找到PHP的array_multisort()方法。虽然实现了效果,但对这个方法还是半董半董
下边就是使用的实例,手册上的例子不列了,就我自己工作中的几个。
工作中碰到最多的可能就是二维数组了,想试试三维数组,但是想想还是算了。
如果有误,请以PHP官网的官方手册为准。http://php.net/manual/zh/function.array-multisort.php
<span> 1</span> <span>header</span>('Content-Type: text/html; charset=utf-8'<span>); </span><span> 2</span> <span>echo</span> '<pre class="brush:php;toolbar:false">'<span>; </span><span> 3</span> <span>//</span><span>原始数组格式</span> <span> 4</span> <span>$array</span> = <span>array</span><span>( </span><span> 5</span> 'key1' => <span>array</span><span>( </span><span> 6</span> 'item1' => '65', <span> 7</span> 'item2' => '35', <span> 8</span> 'item3' => '84', <span> 9</span> ), <span>10</span> 'key2' => <span>array</span><span>( </span><span>11</span> 'item1' => '24', <span>12</span> ), <span>13</span> 'key3' => <span>array</span><span>( </span><span>14</span> 'item1' => '38', <span>15</span> 'item3' => '45', <span>16</span> ), <span>17</span> <span>); </span><span>18</span> <span>//</span><span>要排序的键 </span><span>19</span> <span>//按照数组中的 item1进行排序 </span><span>20</span> <span>//你也可以换成item2</span> <span>21</span> <span>$sort</span> = 'item1'<span>; </span><span>22</span> <span>foreach</span>(<span>$array</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) </span><span>23</span> <span>{ </span><span>24</span> <span>$newArr</span>[<span>$k</span>] = <span>$v</span>[<span>$sort</span><span>]; </span><span>25</span> <span>} </span><span>26</span> <span>//</span><span>这个函数如果执行正确他会直接改变原数组键值的顺序 </span><span>27</span> <span>//如果执行失败,那么他会返回 bool(false)</span> <span>28</span> <span>array_multisort</span>(<span>$newArr</span>,SORT_DESC, <span>$array</span><span>); </span><span>29</span> <span>var_dump</span>(<span>$array</span><span>); </span><span>30</span> <span>//</span><span>---------------------排序后的数组打印效果 开始--------------------</span> <span>31</span> <span>array</span>(3<span>) { </span><span>32</span> ["key1"]=> <span>33</span> <span>array</span>(3<span>) { </span><span>34</span> ["item1"]=> <span>35</span> <span>string</span>(2) "65" <span>36</span> ["item2"]=> <span>37</span> <span>string</span>(2) "35" <span>38</span> ["item3"]=> <span>39</span> <span>string</span>(2) "84" <span>40</span> <span> } </span><span>41</span> ["key3"]=> <span>42</span> <span>array</span>(2<span>) { </span><span>43</span> ["item1"]=> <span>44</span> <span>string</span>(2) "38" <span>45</span> ["item3"]=> <span>46</span> <span>string</span>(2) "45" <span>47</span> <span> } </span><span>48</span> ["key2"]=> <span>49</span> <span>array</span>(1<span>) { </span><span>50</span> ["item1"]=> <span>51</span> <span>string</span>(2) "24" <span>52</span> <span> } </span><span>53</span> <span>} </span><span>54</span> <span>//</span><span>---------------------排序后的数组打印效果 结束---------------------</span>

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
