Pass an array from PHP to PYTHON and do numpy processing to get the result

reference http://stackoverflow.com/questions/25845600/passing-php-array-to-python-doesnt-work

python


   $numbers = [1, 2, 3, 4, 5];
   $pythonScript = "./test.py";
   $cmd = array("python", $pythonScript, escapeshellarg(json_encode($numbers)));
   $cmdText = implode(' ', $cmd);
   
   echo "Running command: " . $cmdText . "\n";
   $result = shell_exec($cmdText);
  var_dump(json_decode($result, true));

python


   # -*- coding: utf-8 -*-                                                                                                                                                                                                                                                     
import numpy as np
import sys, json

try:
    data = np.array(json.loads(sys.argv[1]))
    std = np.std(data)
    avg = np.mean(data)
    min = np.min(data)
    max = np.max(data)
    result = {'std':std, 'avg':avg, 'min':min, 'max':max}

    print json.dumps(result)
except Exception as e:
    print str(e)
       

Recommended Posts

Pass an array from PHP to PYTHON and do numpy processing to get the result
How to get followers and followers from python using the Mastodon API
I want to pass an argument to a python function and execute it from PHP on a web server
Get the last element of the array by splitting the string in Python and PHP
Cheating from PHP to Python
How to swap elements in an array in Python, and how to reverse an array.
Pass OpenCV data from the original C ++ library to Python
Subscript access to python numpy array
How to pass and study the Python 3 Engineer Certification Basic Exam
[Python] How to get the first and last days of the month
How to get the Python version
The road to installing Python and Flask on an offline PC
Various ways to create an array of numbers from 1 to 10 in Python.
Output the report as PDF from DB with Python and automatically attach it to an email and send it
Pass the selected item in Tablacus Explorer from JScript to python and rename it all at once
[Python Kivy] How to get the file path by dragging and dropping
How to get the date and time difference in seconds with python
Tips: [Python] Randomly restore and extract an array from a fasta file
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Extract every n elements from an array (list) in Python and Ruby
[python] Send the image captured from the webcam to the server and save it
How to sort by specifying a column in the Python Numpy array.
C language to see and remember Part 5 Call C language from Python (argument) Array
From Python to using MeCab (and CaboCha)
Academia Potter and the Mysterious Python Pass
Get the address from latitude and longitude
[Python] Get the main color from the screenshot
How to pass the execution result of a shell command in a list in Python
How to get started with the 2020 Python project (windows wsl and mac standardization)
Organize the flow from granting permissions to python users to make migrations and migrating
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
Save an array of numpy to a wav file using the wave module
What to do if you get an error when installing python with pyenv
[Python] I tried to get the type name as a string from the type function
I want to get the file name, line number, and function name in Python 3.4
I tried to pass the G test and E qualification by training from 50
Get the contents of git diff from python
How to do multi-core parallel processing with python
Summary of the differences between PHP and Python
Just add the python array to the json data
Add rows to an empty array with numpy
View the result of geometry processing in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
[Bash] Use here-documents to get python power from bash
PHP and Python integration from scratch on Laravel
How to get the files in the [Python] folder
[Python numpy] Dynamically specify the index of the array
What to do to get google spreadsheet in python
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
An easy way to call Java from Python
An article summarizing the pitfalls addicted to python
What to do if you get an "unknown service" error from your gRPC server
From the initial state of CentOS8 to running php python perl ruby with nginx
What to do if you get an OpenSSL error when installing Python 2 with pyenv
[Blender] How to get the selection order of vertices, edges and faces of an object
Extract images and tables from pdf with python to reduce the burden of reporting
What to do if the print command itself causes an error in Maya python
What to do if you get an error when importing matplotlib in Python (Mac)
How to pass arguments when invoking python script from blender on the command line
What to do if Python does not switch from the System version in pyenv
I tried to compare the processing speed with dplyr of R and pandas of Python