简体   繁体   中英

echo a multi-dimensional associative array into lists based on key value

I would like to sort and echo the following array into separate lists based on which itemtype they are.

Array
(
    [0] => Array
    (
        [itemid] => 1
        [itemname] => Sword
        [itemtype] => Standard
    )

    [1] => Array
    (
        [itemid] => 2
        [itemname] => Dagger
        [itemtype] => Standard
    )

     [2] => Array
    (
        [itemid] => 3
        [itemname] => Backpack
        [itemtype] => Standard
    )

    [3] => Array
    (
        [itemid] => 4
        [itemname] => Rope (50ft)
        [itemtype] => Standard
    )

    [4] => Array
    (
        [itemid] => 5
        [itemname] => Tinderbox
        [itemtype] => Standard
    )

    [5] => Array
    (
        [itemid] => 6
        [itemname] => Torch
        [itemtype] => Standard
    )

    [6] => Array
    (
        [itemid] => 6
        [itemname] => Torch
        [itemtype] => Standard
    )

    [7] => Array
    (
        [itemid] => 6
        [itemname] => Torch
        [itemtype] => Standard
    )

    [8] => Array
    (
        [itemid] => 6
        [itemname] => Torch
        [itemtype] => Standard
    )

    [9] => Array
    (
        [itemid] => 6
        [itemname] => Torch
        [itemtype] => Standard
    )

    [10] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [11] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [12] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [13] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [14] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [15] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [16] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [17] => Array
    (
        [itemid] => 8
        [itemname] => Bread
        [itemtype] => Provisions
    )

    [18] => Array
    (
        [itemid] => 9
        [itemname] => Healing Salve
        [itemtype] => Magical
    )

    [19] => Array
    (
        [itemid] => 9
        [itemname] => Healing Salve
        [itemtype] => Magical
    )

)

I would like to echo this into lists based on the itemtype key. ie:

Standard
=========
Sword Dagger Backpack Rope (50ft) Tinderbox Torch x5

Provisions
=========
Bread x8

Magical
=========
Healing Salve

I tried doing it like this to no avail:

    foreach ($array as $value) {
        if ($value['itemtype'] == 'Standard'){
            echo $value['itemname'] . "\n";
        }
    foreach ($array as $value) {
        if ($value['itemtype'] == 'Provisions'){
            echo $value['itemname'] . "\n";
        }
    foreach ($array as $value) {
        if ($value['itemtype'] == 'Magical'){
            echo $value['itemname'] . "\n";
        }

Would it perhaps be wiser to split this up into separate arrays first?

You could also add the items to 3 different strings and echo the concatenated string at the end:

$standard = '';
$provisions = '';
$magical = '';

foreach ($array as $value) {
    switch $value['itemtype']{
        case 'Standard':
            $standard .= $value['itemname'] . "\n";
            break;
        case 'Provisions':
            $provisions .= $value['itemname'] . "\n";
            break;
        case 'Magical':
            $magical .= $value['itemname'] . "\n";
            break;
        default:
            break;
    }
}
echo $standard . $provisions . $magical;

So you would only need to loop once over the array and you can easily add more cases.

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