-1

I've seen multiple threads converting python dictionaries to json but nothing showing how to save a python dictionary or a json array to a PHP array. I have inherited a PHP array file that needs to be modified. I was able to do this thru the help of another thread or two. I know am able to open the existing php array file, make needed changes and store to a python dictionary. I have saved it to a json array file but need to get it to the original format as shown below in order for existing scripts to have access to the data.

<?php
error_reporting(0);
$items=array (
0 =>
array (
'name' => 'Ted',
'size' => 'Large',
'sleeve' => 'Short',
'color' => 'Red',
),

);
$unObiect->items = $items;
$json = json_encode($unObiect);
echo($json);
?>

Currently using :

with open('data.json', 'w') as outfile:
        json.dump(r, outfile, sort_keys=True, indent=4, ensure_ascii=False)

Where the r is the python dictionary. The result of this array looks like:

{
    "items": [
        {
            "name": 'Ted',
            "size": 'Large',
            "sleeve": 'Short,
            "color":  'Red',
        },

Is there a way to retain the php array style with either the dictionary or the json array?

The reference duplicate is not the same. I do not wish to parse a json with PHP, I want to use Python to save to PHP array.

EDIT: Ignore all references to JSON in this thread. I only wish to save a Python dictionary to a PHP array in the format shown.

snowman
  • 123
  • 3
  • 10
  • 1
    `json_decode` ? I must have misunderstood the question. I'll read it again. – Don't Panic May 31 '19 at 17:27
  • 1
    Can you explain a little bit more what you mean by "get it to the original format" and "retain the php array style"? Maybe show an example of the output you're trying to get if possible? – Don't Panic May 31 '19 at 17:32
  • Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – miken32 May 31 '19 at 17:33
  • The example output is the first part I posted above. – snowman May 31 '19 at 17:34
  • 1
    The first part is actual PHP code, is that what you're trying to produce from JSON? – Don't Panic May 31 '19 at 17:37
  • Yes, trying to get the data back into the PHP array. – snowman May 31 '19 at 17:38
  • 2
    In effect the PHP script just produces JSON. Why not just create the same JSON with python and leave PHP out of it? Or is the problem that other PHP scripts get their data from that one? If that's the case, could you just update the data in the PHP file directly? – Don't Panic May 31 '19 at 17:48
  • Yes, that's the problem other scripts won't accept the data unless in the format. I think you are correct in that maybe the file will have to be updated with PHP instead of Python. – snowman May 31 '19 at 17:50
  • Okay, if I understand correctly, the PHP script currently has an array literal that it outputs as JSON, and instead of that literal, you need to use your Python script as the data source? – Don't Panic May 31 '19 at 17:53
  • @Don'tPanic See the comments under my answer. I still don't get the question.... – MonkeyZeus May 31 '19 at 18:13

1 Answers1

0

If your Python is producing the following JSON:

{
    "items": [
        {
            "name": 'Ted',
            "size": 'Large',
            "sleeve": 'Short,
            "color":  'Red',
        },

and you wish to access that data in PHP then you need json_decode():

$items = json_decode( file_get_contents( '/path/to/my/python/file.json' ), true )[ 'items' ];
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Yes, I have the second format you posted but need it to be as the first, I think at least. Not sure existing scripts will run correctly with what I have. – snowman May 31 '19 at 17:37
  • 1
    @snowman I don't quite understand. Where is the JSON coming from? I think you need `json_decode()` but I cannot make heads or tails of your post. Please write something coherent. – MonkeyZeus May 31 '19 at 17:40
  • The json is coming from converting the python dictionary to json. I don't have to have the json, I am just trying to either get the dictionary or a json array to the PHP array. – snowman May 31 '19 at 17:45
  • Simple, I need to save the Python dictionary to a PHP array. Forget the json. Just thought getting the json to PHP array would be easier than the dictionary. – snowman May 31 '19 at 17:47
  • @snowman That makes more sense now. The fact is that there is no direct Python dictionary to PHP array transfer mechanism. Both programming languages have their own internal constructs for representing data. JSON is a common middle-man for sending data between programming languages as is absolutely the right tool for this scenario. I will update my answer accordingly but please note that your question is written totally backwards from what you've described in your comment here... – MonkeyZeus May 31 '19 at 17:50
  • Noted. I guess I will have to revert to PHP and not use Python in order to get it to work as desired. – snowman May 31 '19 at 17:52
  • @snowman I am not sure what that means but please see my edit – MonkeyZeus May 31 '19 at 17:54