多次元配列ソート

多次元配列

処理内容

 閲覧数:867  投稿日:2019-01-13  更新日:2019-01-13  

「order_no」で昇順ソート。
「counter_at」で降順ソート。
但し、「counter_at」は入力間違いのため存在しない


コード

foreach ($categoryList as  $key => $val){
  $sort_key1[$key] = $val['order_no'];
  $sort_key2[$key] = $val['counter_at'];
  array_multisort($sort_key1, SORT_ASC, $sort_key2, SORT_DESC, $categoryList);
}



結果

(
    [0] => Array
        (
            [category_id] => 5
            [category] => 初めに
            [parent_id] => 0
            [cnt] => 1
            [parent_category] => 初めに
            [order_no] => 1
            [cntall] => 1
        )

    [1] => Array
        (
            [category_id] => 1
            [category] => 法律
            [parent_id] => 0
            [cnt] => 1
            [parent_category] => 法律
            [order_no] => 2
            [cntall] => 1
        )

    [2] => Array
        (
            [category_id] => 2
            [category] => 決済サービス
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 決済サービス
            [order_no] => 2
            [cntall] => 12
        )

    [3] => Array
        (
            [category_id] => 18
            [category] => 集合知
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 集合知
            [order_no] => 2
            [cntall] => 0
        )

    [4] => Array
        (
            [category_id] => 19
            [category] => 数学
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 数学
            [order_no] => 2
            [cntall] => 0
        )

    [5] => Array
        (
            [category_id] => 20
            [category] => 有料会員制のオンラインサロンサービス
            [parent_id] => 21
            [cnt] => 1
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 2
            [cntall] => 1
        )

    [6] => Array
        (
            [category_id] => 21
            [category] => 課金販売できるプラットフォーム
            [parent_id] => 0
            [cnt] => 1
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 2
            [cntall] => 8
        )

    [7] => Array
        (
            [category_id] => 22
            [category] => デジタルコンテンツ販売可能なサービス
            [parent_id] => 21
            [cnt] => 2
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 2
            [cntall] => 2
        )

    [8] => Array
        (
            [category_id] => 23
            [category] => UML
            [parent_id] => 1
            [cnt] => 0
            [parent_category] => 法律
            [order_no] => 2
            [cntall] => 0
        )

    [9] => Array
        (
            [category_id] => 24
            [category] => 出力バッファリング制御
            [parent_id] => 7
            [cnt] => 0
            [parent_category] => その他
            [order_no] => 2
            [cntall] => 0
        )

    [10] => Array
        (
            [category_id] => 25
            [category] => 機械学習
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 機械学習
            [order_no] => 2
            [cntall] => 0
        )

    [11] => Array
        (
            [category_id] => 26
            [category] => パッケージ管理
            [parent_id] => 1
            [cnt] => 0
            [parent_category] => 法律
            [order_no] => 2
            [cntall] => 0
        )

    [12] => Array
        (
            [category_id] => 27
            [category] => サーバ
            [parent_id] => 5
            [cnt] => 0
            [parent_category] => 初めに
            [order_no] => 2
            [cntall] => 0
        )

    [13] => Array
        (
            [category_id] => 28
            [category] =>  HTTPクライアント
            [parent_id] => 5
            [cnt] => 0
            [parent_category] => 初めに
            [order_no] => 2
            [cntall] => 0
        )

    [14] => Array
        (
            [category_id] => 29
            [category] => 「支払、出金」機能を有する決済系サービス表
            [parent_id] => 2
            [cnt] => 1
            [parent_category] => 決済サービス
            [order_no] => 2
            [cntall] => 1
        )

    [15] => Array
        (
            [category_id] => 30
            [category] => 画像
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 画像
            [order_no] => 2
            [cntall] => 0
        )

    [16] => Array
        (
            [category_id] => 31
            [category] => デジタル回路
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => デジタル回路
            [order_no] => 2
            [cntall] => 0
        )

    [17] => Array
        (
            [category_id] => 32
            [category] => Linux
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => Linux
            [order_no] => 2
            [cntall] => 0
        )

    [18] => Array
        (
            [category_id] => 33
            [category] => デーモン
            [parent_id] => 32
            [cnt] => 0
            [parent_category] => Linux
            [order_no] => 2
            [cntall] => 0
        )

    [19] => Array
        (
            [category_id] => 34
            [category] => スクリプト
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => スクリプト
            [order_no] => 2
            [cntall] => 0
        )

    [20] => Array
        (
            [category_id] => 35
            [category] => シェルスクリプト
            [parent_id] => 34
            [cnt] => 0
            [parent_category] => スクリプト
            [order_no] => 2
            [cntall] => 0
        )

    [21] => Array
        (
            [category_id] => 36
            [category] => クローラ
            [parent_id] => 37
            [cnt] => 0
            [parent_category] => 検索エンジン
            [order_no] => 2
            [cntall] => 0
        )

    [22] => Array
        (
            [category_id] => 37
            [category] => 検索エンジン
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 検索エンジン
            [order_no] => 2
            [cntall] => 0
        )

    [23] => Array
        (
            [category_id] => 38
            [category] => 全文検索エンジン
            [parent_id] => 37
            [cnt] => 0
            [parent_category] => 検索エンジン
            [order_no] => 2
            [cntall] => 0
        )

    [24] => Array
        (
            [category_id] => 39
            [category] => 形態素解析
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 形態素解析
            [order_no] => 2
            [cntall] => 0
        )

    [25] => Array
        (
            [category_id] => 40
            [category] => C言語
            [parent_id] => 41
            [cnt] => 0
            [parent_category] => 言語
            [order_no] => 2
            [cntall] => 0
        )

    [26] => Array
        (
            [category_id] => 41
            [category] => 言語
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 言語
            [order_no] => 2
            [cntall] => 0
        )

    [27] => Array
        (
            [category_id] => 42
            [category] => Markdown
            [parent_id] => 41
            [cnt] => 0
            [parent_category] => 言語
            [order_no] => 2
            [cntall] => 0
        )

    [28] => Array
        (
            [category_id] => 43
            [category] => 決済サービス比較
            [parent_id] => 2
            [cnt] => 1
            [parent_category] => 決済サービス
            [order_no] => 2
            [cntall] => 1
        )

    [29] => Array
        (
            [category_id] => 44
            [category] => 設計
            [parent_id] => 1
            [cnt] => 0
            [parent_category] => 法律
            [order_no] => 2
            [cntall] => 0
        )

    [30] => Array
        (
            [category_id] => 45
            [category] => データ圧縮
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => データ圧縮
            [order_no] => 2
            [cntall] => 0
        )

    [31] => Array
        (
            [category_id] => 46
            [category] => 単位
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 単位
            [order_no] => 2
            [cntall] => 0
        )

    [32] => Array
        (
            [category_id] => 47
            [category] => 決済
            [parent_id] => 8
            [cnt] => 5
            [parent_category] => Webサービスビジネス
            [order_no] => 2
            [cntall] => 5
        )

    [33] => Array
        (
            [category_id] => 48
            [category] => 装置
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 装置
            [order_no] => 2
            [cntall] => 0
        )

    [34] => Array
        (
            [category_id] => 49
            [category] => API
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => API
            [order_no] => 2
            [cntall] => 0
        )

    [35] => Array
        (
            [category_id] => 50
            [category] => HTTP
            [parent_id] => 5
            [cnt] => 0
            [parent_category] => 初めに
            [order_no] => 2
            [cntall] => 0
        )

    [36] => Array
        (
            [category_id] => 51
            [category] => 正規表現
            [parent_id] => 2
            [cnt] => 0
            [parent_category] => 決済サービス
            [order_no] => 2
            [cntall] => 0
        )

    [37] => Array
        (
            [category_id] => 52
            [category] => OS
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => OS
            [order_no] => 2
            [cntall] => 0
        )

    [38] => Array
        (
            [category_id] => 53
            [category] => 認証プロトコル
            [parent_id] => 49
            [cnt] => 0
            [parent_category] => API
            [order_no] => 2
            [cntall] => 0
        )

    [39] => Array
        (
            [category_id] => 54
            [category] => 課金
            [parent_id] => 0
            [cnt] => 9
            [parent_category] => 課金
            [order_no] => 2
            [cntall] => 9
        )

    [40] => Array
        (
            [category_id] => 55
            [category] => 商取引
            [parent_id] => 0
            [cnt] => 1
            [parent_category] => 商取引
            [order_no] => 2
            [cntall] => 1
        )

    [41] => Array
        (
            [category_id] => 9
            [category] => クラウドファンディング
            [parent_id] => 21
            [cnt] => 2
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 4
            [cntall] => 2
        )

    [42] => Array
        (
            [category_id] => 4
            [category] => software
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => software
            [order_no] => 5
            [cntall] => 0
        )

    [43] => Array
        (
            [category_id] => 3
            [category] => リリース
            [parent_id] => 1
            [cnt] => 0
            [parent_category] => 法律
            [order_no] => 999
            [cntall] => 0
        )

    [44] => Array
        (
            [category_id] => 6
            [category] => 「チャージ、支払、送金」機能を有する決済系サービス
            [parent_id] => 2
            [cnt] => 1
            [parent_category] => 決済サービス
            [order_no] => 999
            [cntall] => 1
        )

    [45] => Array
        (
            [category_id] => 7
            [category] => その他
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => その他
            [order_no] => 999
            [cntall] => 0
        )

    [46] => Array
        (
            [category_id] => 8
            [category] => Webサービスビジネス
            [parent_id] => 0
            [cnt] => 2
            [parent_category] => Webサービスビジネス
            [order_no] => 999
            [cntall] => 7
        )

    [47] => Array
        (
            [category_id] => 10
            [category] => ライブ配信サービス
            [parent_id] => 21
            [cnt] => 1
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 999
            [cntall] => 1
        )

    [48] => Array
        (
            [category_id] => 11
            [category] => テスト
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => テスト
            [order_no] => 999
            [cntall] => 0
        )

    [49] => Array
        (
            [category_id] => 12
            [category] => セキュリティ
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => セキュリティ
            [order_no] => 999
            [cntall] => 0
        )

    [50] => Array
        (
            [category_id] => 13
            [category] => クリエイター支援プラットフォーム
            [parent_id] => 21
            [cnt] => 1
            [parent_category] => 課金販売できるプラットフォーム
            [order_no] => 999
            [cntall] => 1
        )

    [51] => Array
        (
            [category_id] => 14
            [category] => 「支払」機能を有する決済系サービス
            [parent_id] => 2
            [cnt] => 6
            [parent_category] => 決済サービス
            [order_no] => 999
            [cntall] => 6
        )

    [52] => Array
        (
            [category_id] => 15
            [category] => バージョン管理システム
            [parent_id] => 1
            [cnt] => 0
            [parent_category] => 法律
            [order_no] => 999
            [cntall] => 0
        )

    [53] => Array
        (
            [category_id] => 16
            [category] => 「チャージ、支払、送金、出金」機能を有する決済系サービス
            [parent_id] => 2
            [cnt] => 3
            [parent_category] => 決済サービス
            [order_no] => 999
            [cntall] => 3
        )

    [54] => Array
        (
            [category_id] => 17
            [category] => 統計
            [parent_id] => 0
            [cnt] => 0
            [parent_category] => 統計
            [order_no] => 999
            [cntall] => 0
        )

)



