How to read and sort this PHP json?

Asked

Viewed 251 times

0

{
"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 take the values of groups and Weapons, but the instances are not fixed, IE will not always be "Superadmin" can be another value I just want to read which value will be placed within Groups and Weapon

  • 3

    json_decode($json, true)['weapons'], you don’t have to go through "superadmin"

  • I don’t think you understand very well, I want to get the values inside the tags groups and Weapons. all values written on them

1 answer

2


You must first pass JSON for array, and with a loop, get the information you want from the array.

Examples:

Declares the 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 "Weapons array":

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

I printed all (end) values of "Ammo":

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

    echo '<br>Arma: ' . $json_array['weapons']['WEAPON_PISTOL']['ammo'];
}
  • @Brandowfigueira, avoid posting comment as a thank you. To "thank" just upvote, and mark the topic as solved for the best answer you can find. Read the stack rules :]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.