Passing array argument to bash from php

前端 未结 1 1976
名媛妹妹
名媛妹妹 2021-01-15 08:19

I am trying to pass array from shell_exec and want to access the array in bash file. I tried to research from many websites but did not find the proper solution for it. Plea

1条回答
  •  北海茫月
    2021-01-15 09:00

    If your php variable $array is a php Array, you need to convert it to a string to do the call, eg:

     shell_exec('sh get_countries.sh '.join(' ',$array));
    

    Then your countries.sh should be exactly what you gave as an example:

    #!/bin/bash
    declare -a myArray
    myArray=("$@") 
    for arg in "${myArray[@]}"; do
     echo "$arg"
    done
    

    Make sure you permit the file for execution with chmod a+rx countries.sh.

    0 讨论(0)
提交回复
热议问题