JonnyEnglish Posted March 1, 2017 Share Posted March 1, 2017 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. Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted March 1, 2017 Solution Share Posted March 1, 2017 (edited) Change echo "$row<br>"; To echo "{$d[$row][0]}<br>"; Edited March 1, 2017 by Psycho Quote Link to comment Share on other sites More sharing options...
JonnyEnglish Posted March 1, 2017 Author Share Posted March 1, 2017 Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.