Print php array in python - php

I have an array in php that I would like to print in python
php
return array();
python code
value=os.system("php path/to/file"); // returns array
print value
//output
0
How do I print out the values of value?

Related

Array of array in python to pass in php

How i create an array of array in python to pass php format correctly?
I tried to insert the ret in python also inside the json.dumps function and inside json.loads, but without results
Output in php:
{
[‘event’]=>‘Some text’
[‘data’]=>Array
(
[‘key’]=> value
)
}
In python create a dictionary:
ret = {
‘event’:’Some text’,
‘data’ :{‘key’:’value’}
}
r= request.post(“url”, ret)
But i recived this from python:
Output:
{
[‘event’]=>‘Some text’
[‘data’]=> ‘key’
}

Get JSON value from second level in array's hierarchy using PHP

I have the following JSON:
{
"stars":5,
"numberOfReviews":{
"total":1179,
}
}
and the following PHP:
$json = file_get_contents('url-of-json');
$obj = json_decode($json);
print $obj->stars;
This prints 5 as expected. How do I print the "total" value of 1179 from the second level in the array's hierarchy?
I've been unable to find an explanation on how to achieve this. Here's some example of what I've been trying so far:
print $obj->total
print $obj->{'numberOfReviews', 'total'}
print $obj->['numberOfReviews']['total']
print $obj->numberOfReviews{'total'}
print $obj->[1]['total']
print $obj->numberOfReviews[0]total
print $obj->total[1]
$obj->numberOfReviews is an \stdClass object too, so try it with $obj->numberOfReviews->total
$obj->numberOfReviews->total;
Or add second parameter to json_decode with true value, and use it as array.

How to access & Print Json data from Bottom to Top

I have a json data like
{"Nepal":"1","India":"3","Pakistan":"2","America":"25","Bangaladesh":"23"}
Now what I want is to print and access data from last to top
to print like this using PHP
Bangaladesh 23
America 25
Pakistan 2
India 3
Nepal 1
Thank you in advance..
you can do reverse loop like this using list.length. Code depends upon the language you are using.
for(var i=list.length-1;i>=0;i--){
//your printing here
}
As Ruchan mentioned, iterating over your JSON Array object in reverse is a solution. Here is a snippet for PHP
//Decode the JSON
$array = json_decode('[{"Nepal":"1","India":"3","Pakistan":"2","America":"25","Bangaladesh":"23"}]',false); //Note: Second parameter is a boolean indicating if the resulting array should be associative or not
//Get the length of the Array
$length = count($array);
//Iterate over the array from the last index to the first (ie, length-1 to 0)
while($length) {
echo sprintf("<p>%s</p>", $array[--$length]);
}

Setting Assoc Array from Function to use througough page

How would I setup an associative array to reference specific values at different sections of a page. My function:
<?php
function park_data($park_page_id) {
$data = array();
if($park_page_id){
$data = mysql_fetch_assoc(mysql_query("SELECT * FROM `park_profile` WHERE `park_id` = $park_page_id"));
return $data;
}
}
?>
My print_r:
<?php
print_r (park_data(1));
?>
Produces the following associative array:
Array ( [park_id] => 1 [park_name] => Kenai Fjords [park_address] => 1212 4th Avenue [park_city] => Seward [park_state] => Alaska [park_zip] => 99664)
How would I print just the [park_name] value from this array?
From the docs:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
// on PHP 5.4
print_r(park_data(1)['park_name']);
// earlier versions
$tmp = park_data(1);
print_r($tmp['park_name']);
$park=park_data(1);
echo $park['park_name'];
To output custom formatted text in general, and in this case to output only a single array's key value, use echo, because print_r() called on an array displays the whole array's structure and content, and that's not what you want:
<?php
// code
$park_data=park_data(1);
echo $park_data["park_name"];
// code
?>

Unable to assign value to php array

I am creating an array in php by assigning values retrieved from database. When I print the array it is displaying array as output and not its contents. However it does retrieve values from mysql.
$resultset=mysql_query("select isbn from tbl_book where publisherid='$publisherid'");
/***Retrieve Books*****/
while($resultISBNArray = mysql_fetch_assoc($resultset))
{
$isbn = $resultISBNArray["isbn"];
$myArr[]=$isbn;
}
echo $myArr
echoing any array always prints "Array". You need to pick individual values in the array (echo $myArr[0]) or use something like print_r().
You can not print an array. You have todo something like var_dump($myArr);

Categories