Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为数组中的每个元素调用相同的函数?_Php_Mysql_Arrays - Fatal编程技术网

Php 如何为数组中的每个元素调用相同的函数?

Php 如何为数组中的每个元素调用相同的函数?,php,mysql,arrays,Php,Mysql,Arrays,我基本上有一个表单,一旦按下submit按钮,它就会从MySQL传递数据 我从MySQL中检索了一个数组,希望为每个元素调用相同的函数。在过去的14个小时里,我尝试了一个for循环,但我无法让它为数组中的每个元素运行相同的函数 形式 我基本上需要将pinPost()中的每个数组作为$boardId发布,然后对所有其他数组运行pinPost()。让它真正容易理解这正是我想要做的 当我点击post按钮时,它将抓取第一个数组string(18)“136234026166934321”,将其作为$boa

我基本上有一个表单,一旦按下submit按钮,它就会从MySQL传递数据

我从MySQL中检索了一个数组,希望为每个元素调用相同的函数。在过去的14个小时里,我尝试了一个for循环,但我无法让它为数组中的每个元素运行相同的函数

形式 我基本上需要将
pinPost()
中的每个数组作为
$boardId
发布,然后对所有其他数组运行
pinPost()
。让它真正容易理解这正是我想要做的

当我点击post按钮时,它将抓取第一个数组
string(18)“136234026166934321”
,将其作为
$boardId
提交到
pinPost()
,然后它将返回到开头并运行第二个数组,
string(18)“286119451265381250”
,直到提交所有数组,基本上,它只是将相同的信息发布到不同的板上,这正是我想要做的


我尝试过执行for循环并在内部运行
pinput()
,但我无法将板ID传递给
pinput()
函数,因此它无法运行。

变量名可能有问题。 试着这样做:

$postBoards = $_POST['post_board_id'];

function pinLogin() 
{
    $ch         = // ch is a variable from cURL
    $cookie     = '/cookie.txt'; //cookie session
    $url        = 'http://www.website.com/';

    // Run the pinPost callback for each element of $postBoards
    // with your custom user data from this function
    array_walk("pinPost", $postBoards, array($ch, $cookie, $url);
}

function pinPost($boardId, $boardKey, $data)
{
    // You have your boardId for each submitted board available here
    $postDesc   = $_POST['post_description'];
    $postUrl    = $_POST['post_url'];
    $postImage  = $_POST['post_image'];

    // $data contains your pinPost data for example
    $ch = $data[0];
}

因此,您可以使用一些不同的技术来实现这一点。我的首选是使用,因为它的回调同时接受数组的键和值也可以在这里使用,尽管它只在回调中使用数组的值

  /**
   * logs into website with headers
   */
  function pinLogin() {
     $ch = // ch is a variable from cURL
     $cookie = '/cookie.txt'; //cookie session
     $url = 'http://www.website.com/'; // website to post data to


     /**
      * here iterate over all of your board id's and pass them to pin post function. The pinPost function
      * will be run 18 times
      */
     foreach($_POST['post_board_id'] as $boardId) {
        /**
         * @var $boardId string this is the 18 characters long stringg $boardid
         */
        pinPost($ch, $cookie, $url, $boardId);
     }
  }

  /**
   * posts the information below to the logged in website
   *
   * @param $ch
   * @param $cookie
   * @param $url
   * @param $boardId id of the board you want to pin post to (NEW PARAMETER)
   */
  function pinPost($ch, $cookie, $url, $boardId) {
     //$boardId is already here
     $postDesc = $_POST['post_description'];
     $postUrl = $_POST['post_url'];
     $postImage = $_POST['post_image'];

     /* do your stuff here */
  }

这使您的
pinPost
函数成为
$postBoards
数组中每个元素的回调函数,并将在调用
pinLogin()
后按顺序运行每个数组条目。您可以尝试按如下方式更新代码


输出就在那里,正如在
$\u POST['POST\u board\u id']
上的调试所演示的那样,这并不能解决OP的问题。我选择你的答案是因为它更容易理解,更有意义,也让我有点恼火,因为我没有想到用board id运行pinLogin,但这是完美的。谢谢你的时间,如果我浪费了超过14个小时的生命,我也会很生气,但我只是学会了如何不去做,我选择了它利用你现有的思维和工作方式,而不从根本上改变你的代码。关于浪费的时间,相信我,时间不是浪费的。尝试(不管成功与否)是一种有效的学习方式。托马斯·爱迪生——我没有失败。我刚刚找到了2000种不做灯泡的方法;我只需要找到一种方法就行了。
string(18) "136234026166934321"

string(18) "286119451265381250"

string(18) "106468047379795203"

string(18) "468022654964640361"

string(18) "409757334785529893"

string(18) "409757334785575605"

string(18) "490681390589888313"
foreach ($pClass->retrieveAllInfo() as $data) {
    foreach ($pClass->retrieveBoards() as $boards) {
    }
}
$postBoards = $_POST['post_board_id'];

function pinLogin() 
{
    $ch         = // ch is a variable from cURL
    $cookie     = '/cookie.txt'; //cookie session
    $url        = 'http://www.website.com/';

    // Run the pinPost callback for each element of $postBoards
    // with your custom user data from this function
    array_walk("pinPost", $postBoards, array($ch, $cookie, $url);
}

function pinPost($boardId, $boardKey, $data)
{
    // You have your boardId for each submitted board available here
    $postDesc   = $_POST['post_description'];
    $postUrl    = $_POST['post_url'];
    $postImage  = $_POST['post_image'];

    // $data contains your pinPost data for example
    $ch = $data[0];
}
  /**
   * logs into website with headers
   */
  function pinLogin() {
     $ch = // ch is a variable from cURL
     $cookie = '/cookie.txt'; //cookie session
     $url = 'http://www.website.com/'; // website to post data to


     /**
      * here iterate over all of your board id's and pass them to pin post function. The pinPost function
      * will be run 18 times
      */
     foreach($_POST['post_board_id'] as $boardId) {
        /**
         * @var $boardId string this is the 18 characters long stringg $boardid
         */
        pinPost($ch, $cookie, $url, $boardId);
     }
  }

  /**
   * posts the information below to the logged in website
   *
   * @param $ch
   * @param $cookie
   * @param $url
   * @param $boardId id of the board you want to pin post to (NEW PARAMETER)
   */
  function pinPost($ch, $cookie, $url, $boardId) {
     //$boardId is already here
     $postDesc = $_POST['post_description'];
     $postUrl = $_POST['post_url'];
     $postImage = $_POST['post_image'];

     /* do your stuff here */
  }