简体   繁体   中英

How do you pull a specific JSON Object from an array using PHP

I'm making a character showcase webapge for a project and I'm new to learning PHP and essentially I'm trying to pass in a key and pull a specific object from a json array. This is the beginning of my php file:

<?
    $character = $_GET['character'];
    $json = file_get_contents("abilities.json");
    ....json parsing here halp....
?>

$character passed in by jquery in my html file:

function getAbilities(){
            $.getJSON("read_stats.php", {"character": "Commando"}, function(data){
                //jquery loop here
            });
        }

My JSON file I created looks like this:

[
    {"Commando":[
        {"double_tap": "Shoot twice for 2x90% damage."},
        {"phase_round": "Fire a piercing bullet for 230% damage."},
        {"tactical_dive": "Roll a short distance."},
        {"suppressive_fire": "Fire rapidly, stunning enimes for 6x100% damage."}
    ]},
       {"Huntress":[
        {"strafe": "Fire a seeking arrow for 150% damage. Can be used while sprinting."},
        {"laser_glaive": "Throw a seeking glaive that bounces up to 6 times for 250% damage. Damage increases by 10% per bounce"},
        {"blink": "Disappear and teleport forward."},
        {"phase_blink": "Replaces Blink. Disappear and teleport a short distance. Can store up to 3 charges."},
        {"arrow_rain": "Teleport into the sky. Target an area to rain arrows, slowing all enemies and dealing 225% damage per second."},
        {"ballista": "Replaces Arrow Rain. Teleport backwards into the sky. Fire up to 3 energy bolts, dealing 3x900% damage."}
    ]} ......etc. ]

So I have an array of JSON objects with the key being the character name, and the value being another array of ablities, my end goal is to be able to print out each ability, but that could be handled using jquery if I could manage to just grab the correct JSON object from the array. Any help or pointers would be really appreciated!!!!

Probably something like this:

<?php

$character = $_GET['character'];
$json = file_get_contents("ablities.json");
$data = json_decode($json, TRUE); // "TRUE" for parsing as assoc array

// Get the character by the name given in $_GET['character']
// or NULL if no value matches.
$result = NULL;
foreach ($data as $character_info) {
    $character_name = @array_pop(array_keys($character_info));
    if (strtolower($character_name) === strtolower($character)) {
        $result = $character_info;
        break;
    }
}

if ($result !== NULL) {
  echo json_encode([
    'status' => 'success',
    'data' => $character_info,
  ]);
} else {
  echo json_encode([
    'status' => 'failed',
    'message' => 'Character not found',
  ]);
}

?>

If searching for "commando":

{
  "status": "success",
  "data": {
    "Commando":[
      {"double_tap":"Shoot twice for 2x90% damage."},
      {"phase_round":"Fire a piercing bullet for 230% damage."},
      {"tactical_dive":"Roll a short distance."},
      {"suppressive_fire":"Fire rapidly, stunning enimes for 6x100% damage."}
    ]
  }
}

If searching for "some guy":

{
  "status": "failed",
  "message":"Character not found"
}

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