Needing some help on where to start. I have inherited a file called data.php which consist of an array shaped as follows:
<?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);
?>
There are actually about one hundred arrays in the file. I am trying to open the file, search an element of the array and replace a value. Not concerned about the replace part yet just trying to open the file and loop thru the array. I'm not sure how to actually open the file in Python and loop it. I have tried
with open("data.php") as raw:
for items in raw:
print(items)
This opens the file but seems to be reading by line which I expected. How can it be opened and loop thru as an array, something like
with open("data.php") as raw:
for items in raw['name']:
print(items)
Where I can get an element of the array without going thru every line and seeing if that line contains 'name' then splitting the line to get to the value after the '=>'. I guess basically looking to open the file and loop the array. I know there are other post similar to this but I guess I'm not sure if I'm actually opening a php file as a text file somebody named with php or an actual php file that has an actual json array. Hope this makes sense.
Edit: From a comment I took suggestion to execute the php from the python. Using the following I was able to do this:
result = subprocess.run(
['php', 'data.php'],
stdout=subprocess.PIPE,
check=True
)
print(result.stdout)
Not sure now what to do with the result.stdout, how to iterate thru it. This returns a byte-string.