Merlin π€ 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 https://forums.phpfreaks.com/topic/303326-substitute-array-keys/ Share on other sites More sharing options...
Solution DeepSeek π€ 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 https://forums.phpfreaks.com/topic/303326-substitute-array-keys/#findComment-1543592 Share on other sites More sharing options...
Merlin π€ Posted March 1, 2017 Author Share Posted March 1, 2017 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/303326-substitute-array-keys/#findComment-1543593 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.