How to read and sort this json PHP?

1
{
"groups": {
    "superadmin": true,
    "admin": true,
    "user": true,
    "Hitman": true
},
"customization": {
    "1": [0, 0, 2],
    "2": [0, 0, 0],
    "3": [17, 0, 2],
    "4": [98, 1, 2],
    "5": [51, 0, 2],
    "6": [51, 0, 2],
    "7": [0, 0, 2],
    "8": [127, 0, 2],
    "9": [0, 1, 2],
    "10": [0, 0, 2],
    "11": [251, 1, 2],
    "12": [0, 2, 0],
    "13": [0, 2, 0],
    "14": [0, 0, 255],
    "15": [0, 2, 100],
    "16": [1, 2, 0],
    "17": [16777472, 2, 0],
    "18": [33554946, 2, 0],
    "19": [33686018, 2, 0],
    "20": [33686018, 2, 124],
    "modelhash": 1885233650,
    "p8": [-1, 0],
    "p3": [-1, 0],
    "p2": [-1, 0],
    "p1": [-1, 0],
    "p0": [124, 0],
    "p4": [-1, 0],
    "p5": [-1, 0],
    "p10": [-1, 0],
    "p6": [4, 0],
    "p7": [-1, 0],
    "p9": [-1, 0],
    "0": [0, 0, 2]
},
"weapons": {
    "GADGET_PARACHUTE": {
        "ammo": 0
    },
    "WEAPON_PISTOL": {
        "ammo": 43
    }
},
"inventory": {
    "medkit": {
        "amount": 9
    },
    "algemas": {
        "amount": 2
    },
    "vendaolhos": {
        "amount": 2
    },
    "lemonlimonad": {
        "amount": 2
    }
},
"gaptitudes": {
    "physical": {
        "strength": 105
    },
    "science": {
        "mathematics": 0,
        "chemicals": 490
    },
    "level": {
        "levelup": 0
    }
},
"position": {
    "z": 31.794088363647,
    "y": -732.02807617188,
    "x": 245.24957275391
},
"health": 173,
"phone_directory": [],
"thirst": 8.2327399233165,
"hunger": 38.863803982277
}

I just want to get the values of groups and weapons, but the instances are not fixed, or it will not always be "SuperAdmin" can be another value. I just want to read what value will be placed inside Groups and Weapon

    
asked by anonymous 03.04.2018 / 19:04

1 answer

3

You first need to move from JSON to array , and with a loop, get the information you want from array .

Examples:

Declares json in a variable:

$json = '{
    "groups": {
        "superadmin": true,
        "admin": true,
        "user": true,
        "Hitman": true
    },
    "customization": {
        "1": [0, 0, 2],
        "2": [0, 0, 0],
        "3": [17, 0, 2],
        "4": [98, 1, 2],
        "5": [51, 0, 2],
        "6": [51, 0, 2],
        "7": [0, 0, 2],
        "8": [127, 0, 2],
        "9": [0, 1, 2],
        "10": [0, 0, 2],
        "11": [251, 1, 2],
        "12": [0, 2, 0],
        "13": [0, 2, 0],
        "14": [0, 0, 255],
        "15": [0, 2, 100],
        "16": [1, 2, 0],
        "17": [16777472, 2, 0],
        "18": [33554946, 2, 0],
        "19": [33686018, 2, 0],
        "20": [33686018, 2, 124],
        "modelhash": 1885233650,
        "p8": [-1, 0],
        "p3": [-1, 0],
        "p2": [-1, 0],
        "p1": [-1, 0],
        "p0": [124, 0],
        "p4": [-1, 0],
        "p5": [-1, 0],
        "p10": [-1, 0],
        "p6": [4, 0],
        "p7": [-1, 0],
        "p9": [-1, 0],
        "0": [0, 0, 2]
    },
    "weapons": {
        "GADGET_PARACHUTE": {
            "ammo": 0
        },
        "WEAPON_PISTOL": {
            "ammo": 43
        }
    },
    "inventory": {
        "medkit": {
            "amount": 9
        },
        "algemas": {
            "amount": 2
        },
        "vendaolhos": {
            "amount": 2
        },
        "lemonlimonad": {
            "amount": 2
        }
    },
    "gaptitudes": {
        "physical": {
            "strength": 105
        },
        "science": {
            "mathematics": 0,
            "chemicals": 490
        },
        "level": {
            "levelup": 0
        }
    },
    "position": {
        "z": 31.794088363647,
        "y": -732.02807617188,
        "x": 245.24957275391
    },
    "health": 173,
    "phone_directory": [],
    "thirst": 8.2327399233165,
    "hunger": 38.863803982277
}';

Transforms to array:

$json_array = json_decode($json, true);

Print the array "weapons":

echo '<pre>';
print_r($json_array['weapons']);

Print all (final) ammo values:

echo '<pre>';
foreach ($json_array as $v) {

    echo '<br>Arma: ' . $json_array['weapons']['WEAPON_PISTOL']['ammo'];
}
    
03.04.2018 / 19:11