简体   繁体   中英

How to sort multidimensional array in PHP

I have tried almost all suggestions online, and I really can't figure out how to sort this SESSION array. I want it sorted by "itemname"..

I have and want this output, but I need the itemname to determine which item array come first.

Array
(
[person] => Array
    (
        [namex] => Array
            (
                [itema] => Array
                    (
                        [itemid] => 43
                        [itemname] => def
                    )

                [itemc] => Array
                    (
                        [itemid] => 33
                        [itemname] => abc
                    )

                [itemg] => Array
                    (
                        [itemid] => 29
                        [itemname] => ghi
                    )

            )

        [namey] => Array
            (
                [itemj] => Array
                    (
                        [itemid] => 12
                        [itemname] => abc
                    )

                [iteme] => Array
                    (
                        [itemid] => 44
                        [itemname] => jkl
                    )

                [itemr] => Array
                    (
                        [itemid] => 20
                        [itemname] => rst
                    )

            )

    )

)

So, "person" stays the same, but name, item, itemid and itemname is always different.

Can anyone help me sort this by itemname? I need the array to be like this, so can't change it up.

Also.. I need to access it later in a foreach, so I can print out the items.

As pointed out in the comments to the question the desired output is not really defined. You'd have to give a specific definition of the output, without that we have to assume what you probably are looking for:

<?php
$data = [
  "person" => [
    "namex" => [
      "itema" => [
        "itemid" => 43,
        "itemname" => "def"
      ],
      "itemc" => [
        "itemid" => 33,
        "itemname" => "abc"
      ],
      "itemg" => [
        "itemid" => 29,
        "itemname" => "ghi"
      ]
    ],
    "namey" => [
      "itemj" => [
        "itemid" => 12,
        "itemname" => "abc"
      ],
      "iteme" => [
        "itemid" => 44,
        "itemname" => "jkl"
      ],
      "itemr" => [
        "itemid" => 20,
        "itemname" => "rst"
      ]
    ]
  ]
];
foreach ($data["person"] as $personName => &$person) {
  uasort($person, function($a, $b) {
    return $a["itemname"] > $b["itemname"];
  });
}
print_r($data);

The obvious output is:

Array
(
    [person] => Array
        (
            [namex] => Array
                (
                    [itemc] => Array
                        (
                            [itemid] => 33
                            [itemname] => abc
                        )
                    [itema] => Array
                        (
                            [itemid] => 43
                            [itemname] => def
                        )
                    [itemg] => Array
                        (
                            [itemid] => 29
                            [itemname] => ghi
                        )
                )
            [namey] => Array
                (
                    [itemj] => Array
                        (
                            [itemid] => 12
                            [itemname] => abc
                        )
                    [iteme] => Array
                        (
                            [itemid] => 44
                            [itemname] => jkl
                        )
                    [itemr] => Array
                        (
                            [itemid] => 20
                            [itemname] => rst
                        )
                )
        )
)

Assuming the input from the example is represented via array as:

<?php
$array = [
    'person' => [
        'namex' => [
            'itema' => [
                'itemid' => 43,
                'itemname' => 'def'
            ],
            'itemb' => [
                'itemid' => 33,
                'itemname' => 'abc'
            ],
            'itemc' => [
                'itemid' => 29,
                'itemname' => 'ghi'
            ],
        ],
        'namey' => [
            'itema' => [
                'itemid' => 12,
                'itemname' => 'abc'
            ],
            'itemb' => [
                'itemid' => 44,
                'itemname' => 'jkl'
            ],
            'itemc' => [
                'itemid' => 20,
                'itemname' => 'rst'
            ],
        ],
    ]
];


You could do use array_multisort in a loop:

$ids = [];
foreach($array as $key => $value) {
    foreach ($value as $newKey => $value2) {
        $ids[$newKey] = array_column($value2, 'itemname');
        array_multisort($ids[$newKey], SORT_NATURAL, $array[$key][$newKey]);
    }
}

This will output

array(1) {
  'person' =>
  array(2) {
    'namex' =>
    array(3) {
      'itemb' =>
      array(2) {
        'itemid' =>
        int(33)
        'itemname' =>
        string(3) "abc"
      }
      'itema' =>
      array(2) {
        'itemid' =>
        int(43)
        'itemname' =>
        string(3) "def"
      }
      'itemc' =>
      array(2) {
        'itemid' =>
        int(29)
        'itemname' =>
        string(3) "ghi"
      }
    }
    'namey' =>
    array(3) {
      'itema' =>
      array(2) {
        'itemid' =>
        int(12)
        'itemname' =>
        string(3) "abc"
      }
      'itemb' =>
      array(2) {
        'itemid' =>
        int(44)
        'itemname' =>
        string(3) "jkl"
      }
      'itemc' =>
      array(2) {
        'itemid' =>
        int(20)
        'itemname' =>
        string(3) "rst"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM