Jump to content

JonnyEnglish

New Members
  • Posts

    7
  • Joined

  • Last visited

JonnyEnglish's Achievements

Member

Member (2/5)

0

Reputation

  1. Hi guys, I like to run this script for each of the json files in a folder but I’m struggling to get it working and would appreciate some guidance =) <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); include_once('db.txt'); ///-----------------first effort--------------------------------------------- /// runs without an error but it doesnt produce anything //$files = glob('C:\php\Inventories\Json.{json}', GLOB_BRACE); //foreach($files as $file) { ///-----------------second effort--------------------------------------------- /// this produces this interesting error: PHP Warning: file_get_contents(C:\php\Inventories\Json): failed to open stream: Permission denied in C:\php\Inventories\index.php on line 17 (thats line 27 in this example) $dir = "C:\php\Inventories\Json"; foreach(glob($dir) as $file) { ///-------------------------------------------------------------- $json = file_get_contents("$file"); $json = json_decode($json, true); ///-------------------------------------------------------------- $name1 = array(); $name1[] = $json['user_data']['name']; ///------------------- DECK1 ------------------------------------ $deck1 = array(); $id = $json['user_decks']['1']['deck_id']; $com = $json['user_decks']['1']['commander_id']; $deck1[] = $commander_id = $d[$com][0]; $dom = $json['user_decks']['1']['dominion_id']; $deck1[] = $dominion_id = $d[$dom][0]; $card = $json['user_decks']['1']['cards']; foreach ($card as $cardid => $cardqty){ $deck1[] = $g = $d[$cardid][0] . "-" . $d[$cardid][1] . "(" . $cardqty . ")"; } ///------------------- INVENTORY -------------------------------- $inventory = array(); foreach ($json['user_cards'] as $row => $v){ if ($v['num_owned'] > 0){ $inventory[] = $d[$row][0] . "-" . $d[$row][1] . "(" . $v['num_owned'] . ")" . "\n"; } } sort ($inventory); $name2 = implode($name1); $deck2 = implode(",",$deck1); $inventory2 = implode($inventory); $file = $name2 . ".txt"; file_put_contents($file, $deck2, FILE_APPEND); file_put_contents($file, $inventory2, FILE_APPEND); } ?>
  2. Thanks for your patience. Your solution worked I was just being stupid.
  3. Hi, thanks for taking the time to reply – I appreciate it but I’m still confused so I thought it best to describe what I’m after in full as I think it’ll be more beneficial. I want to produce an output that looks like this: Infantry-1(9) Bazooka Marine-1( Medic-1(11) But the code above produces : Infantry-1(9) Infantry-3(1) Bazooka Marine-1( Medic-1(11) I don’t want to display “Infantry-3(1)” because the value in "num_owned” is 0. I’m substituting the keys in the user_cards object ("1", "2", etc.) with the values in db.txt and then appending the "num_owned” value in brackets. db.txt <?php $d[1] = array("Infantry",1); $d[2] = array("Infantry",2); $d[3] = array("Infantry",3); $d[4] = array("Bazooka Marine",1); $d[350] = array("Bazooka Marine",2); $d[351] = array("Bazooka Marine",3); $d[5] = array("Medic",1); ?> Do I still need to get rid of the inner for loop?
  4. Hi guys, How can I modify the code below to only echo $val where "num_owned" is greater than 0? Unfortunately, “if (empty($val))” echos the values where “"is_locked": true” is present as one which is something I’d like to avoid. json data: { "user_cards": { "1": { "num_owned": "9", "num_used": "0" }, "2": { "num_owned": "0", "num_used": "0" }, "3": { "num_owned": "0", "num_used": "0", "is_locked": true }, "4": { "num_owned": "8", "num_used": "0" }, "5": { "num_owned": "11", "num_used": "0" } } } PHP script <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); include_once('db.txt'); $json = file_get_contents("TU.json"); $json = json_decode($json, true); foreach ($json['user_cards'] as $row => $v) { foreach ($v as $key => $val){ if (empty($val)) {continue;} echo $d[$row][0] . "-" . $d[$row][1] . "(" . $val . ")" . PHP_EOL; echo "<br>"; } } ?>
  5. Hi, I’d like to substitute array keys but I’m new to PHP and I don’t know how to do it – I was hoping you could help. The source array looks like this: "user_cards": { "1": { "num_owned": "9", "num_used": "0" }, "2": { "num_owned": "0", "num_used": "0" }, "3": { "num_owned": "0", "num_used": "0" }, "4": { "num_owned": "8", "num_used": "0" }, "5": { "num_owned": "11", "num_used": "0" } I’d like to substitute the keys “1,2,3,4,5” etc. with the array values from a txt file called db.txt That array looks like this: <?php $d[1] = array("Infantry",0); $d[2] = array("Infantry",2); $d[3] = array("Infantry",3); $d[4] = array("Bazooka Marine",0); $d[350] = array("Bazooka Marine",2); $d[351] = array("Bazooka Marine",3); $d[5] = array("Medic",0); ?> so my current output of 1 2 3 4 5 would be replaced with Infantry Infantry Infantry Bazooka Marine Medic this is what I have so far: <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); include_once('db.txt'); $json = file_get_contents("TU.json"); $json = json_decode($json, true); foreach ($json['user_cards'] as $row => $v) { echo "$row<br>"; // foreach ($v as $key => $val) // { // echo "$key : $val'<br>"; // } } ?> I don’t know if this’ll make a difference to any advice you may offer but I will be trying to append the value from "num_owned" to each name too.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.