PHP 陣列的一些高階用法

2021-08-02 20:44:50 字數 633 閱讀 9923

使用 php 原生的陣列函式,不要看見陣列就使用 foreach , 能有效降低程式的複雜度,精簡**量。

過濾組元素

$input = [ 'a' => '', 'b' => '', 'c' => ''];

$allowed = ['a', 'b'];

$data = array_insert_key( $input , array_flib( $allowed ) );

將資料庫獲取的多條資料的主鍵作為陣列的key

$sql

=" select id, name from user; ";

$raw_data

=$pdo

-> execute( $sql );

$data

= array_combine( array_column( $raw_data, 'id'),$raw_data);

將資料中錯亂的key 還原成一般的 0..n

array_values($input);

oracle 一些高階用法

隨機返回 5條記錄,的時候挺好用。select from select a.prod id,a.prod name fromtb prod mas a order bydbms random.value where rownum 5 查詢跳過錶中的偶數行 select x.from selectro...

Redis 一些高階用法

redis 過期時,會向特定的訊息佇列傳送訊息,監聽該訊息佇列 優點 缺點 sorted set,用時間戳作為 score,member 為你想處理的資料內容 優點 缺點 tip 刪除返回行數為 0 時,可以認為已經被別人處理 同時,可以 sleep 一些時間,使得不同伺服器執行定時任務的時間錯開,...

Sed的一些高階用法(一)

1.刪除指定的空白行,單行next 現在有乙個檔案中有有空白行,但是需要刪除指定的空白行,並不是全部,檔案內容如下 1 this is the header line 3 this is the data line 5 this is the last line需求 需要刪除第一行和第三行之間的空白...