<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## **數組合并** ### PHP 數組相加 和 數組合并array_merge ``` $arr1 = array("a"=>"PHP","b"=>"java","python"); $arr2 = array("c" =>"ruby","d" => "c++","go","a"=> "swift"); $arr3 = array_merge($arr1,$arr2); $arr4 = $arr1 + $arr2; var_dump($arr3); var_dump($arr4); ``` 輸出: ``` array ( 'a' => 'swift', 'b' => 'java', 0 => 'python', 'c' => 'ruby', 'd' => 'c++', 1 => 'go', ) array ( 'a' => 'PHP', 'b' => 'java', 0 => 'python', 'c' => 'ruby', 'd' => 'c++', ) ``` 區別: 當下標為數值時,array\_merge()不會覆蓋掉原來的值,但arrayA+arrayB合并數組則會把最先值arrayA的值作為最終結果返回,而把arrayB數組擁有相同鍵名的那些值“拋棄”掉,而不是覆蓋 當下標為字符時,arrayA+arrayB 仍然把最先出現的值作為最終結果返回,而把后面的數組擁有相同鍵名的那些值“拋棄”掉,但array\_merge()此時會覆蓋掉前面相同鍵名的值 ### [array\_?combine?($keysArr , $valuesArr);](https://www.php.net/manual/zh/function.array-combine.php "array_?combine")? 兩個數組分別作為新數組的鍵與值進行合并 ## **數組排序** ### **排序函數一覽** | 函數名稱 | 排序依據 | 數組索引鍵保持 | 排序的順序 | 相關函數 | | --- | --- | --- | --- | --- | | [array\_multisort()](https://www.php.net/manual/zh/function.array-multisort.php) | 值 | 鍵值關聯的保持,數字類型的不保持 | 第一個數組或者由選項指定 | [array\_walk()](https://www.php.net/manual/zh/function.array-walk.php) | | [asort()](https://www.php.net/manual/zh/function.asort.php) | 值 | 是 | 由低到高 | [arsort()](https://www.php.net/manual/zh/function.arsort.php) | | [arsort()](https://www.php.net/manual/zh/function.arsort.php) | 值 | 是 | 由高到低 | [asort()](https://www.php.net/manual/zh/function.asort.php) | | [krsort()](https://www.php.net/manual/zh/function.krsort.php) | 鍵 | 是 | 由高到低 | [ksort()](https://www.php.net/manual/zh/function.ksort.php) | | [ksort()](https://www.php.net/manual/zh/function.ksort.php) | 鍵 | 是 | 由低到高 | [asort()](https://www.php.net/manual/zh/function.asort.php) | | [natcasesort()](https://www.php.net/manual/zh/function.natcasesort.php) | 值 | 是 | 自然排序,大小寫不敏感 | [natsort()](https://www.php.net/manual/zh/function.natsort.php) | | [natsort()](https://www.php.net/manual/zh/function.natsort.php) | 值 | 是 | 自然排序 | [natcasesort()](https://www.php.net/manual/zh/function.natcasesort.php) | | [rsort()](https://www.php.net/manual/zh/function.rsort.php) | 值 | 否 | 由高到低 | [sort()](https://www.php.net/manual/zh/function.sort.php) | | [shuffle()](https://www.php.net/manual/zh/function.shuffle.php) | 值 | 否 | 隨機 | [array\_rand()](https://www.php.net/manual/zh/function.array-rand.php) | | [sort()](https://www.php.net/manual/zh/function.sort.php) | 值 | 否 | 由低到高 | [rsort()](https://www.php.net/manual/zh/function.rsort.php) | | [uasort()](https://www.php.net/manual/zh/function.uasort.php) | 值 | 是 | 由用戶定義 | [uksort()](https://www.php.net/manual/zh/function.uksort.php) | | [uksort()](https://www.php.net/manual/zh/function.uksort.php) | 鍵 | 是 | 由用戶定義 | [uasort()](https://www.php.net/manual/zh/function.uasort.php) | | [usort()](https://www.php.net/manual/zh/function.usort.php) | 值 | 否 | 由用戶定義 | [uasort()](https://www.php.net/manual/zh/function.uasort.php) | ### [**array\_?multisort**](https://www.php.net/manual/zh/function.array-multisort.php "array_?multisort") ( &$array1, mixed SORT_ASC, mixed SORT_REGULAR,&$array2, mixed SORT_ASC, mixed SORT_REGULAR, mixed $... = ? ): bool對多個數組或多維數組進行排序 參數1:要排序的array 參數2:之前array參數要排列的順序。**`SORT_ASC`**按照上升順序排序,**`SORT_DESC`** 按照下降順序排序 參數3:為array參數設定選項: 排序類型標志: * **`SORT_REGULAR`**\- 將項目按照通常方法比較(不修改類型) * **`SORT_NUMERIC`**\- 按照數字大小比較 * **`SORT_STRING`**\- 按照字符串比較 * **`SORT_LOCALE_STRING`**\- 根據當前的本地化設置,按照字符串比較。 它會使用 locale 信息,可以通過[setlocale()](https://www.php.net/manual/zh/function.setlocale.php)修改此信息。 * **`SORT_NATURAL`**\- 以字符串的"自然排序",類似[natsort()](https://www.php.net/manual/zh/function.natsort.php) * **`SORT_FLAG_CASE`**\- 可以組合 (按位或 OR)**`SORT_STRING`**或者**`SORT_NATURAL`** 大小寫不敏感的方式排序字符串。 * ...更多參數:可選的選項,可提供更多數組,跟隨在 sort order 和 sort flag 之后。 提供的數組和之前的數組要有相同數量的元素。 換言之,排序是按字典順序排列的。 通常可以用此函數進行排名 ``` $arr=[286,298,284,288]; array_multisort($arr,SORT_DESC,SORT_REGULAR); array_multisort($arr,SORT_REGULAR,SORT_DESC);//數組之后跟的兩個參數可以呼互換 //[298,288,268,284] ``` 示例 #2 排序多維數組 ``` $ar = array( array("10", 11, 100, 100, "a"), array( 1, 2, "2", 3, 1) ); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); var_dump($ar); ``` #### 對數據庫指定的列排序,這里指定cost_staus和subject兩個進行排序(太神奇了) ``` $newdata =[ ['id' => 1,'cost_staus'="1",'subject'=>"營銷部"], ['id' => 2,'cost_staus'="1",'subject'=>"招待部"], ['id' => 3,'cost_staus'="0",'subject'=>"營業部"] ] foreach ($newdata as $key => $row) { $cost_staus[$key] = $row['cost_staus']; $subject[$key] = $row['subject']; } //對這兩個進行升序,優先級以靠前的參數為主,其他字段傳入newdata按照普通排序 array_multisort($cost_staus, SORT_ASC, $subject, SORT_ASC, $newdata); ``` **示例 4: 不區分大小寫字母排序** **`SORT_STRING`**和**`SORT_REGULAR`**都是區分大小寫字母的,大寫字母會排在小寫字母之前。 要進行不區分大小寫的排序,就要按照原數組的小寫字母拷貝來排序。 ``` $array?=?array('Alpha',?'atomic',?'Beta',?'bank'); $array_lowercase?=?array_map('strtolower',?$array); array_multisort($array_lowercase,?SORT_ASC,?SORT_STRING,?$array); print_r($array); ``` 輸出 ~~~ Array ( [0] => Alpha [1] => atomic [2] => bank [3] => Beta ) ~~~ ## **分割字符串為數組** ### explode ~~~ $str = "Hello world. I love Shanghai!"; print_r (explode(" ",$str));//Array ( [0] => Hello [1] => world. [2] => I [3] => love [4] => Shanghai! ) var_dump(explode('<=>','a<=>b c<=>d'));//array ( 0 => 'a', 1 => 'b c', 2 => 'd', ) ~~~ ### preg_split 多處分割 ``` $str="FreeSpace=46627741696 Size=85897945088"; $res=preg_split("/[(Size=)|(FreeSpace=)]+/", trim($v),-1, PREG_SPLIT_NO_EMPTY);//PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE 結果: array ( 0 => '46627741696 ', 1 => '85897945088', ) ``` **數組:** >[danger]PHP使用explode和preg_split截取出來的數組,獲取指定下標時報錯:Undefined offset: 1 **解決辦法** 首先指定explode截取長度;然后使用array\_pad將未定義的數組長度的值補全,這里賦值為null list($a, $b, $c, $d) = array\_pad(explode("-", $str, 4), 4, null); 方法2:添加錯誤抑制符@ >[danger]**注意:[array\_?walk\_?recursive](https://www.php.net/manual/zh/function.array-walk-recursive.php "array_?walk_?recursive")、?[array\_?merge\_?recursive](https://www.php.net/manual/zh/function.array-merge-recursive.php "array_?merge_?recursive")?、[array\_?replace\_?recursive](https://www.php.net/manual/zh/function.array-replace-recursive.php "array_?replace_?recursive")?支持多維數組,其他都是一維數組的操作?** ## **刪除及過濾** ### **array\_filter**($arr ,$callback \[, int $flag = 0 \]\] );//數組過濾刪除回調函數中返回fasle的數組 ~~~ $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; //ARRAY_FILTER_USE_KEY 只接受鍵 array_filter($arr, function($key) { return $key == 'b'; #當為true時則保留 }, ARRAY_FILTER_USE_KEY); #['b'=>2] //ARRAY_FILTER_USE_BOTH 接受鍵值 array_filter($arr, function($v, $key) { return $key == 'b' || $v == 4; }, ARRAY_FILTER_USE_BOTH); #['b'=>2,'d'=>4]; //只有一個參數時會將數組的值轉為boolean 為true 則保留,否則刪除 ~~~ ### 使用回調函數過濾數組的元素 ~~~ $arr = array('foo', 5, 10, 'bar',false);//第一個參數數組的值作為填充數組的鍵,第二個參數作為值 $a=array_filter($arr ,function($value,$key){//如果沒有回調函數將刪除 array 中所有等值為 FALSE 的條目 if($key==1){ return false;//此函數內一定要有返回true,保留數組中返回true的元素 }else{ return true; } //return ==1; },ARRAY_FILTER_USE_BOTH);//數組過濾刪除回調函數中返回fasle的數組值 存在ARRAY_FILTER_USE_BOTH時,回調函數同時接受鍵值兩個參數否則只接受key /* 返回 array{ [0]=> "foo", [2]=>10, [3]=>"bar", [4]=>false, } */ $a=array_filter($arr); /* 返回 array{ [0]=> "foo", [1]=>5, [2]=>10, [3]=>"bar", } */ ~~~ 獲取數組指定范圍的新數組 ``` $arr=[1,2,3,4,5,6,7,8,9,10,11,12]; function getArrRangeValue($arr, $min, $max){ $newArr=array_filter($arr ,function($value,$key) use($min,$max){ if ($value>=$min && $value<=$max) { return true; } },ARRAY_FILTER_USE_BOTH); return $newArr; } $res=getArrRangeValue($arr,2,5); print_r($res);//Array([1] => 2,[2] => 3,[3] => 4,[4] => 5) ``` ### **array_splice**($arr,$start_index_int,$remove_length);//移除索引數組中指定位置及之后的length個元素,返回被移除的元素數組` ~~~ $arr=[1,2,3,4,5]; array_splice($arr,0,2);//返回[1,2] 此時$arr=[3,4,5] ~~~ ### **unset**()函數刪除的話,數組的索引值沒有變化。 ~~~ $arr=[1,2,3,4]; $unset($arr[1]);//arr=[0=>1,2=>3,3=>4] ~~~ ### **array_pop**($arr);刪除最后一個元素(出棧)? ~~~ $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_pop($stack); ~~~ ### **array_shift**(?`$arr`?);刪除第一個元素 ~~~ $arr= array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($arr);//[0=>'banana',1=>'apple',2=>'raspberry']; ~~~ ## **數組元素追加** ### **array_splice**($arr,$start_index_int,$remove_length,$replace_arr);//$start_index_int,$remove_length組合不再$arr的范圍即不能刪除和替換時就插入 ### $arr[]='value'; ### **array_push**($arr,$value1,$value2,...);//尾部插入新單元(入棧) 就新增單元而言$arr\[\]=value 效率更高 ?注意:array\_splice不保留鍵名每次都會截斷并重新生成索引鍵 ### **array\_unshift**($arr,value1,$value2,$valueN...)? 數組的開頭插入value ## **隨機:** ### shuffle? 打亂數組 ### array_rand($arr,5)? ? 重數組總共隨機抽取5個一維元素 ## **鍵值互換**: ### **array_flip**($arr) ## **合并** ### [array_?merge](https://www.php.net/manual/zh/function.array-merge.php "array_?merge") ### [array_?merge\_?recursive](https://www.php.net/manual/zh/function.array-merge-recursive.php "array_?merge_?recursive") ## **拆分** ### **array_chunk**($arr,$size,false為不保留鍵)? ?按照每個$size個數拆分數組 ### [**array_?chunk**($arr, $size, false);//[1,2,3,4]==>[[1,2],[3,4]]? ?分割成size大小的數組 第三個參數preserve_keys是否保留原鍵名](https://www.php.net/manual/zh/function.array-chunk.php "array_?chunk") ## **截取** ### **array_slice**($arr ,$start\_index [,$length = NULL [,$preserve_keys = false ]] );//length 為null 截取 start_index ~數組末尾的 ~~~ $input = array("a", "b", "c", "d", "e"); #第二個參數為父則從數組末尾開始,第三個參數為負則截取在末尾至(-參數)位置之前的 第三參數為true則不重置數字索引 array_slice($input, 2); //[ "c", "d", "e"] array_slice($input, -2, 1); // ["d"] array_slice($input, 0, 3); //["a","b","c"] array_slice($input, 2, -1); //[0=>'c',1=>'d'] array_slice($input, 2, -1, true);//[2=>'c',3=>'d'] ~~~ ## **數組元素替換(修改)** ### **array_splice**($arr,$start_index_int,$remove_length,$replace_arr);//第四個參數存在時不是移除而是修改` ### **array_replace_recursive**($arr,$arr1,$arrN);//遞歸替換$arr獨有保持不變,$arr1獨有則追加,同時存在則替換(先arr1替換,完事后arrN再替換)即支持多維替換 ~~~ $arr=['a'=>1,'b'=>2,'c'=>3,4]; $arr1=['a'=>100]; $arrN=['d'=>5]; $ar=array_replace_recursive($arr,$arr1,$arrN); array(5) { ["a"]=> int(100) ["b"]=>int(2) ["c"]=>int(3) [0]=>int(4) ["d"]=>int(5) } ~~~ ### **array_replace**($arr ,$arr1,$arrN )? ?(用法同**array_replace_recursive**)一維?數組替換 不能遞歸替換 ## **數組元素查找** ### **array_search**($search_value,?`$arr`,?false ); 一維數組中查找指定元素 返回鍵名 (只返回匹配的第一個) ### **array_keys**($arr?,?`$search_value=null,false`?);//返回一維數組元素值相同的所有鍵名 ### **array_keys**($arr);//返回一維數組的所有鍵名 ~~~ $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red',4=>"green"); $key = array_search('green', $array); // $key = 2; $keys=array_keys($array,'green');//[2,4] ~~~ ### **array_values**($arr);? ? 返回數組中所有的值? ### **in_array**($search_value, $arr, FALSE【嚴格匹配】);? ?檢查一維數組里是否存在給定的值? ? ### [array_key_exists()](https://www.php.net/manual/zh/function.array-key-exists.php)?- 檢查數組一維里是否有指定的鍵名或索引(返回bolean)? ## **求和** ## **array_sum**()? 返回一維數組所有的值總和 ## **數組去重** ### **array_unique($arr)** ### **array_flip**(array_flip($arr)); 這樣便可以刪除重復元素。 比上面的要效率; 原理:array_flip()交換數組中的鍵和值但是php鍵是唯一的而且數組中可以有相同的值如果交換鍵和值的話最后一個相同的值或取代前面相同的值進行交換 ## **關聯數組變索引數組:** ### **array_values($arr)** 返回含所有值的索引數組 ## **php數組差集交集并集** ### **差集:(取獨有)**? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 返回arr中有某個值而其他它arr1~arrN無此值或鍵或鍵值的結果 array\_?diff\_?key? ? ? ? ? ? ? ? ? ? ? ? 計算差集(系統函數比較鍵) array\_?diff? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算差集(系統函數比較值) array\_?diff\_?assoc? ? ? ? ? ? ? ? ? ? ?計算差集(系統函數比較鍵和值) array\_?diff\_?uassoc? ? ? ? ? ? ? ? ? ?計算差集(系統函數比較值,自定義函數比較鍵) array\_?diff\_?ukey? ? ? ? ? ? ? ? ? ? ? ?計算差集(自定義函數比較鍵)要么全是關聯數組要么全是索引數組 array\_?udiff\_?assoc? ? ? ? ? ? ? ? ? ?計算差集(系統函數比較鍵,自定義函數比較值) array\_?udiff\_?uassoc? ? ? ? ? ? ? ? ?計算差集(自定義函數比較值和鍵) array\_?udiff? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?計算差集(自定義函數比較值) ### **交集(取共同)**? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 返回arr中有某個值且其他它arr1~arrN也有此鍵或值或鍵值的結果 array\_?intersect\_?key? ? ? ? ? ? ? ? ?計算交集(系統函數比較鍵) array\_?intersect? ? ? ? ? ? ? ? ? ? ? ? 計算交集(系統函數比較值) array\_?intersect\_?assoc? ? ? ? ? ? ?計算交集(系統函數比較鍵和值) array\_?intersect\_?uassoc? ? ? ? ? ?計算交集(系統函數比較鍵和值) array\_?intersect\_?ukey? ? ? ? ? ? ? ?計算交集(系統函數比較值,自定義函數比較鍵) array\_?uintersect\_?assoc? ? ? ? ? ?計算交集(系統函數比較鍵,自定義函數比較值) array\_?uintersect\_?uassoc? ? ? ? ?計算交集(自定義函數比較值和鍵) array\_?uintersect? ? ? ? ? ? ? ? ? ? ? 計算交集(自定義函數比較值) ### **并集**(取全部) array\_merge($arr1,$arrN); array\_merge\_recursive ($array1 ,$arrN); //有相同字符串的key則合并,其余追加(鍵值相同不覆蓋) ## **數組循環比較** 一般加引用要慢點 foreach 與array\_walk相比?array\_walk多出一個函數調用 效率 for≈foreach(無key) >foreach(有key)>foreach(有key+引用)>foreach(無key+引用)>array\_walk(函數在外) >array\_walk(函數在內)?>array\_walk(引用)?>array\_map ~~~ foreach是最快的,因為php的array內部元素之間有指針,就是list,直接指向下個元素的內存地址,所以遍歷速度很快 for的問題在于,每次都要做一個hash table的查詢。 array_walk/array_map 遍歷的方式和foreach一樣的,慢是慢在回調函數 ~~~ ?但是如果在循環里有函數調用array\_walk的效率是最高的、 ~~~ $max = 1000000; $test_arr = range(0, $max); $temp=0; echo "<br>"; echo "<br>"; echo "<br>"; /*for 的方法*/ $t1 = microtime(true); for ($i = 0; $i < $max; $i++) { demo($i); } $t2 = microtime(true); $t = $t2 - $t1; echo "就使用for, 花費: {$t}<br>\n"; /*foreach*/ $t1 = microtime(true); foreach ($test_arr as $key => $value) { demo($value); } $t2 = microtime(true); $t = $t2 - $t1; echo "就使用foreach有key, 花費: {$t}<br>\n"; /*foreach*/ $t1 = microtime(true); foreach ($test_arr as $value) { demo($value); } $t2 = microtime(true); $t = $t2 - $t1; echo "就使用foreach無key, 花費: {$t}<br>\n"; /*foreach*/ $t1 = microtime(true); foreach ($test_arr as &$value) { demo($value); } $t2 = microtime(true); $t = $t2 - $t1; echo "就使用foreach無key+引用, 花費: {$t}<br>\n"; /*foreach*/ $t1 = microtime(true); foreach ($test_arr as $key => &$value) { demo($value); } $t2 = microtime(true); $t = $t2 - $t1; echo "就使用foreach有key+引用, 花費: {$t}<br>\n"; /*array_walk*/ $t1 = microtime(true); array_walk($test_arr ,function(&$itemValue,$key){ demo($itemValue); }); $t2 = microtime(true); $t = $t2 - $t1; echo "array_walk+引用, 花費: {$t}<br>\n"; /*array_walk*/ $t1 = microtime(true); array_walk($test_arr ,function($itemValue,$key){ demo($itemValue); }); $t2 = microtime(true); $t = $t2 - $t1; echo "array_walk, 花費: {$t}<br>\n"; /*array_walk*/ $t1 = microtime(true); array_walk($test_arr ,'arrayWalkCallBack'); $t2 = microtime(true); $t = $t2 - $t1; echo "array_walk 函數在外, 花費: {$t}<br>\n"; function demo($num) { $test=new stdClass(); $test->name='dash'; $test->age=28; $test->like='乒乓'; $test->code=$num; return $test; } function arrayWalkCallBack($itemValue,$key){ demo($itemValue); } ~~~ ### **array_map** array_map的主要作用是通過自定義函數對數組的值進行另外處理,另外可以做為對多個數組的相對應單元進行數據合并處理等。 * 1,array_map在調用的方法中取不到數組的鍵名,特別是只傳遞一個數組的時候,它完全是處理值。 * 2,只傳遞一個數組給array\_map最后得到的數組鍵名不變,但是如果傳遞多個數組則鍵名全都自動變成重新排列的數字索引。 * 3,回調函數是平行作用于相應的單元上的, 所以使用多個數組時,它們的長度應該相同。不然則最短的一個將被用空的單元擴充。 ~~~ $data =[ 226495 => '2018-12-25 10:47:47', 226519 => '2018-12-25 10:47:45', 226499 => '2018-12-25 10:47:44', 226604 => '2019-01-11 11:21:51', 226517 => '2018-12-25 10:47:36', 226630 => '2019-01-11 11:17:56', 226493 => '2018-12-25 10:47:34', 226502 => '2018-12-25 10:47:32', ]; $datas =[ 1=>'2018-12-25 10:47:47', 2=>'2018-12-25 10:47:45', 3=>'2018-12-25 10:47:44', 4=>'2019-01-11 11:21:51', 4=>'2018-12-25 10:47:36', 6=>'2019-01-11 11:17:56', 7=>'2018-12-25 10:47:34', 8=>'2018-12-25 10:47:32', ]; //調用array_map對單個數組值進行處理,索引能保持 $res1=array_map(function($val){ //只獲取時分秒 return explode(' ', $val)[1]; }, $data); var_export($res1); /* array ( 226495 => '10:47:47', 226519 => '10:47:45', 226499 => '10:47:44', 226604 => '11:21:51', 226517 => '10:47:36', 226630 => '11:17:56', 226493 => '10:47:34', 226502 => '10:47:32', ) */ //傳遞多個數組后返回的數組鍵名重新進行數字索引 $res2=array_map(function($value,$value2){ //只獲年月日 return current(explode(' ', $value)); }, $data, $datas); var_export($res2); /* array ( 0 => '2018-12-25', 1 => '2018-12-25', 2 => '2018-12-25', 3 => '2019-01-11', 4 => '2018-12-25', 5 => '2019-01-11', 6 => '2018-12-25', 7 => '2018-12-25', ) */ //傳遞多個數組進行數據重組處理 $res3=array_map(function($value,$value2){ return explode(' ', $value)[1].'=='.$value2; }, $data, $datas); var_export($res3); /**數據怎么錯亂了?? array ( 0 => '10:47:47==2018-12-25 10:47:47', 1 => '10:47:45==2018-12-25 10:47:45', 2 => '10:47:44==2018-12-25 10:47:44', 3 => '11:21:51==2018-12-25 10:47:36', 4 => '10:47:36==2019-01-11 11:17:56', 5 => '11:17:56==2018-12-25 10:47:34', 6 => '10:47:34==2018-12-25 10:47:32', 7 => '10:47:32==', ) / ~~~ 實戰:三個維度的數據合并 ``` //三個維度的數據合并 $names = array('zhangsan', 'lisi', 'wangwu'); $sex = array("man", "sex", "man"); $age = array(20, 25, 28); $users = array_map(null, $names, $sex, $age); print_r($users); //輸出結果如下: Array ( [0] => Array ( [0] => zhangsan [1] => 20 [2] => man ) [1] => Array ( [0] => lisi [1] => 25 [2] => sex ) [2] => Array ( [0] => wangwu [1] => 28 [2] => man ) ) ``` ### [**array_walk**()](https://www.php.net/manual/zh/function.array-walk.php)( array &$array , callable $funcname [, mixed $userdata = NULL ] ) - 使用用戶自定義函數對數組中的每個元素做回調處理 1, array\_walk同時能拿到數據的鍵和值 2,array\_walk并不返回新數組,只返回標識操作是否成功的布爾值 3,因為array\_walk不返回數組,array\_walk的使用更傾向于在原數組上進行數據修改處理,當然也可以在另外一個變量上處理。 ``` $names = array(1=>'zhangsan',2=> 'lisi',3=> 'wangwu'); //不修改原數組,直接顯示 $result = array_walk($names, function($value, $key){ echo '第',$key,'名:',$value,'<br>'; }); var_dump($result); /* 第1名:zhangsan 第2名:lisi 第3名:wangwu bool(true) */ //地址引用,直接修改 $result2 = array_walk($names, function(&$value, $key){ $value = '第'.$key.'名:'.$value; }); print_r($names); var_dump($result2); /* Array ( [1] => 第1名:zhangsan [2] => 第2名:lisi [3] => 第3名:wangwu ) bool(true) */ //地址引用,直接修改, 附帶其它數據 $result3 = array_walk($names, function(&$value, $key, $extra){ $value = $extra .$value; }, $extra="大家好,"); print_r($names); var_dump($result3); /* Array ( [1] => 大家好,第1名:zhangsan [2] => 大家好,第2名:lisi [3] => 大家好,第3名:wangwu ) bool(true) */ ``` **通過上面對array\_map和array\_walk的使用,綜合起來對比區別如下:** **1,array\_walk主要是針對單個數組進行處理,array\_map可以處理單個,也可以把多個數組結合一起處理。** **2,array\_map的作用更多的是得到一個新數組,array\_walk更多的是想做一些job, 只要結果執行成功了即行。** **3,對單個數組進行操作時,array\_map和array\_walk功能很像,區別在于array\_map是取得返回的數組;而array\_walk因為不返回所以通過地址引用在原數組上修改以實現“返回”。** **4,對多個數組處理時array\_map比array\_walk更適合。** ### [**array_?walk_?recursive**](https://www.php.net/manual/zh/function.array-walk-recursive.php "array_?walk_?recursive")($arr?,?`$callback`?\[,?[mixed](https://www.php.net/manual/zh/language.pseudo-types.php#language.types.mixed)?$userdata?=?NULL?])? 數組循環之遞歸的為每個數組元素調用callback 與array\_walk不同的是,array\_walk\_recursive函數會遞歸到更深層的數組中去,即對多維數組,array\_walk\_recursive會深入到更深層的數組鍵值中去處理。有一點需要要特別注意的是傳入給array\_walk\_recursive的數組參數中任何值為 array 的鍵都不會被傳遞到回調函數中去如: ``` $fruits = ['sweet' => ['a' => 'apple', 'b' => 'banana'], 'sour' => 'lemon']; array_walk_recursive($fruits, function($item, $key){ echo "$key => $item\n"; }); /**注意上例中的鍵 'sweet' 并沒有顯示出來。任何其值為 array 的鍵都不會被傳遞到回調函數中去 就像這里sweet下標對應的值位數組,所以這里不會返回 a => apple b => banana sour => lemon **/ ``` **示例:多維變一維** ``` $array=[ 1=>[2,5=>[4,2],[7,8=>[3,6]],5], 2=>4 ]; var_export($array); $arr=[]; array_walk_recursive($array, function($k){ global $arr; $arr[]=$k; }); print_r($arr);//Array ( [0] => 2 [1] => 4 [2] => 2 [3] => 7 [4] => 3 [5] => 6 [6] => 5 [7] => 4 ) ``` **額外參數:** ``` $fruits = ['sweet' => ['a' => 'apple', 'b' => 'banana'], 'sour' => 'lemon']; $extra1=1; $extra2=2; array_walk_recursive($fruits, function($item, $key, $extra){ echo "$key => $item\n $extra[0]<==>$extra[1]\n"; }, [$extra1,$extra2]); ``` **外部變量持久化** ``` $arr = [ 'one' => ['one_one' => 'A', 'one_two' => 'B'], 'two' => 2 ]; $counter = 0; //默認情況下,外部變量在回調函數中保存不了的 array_walk_recursive( $arr, function($value, $key, $counter) { $counter++; echo $value." :". $counter"; }, $counter); echo "counter :". $counter; /** 結果: A : 1 B : 1 2 : 1 counter : 0 **/ //僅在相同的數組節點中持久存在使用引用 array_walk_recursive( $arr, function($value, $key, &$counter) { $counter++; echo $value." :". $counter"; }, $counter); echo "counter :". $counter; /** 結果: A : 1 B : 2 2 : 1 counter : 0 **/ //完全持久的化使用"use"關鍵字 array_walk_recursive( $arr, function($value, $key) use (&$counter) { $counter++; echo $value." :". $counter"; }, $counter); echo "counter :". $counter; /** 結果: A : 1 B : 2 2 : 3 counter : 3 **/ ``` ## 所有鍵名轉小寫或者大寫 ### array_?change_?key_?case($arr , CASE_LOWER | CASE_UPPER): ## [返回指定【下標】的值組成的新數組(返回的值還可以指定下標)](https://www.php.net/manual/zh/function.array-column.php "array_?column") [array\_?column($arr, $key=null ,$newArrKey=null);//返回指定【下標key】的值組成的新數組(第三個參數指定返回值的下標)](https://www.php.net/manual/zh/function.array-column.php "array_?column") ``` array_column ($arr, null ,null);//返回指定【下標】的值(返回的值還可以指定下標) /* $arr=[ [ 'id' => 21, 'first_name' => 'John', 'last_name' => 'Doe', ], [ 'id' => 35, 'first_name' => 'John', 'last_name' => 'Doe', [ 'id'=>'50' ] ] ] array_column($records, 'first_name',null); $arr=[ [John,Peter] ]; array_column($records, 'first_name','id'); $arr=[ [21=>John,35=>Peter] ]; array_column($records, null,'id'); $arr=[ 21=>[ 'id' => 21, 'first_name' => 'John', 'last_name' => 'Doe', ], 35=>[ 'id' => 35, 'first_name' => 'John', 'last_name' => 'Doe', [ 'id'=>'50' ] ] ] */ ``` ## [array\_?count\_?values](https://www.php.net/manual/zh/function.array-count-values.php "array_?count_?values")($arr)? ?計算數組中每個值出現的次數 返回原數組的值為鍵 出現次數為值的數組 ## [array\_?fill\_?keys($keysArr, $mixed\_value);](https://www.php.net/manual/zh/function.array-fill-keys.php "array_?fill_?keys")? 用戶指定鍵和值來填充數組 ~~~ $keys = array('foo', 5, 10, 'bar');//第一個參數數組的值作為填充數組的鍵,第二個參數作為值 array_fill_keys($keys, 'banana'); //['foo'=>'banana',5=>'banana',10=>'banana','bar'=>'banana'] $keys = array('foo', 5); $value=array('banana','apple'); $a=array_fill_keys($keys,$value); //['foo'=>['banana','apple'],5=>['banana','apple']] ~~~ ## [array\_?fill](https://www.php.net/manual/zh/function.array-fill.php "array_?fill")($start\_index , $num(超過這個給定數組的長度就不填充了) ,$mixed\_value);給定的值填充數組 [array\_?flip](https://www.php.net/manual/zh/function.array-flip.php "array_?flip") [array\_?intersect\_?assoc](https://www.php.net/manual/zh/function.array-intersect-assoc.php "array_?intersect_?assoc") [array\_?intersect\_?key](https://www.php.net/manual/zh/function.array-intersect-key.php "array_?intersect_?key") [array\_?intersect\_?uassoc](https://www.php.net/manual/zh/function.array-intersect-uassoc.php "array_?intersect_?uassoc") [array\_?intersect\_?ukey](https://www.php.net/manual/zh/function.array-intersect-ukey.php "array_?intersect_?ukey") [array\_?intersect](https://www.php.net/manual/zh/function.array-intersect.php "array_?intersect") [array\_?key\_?exists](https://www.php.net/manual/zh/function.array-key-exists.php "array_?key_?exists") [array\_?key\_?first](https://www.php.net/manual/zh/function.array-key-first.php "array_?key_?first") [array\_?key\_?last](https://www.php.net/manual/zh/function.array-key-last.php "array_?key_?last") [array\_?keys](https://www.php.net/manual/zh/function.array-keys.php "array_?keys") [array\_?map](https://www.php.net/manual/zh/function.array-map.php "array_?map") [array\_?merge\_?recursive](https://www.php.net/manual/zh/function.array-merge-recursive.php "array_?merge_?recursive") [array\_?merge](https://www.php.net/manual/zh/function.array-merge.php "array_?merge") [array\_?pad](https://www.php.net/manual/zh/function.array-pad.php "array_?pad") [array\_?pop](https://www.php.net/manual/zh/function.array-pop.php "array_?pop") [array\_?product](https://www.php.net/manual/zh/function.array-product.php "array_?product") [array\_?push](https://www.php.net/manual/zh/function.array-push.php "array_?push") [array\_?rand](https://www.php.net/manual/zh/function.array-rand.php "array_?rand") [array\_?reduce](https://www.php.net/manual/zh/function.array-reduce.php "array_?reduce") [array\_?replace\_?recursive](https://www.php.net/manual/zh/function.array-replace-recursive.php "array_?replace_?recursive") [array\_?replace](https://www.php.net/manual/zh/function.array-replace.php "array_?replace") [array\_?reverse](https://www.php.net/manual/zh/function.array-reverse.php "array_?reverse") [array\_?search](https://www.php.net/manual/zh/function.array-search.php "array_?search") [array\_?shift](https://www.php.net/manual/zh/function.array-shift.php "array_?shift") [array\_?slice](https://www.php.net/manual/zh/function.array-slice.php "array_?slice") [array\_?splice](https://www.php.net/manual/zh/function.array-splice.php "array_?splice") [array\_?sum](https://www.php.net/manual/zh/function.array-sum.php "array_?sum") [array\_?udiff\_?assoc](https://www.php.net/manual/zh/function.array-udiff-assoc.php "array_?udiff_?assoc") [array\_?udiff\_?uassoc](https://www.php.net/manual/zh/function.array-udiff-uassoc.php "array_?udiff_?uassoc") [array\_?udiff](https://www.php.net/manual/zh/function.array-udiff.php "array_?udiff") [array\_?uintersect\_?assoc](https://www.php.net/manual/zh/function.array-uintersect-assoc.php "array_?uintersect_?assoc") [array\_?uintersect\_?uassoc](https://www.php.net/manual/zh/function.array-uintersect-uassoc.php "array_?uintersect_?uassoc") [array\_?uintersect](https://www.php.net/manual/zh/function.array-uintersect.php "array_?uintersect") [array\_?unique](https://www.php.net/manual/zh/function.array-unique.php "array_?unique") [array\_?unshift](https://www.php.net/manual/zh/function.array-unshift.php "array_?unshift") [array\_?values](https://www.php.net/manual/zh/function.array-values.php "array_?values") [array\_?walk\_?recursive](https://www.php.net/manual/zh/function.array-walk-recursive.php "array_?walk_?recursive") [array\_?walk](https://www.php.net/manual/zh/function.array-walk.php "array_?walk") [array](https://www.php.net/manual/zh/function.array.php "array") [arsort](https://www.php.net/manual/zh/function.arsort.php "arsort") [asort](https://www.php.net/manual/zh/function.asort.php "asort") [compact](https://www.php.net/manual/zh/function.compact.php "compact") [count](https://www.php.net/manual/zh/function.count.php "count") [current](https://www.php.net/manual/zh/function.current.php "current") [end](https://www.php.net/manual/zh/function.end.php "end") [extract](https://www.php.net/manual/zh/function.extract.php "extract") [in\_?array](https://www.php.net/manual/zh/function.in-array.php "in_?array") [key\_?exists](https://www.php.net/manual/zh/function.key-exists.php "key_?exists") [key](https://www.php.net/manual/zh/function.key.php "key") [krsort](https://www.php.net/manual/zh/function.krsort.php "krsort") [ksort](https://www.php.net/manual/zh/function.ksort.php "ksort") [list](https://www.php.net/manual/zh/function.list.php "list") [natcasesort](https://www.php.net/manual/zh/function.natcasesort.php "natcasesort") [natsort](https://www.php.net/manual/zh/function.natsort.php "natsort") [next](https://www.php.net/manual/zh/function.next.php "next") [pos](https://www.php.net/manual/zh/function.pos.php "pos") [prev](https://www.php.net/manual/zh/function.prev.php "prev") [range](https://www.php.net/manual/zh/function.range.php "range") [reset](https://www.php.net/manual/zh/function.reset.php "reset") [rsort](https://www.php.net/manual/zh/function.rsort.php "rsort") [shuffle](https://www.php.net/manual/zh/function.shuffle.php "shuffle") [sizeof](https://www.php.net/manual/zh/function.sizeof.php "sizeof") [sort](https://www.php.net/manual/zh/function.sort.php "sort") [uasort](https://www.php.net/manual/zh/function.uasort.php "uasort") [uksort](https://www.php.net/manual/zh/function.uksort.php "uksort") [usort](https://www.php.net/manual/zh/function.usort.php "usort") Deprecated [each](https://www.php.net/manual/zh/function.each.php "each") 檢測方法是否存在:method\_exists() 檢測方法是否存在且(外部)可調用:is\_callable() ~~~ class Test { public function PublicMethod(){} protected function ProtectedMethod(){} private function PrivateMethod(){} public static function PublicStaticMethod(){} protected static function ProtectedStaticMethod(){} private static function PrivateStaticMethod(){} } $test = new Test(); $callbacks = array( array($test, 'PublicMethod'),//true array($test, 'ProtectedMethod'),//false array($test, 'PrivateMethod'),//false array($test, 'PublicStaticMethod'),//true array($test, 'ProtectedStaticMethod'),//false array($test, 'PrivateStaticMethod'),//false array('Test', 'PublicMethod'),//true array('Test', 'ProtectedMethod'),//false array('Test', 'PrivateMethod'),//false array('Test', 'PublicStaticMethod'),//true array('Test', 'ProtectedStaticMethod'),//false array('Test', 'PrivateStaticMethod'),//false ); foreach ($callbacks as $callback){ //var_dump($callback); echo'is_callable:'; var_dump(is_callable($callback)); } class Test { public function PublicMethod(){} protected function ProtectedMethod(){} private function PrivateMethod(){} public static function PublicStaticMethod(){} protected static function ProtectedStaticMethod(){} private static function PrivateStaticMethod(){} } $test = new Test(); $callbacks = array( array($test, 'PublicMethod'),//true array($test, 'ProtectedMethod'),//true array($test, 'PrivateMethod'),//true array($test, 'PublicStaticMethod'),//true array($test, 'ProtectedStaticMethod'),//true array($test, 'PrivateStaticMethod'),//true array('Test', 'PublicMethod'),//true array('Test', 'ProtectedMethod'),//true array('Test', 'PrivateMethod'),//true array('Test', 'PublicStaticMethod'),//true array('Test', 'ProtectedStaticMethod'),//true array('Test', 'PrivateStaticMethod'),//true ); foreach ($callbacks as $callback){ //var_dump($callback); echo'method_exists:'; var_dump(method_exists($callback[0], $callback[1])); } ~~~ 類或對象的相關函數 [\_?\_?autoload](https://www.php.net/manual/zh/function.autoload.php "_?_?autoload") [class\_?alias](https://www.php.net/manual/zh/function.class-alias.php "class_?alias") [class\_?exists](https://www.php.net/manual/zh/function.class-exists.php "class_?exists") [get\_?called\_?class](https://www.php.net/manual/zh/function.get-called-class.php "get_?called_?class") [get\_?class\_?methods](https://www.php.net/manual/zh/function.get-class-methods.php "get_?class_?methods") [get\_?class\_?vars](https://www.php.net/manual/zh/function.get-class-vars.php "get_?class_?vars") [get\_?class](https://www.php.net/manual/zh/function.get-class.php "get_?class") [get\_?declared\_?classes](https://www.php.net/manual/zh/function.get-declared-classes.php "get_?declared_?classes") [get\_?declared\_?interfaces](https://www.php.net/manual/zh/function.get-declared-interfaces.php "get_?declared_?interfaces") [get\_?declared\_?traits](https://www.php.net/manual/zh/function.get-declared-traits.php "get_?declared_?traits") [get\_?object\_?vars](https://www.php.net/manual/zh/function.get-object-vars.php "get_?object_?vars") [get\_?parent\_?class](https://www.php.net/manual/zh/function.get-parent-class.php "get_?parent_?class") [interface\_?exists](https://www.php.net/manual/zh/function.interface-exists.php "interface_?exists") [is\_?a](https://www.php.net/manual/zh/function.is-a.php "is_?a") [is\_?subclass\_?of](https://www.php.net/manual/zh/function.is-subclass-of.php "is_?subclass_?of") [method\_?exists( $className\_str|`$object`? ,`$methodName_str`?)](https://www.php.net/manual/zh/function.method-exists.php "method_?exists")? ?檢查類的方法是否存在 [property\_?exists](https://www.php.net/manual/zh/function.property-exists.php "property_?exists") [trait\_?exists](https://www.php.net/manual/zh/function.trait-exists.php "trait_?exists") Deprecated [call\_?user\_?method\_?array](https://www.php.net/manual/zh/function.call-user-method-array.php "call_?user_?method_?array") [call\_?user\_?method](https://www.php.net/manual/zh/function.call-user-method.php "call_?user_?method") **為什么我在點擊瀏覽器的后退按鈕后,所有字段的信息都被清空了?** 提交表單的頁面使用了session\_start();該函數會強制當前頁面不被緩存,表單提交錯誤數據時后在返回提交頁面表單使內容被清空,要想保留表單的數據怎么辦? 第一種方法:使用session\_cache\_limiter('private, must-revalidate')函數,但是要值得注意的是session\_cache\_limiter()方法要寫在session\_start()方法之前才有用; 第二種方法:header('Cache-control: private, must-revalidate');//支持頁面回跳;在session\_start()方法之后用 \_\_DIR\_\_和dirname(\_\_FILE\_\_);都是獲取當前腳本所在目錄的\_\_DIR\_\_效率高但是實在php5.3加入的所以用時需要注意
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看