0

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!!!!

1 Answer 1

1

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"
}
Sign up to request clarification or add additional context in comments.

1 Comment

This did work thankyou! I had something weird going on with my jquery request on my server so it didn't show up initially, but I got it working. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.