Jump to content

substitute array keys


JonnyEnglish
Go to solution Solved by Psycho,

Recommended Posts

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.