タグ


array_multisort 





再帰処理で平坦な連想配列をツリー化

(データベースなどから取得した)多次元配列の構造を変換して一次元配列を返す



週間人気ページランキング / 11-16 → 11-22
順位 ページタイトル抜粋 アクセス数
1 配列キー内に「指定文字列が含まれるキー」と「その値」を抽出して、新しい配列を返す | 配列(型) 8
1 後ろから3文字削除 / 「18:00:00」→「18:00」 | 文字列(テキスト処理) 8
2 URLから、トップページ(index.html)判定 | ルーティング 6
3 指定文字列より前を取得 | 文字列(テキスト処理) 5
4 配列の内容(ひらがな)を、読み(あ行~わ行)で分け、新たな配列へ格納 | 配列(型) 4
4 MySQLのdatetime型カラムに格納している値と比較して、24時間以内かどうかを判定 | 日付および時刻関連 4
5 現在WebページのURLパスを第2階層まで取得 | パス 3
5 対象文字列の内、「最初の指定文字列以前」と「それ以降の文字列」を取得する | 文字列(テキスト処理) 3
5 2 つの配列(文字列とdatetime)が要素順で 対応している時、日付を整形して 新しい配列を作成 | 配列(型) 3
6 再帰処理で平坦な連想配列をツリー化 | 多次元配列(型) 2
6 「1週間後の年月日時分秒」を表示 … time() | 日付および時刻関連 2
6 投稿日時をTwitterのように「★分前」「★時間前」という文字列変換するユーザ定義PHP関数 | 日付および時刻関連 2
6 配列要素を、文字列連結して表示(元配列を上書) … foreach文+implode | 配列(型) 2
6 8桁の生年月日数字から、年齢を計算 | 日付および時刻関連 2
6 null代入とunset()の違い | 変数 2
6 PHPで最後の「指定区切り文字」より後ろを取得 | 配列(型) 2
7 配列定数同士をarray_mergeで後ろに単純連結(キーは新たに振り直す) | 定数 1
7 gethostbyaddr / IPアドレスからホスト名を取得 | ネットワーク 関数 1
7 マークダウンで書かれたURLを(エスケープ処理するのではなく)削除する | テキスト処理 1
7 対象文字列を、文字数に応じて半分に分割後、それぞれを取得 | 文字列(テキスト処理) 1
2024/11/23 1:02 更新