我基本上有一个表单,一旦按下提交按钮,它就会从 MySQL 传递数据。
我有一个从 MySQL 检索到的数组,我想为每个元素调用相同的函数。在过去的 14 小时里,我尝试了一个 for 循环,但我无法让它为数组中的每个元素运行相同的函数。
表格
<?php
foreach($pClass->retrieveAllInfo() as $data)
{
echo '
<form method="post" action"">
<input type="hidden" name="post_url" value="'.$data['p_Url'].'">'.$data['p_Url'].'
<input type="hidden" name="post_description" value="'.$data['p_Description'].'">'.$data['p_Description'].'
<input type="hidden" name="post_image" value="'.$data['p_Image'].'">'.$data['p_Image'].'
<ul class="items">';?>
<?php
foreach($pClass->retrieveBoards() as $data)
{
echo '
<li><input type="checkbox" name="post_board_id[]" value="'.$data['boardiD'].'">'.$data['boardName'].'</li>
';
}
?>
<?php
echo '
</ul>
<input name="post_info" type="submit" value="Post">
</form>';
?>
在同一个文件中,我有以下内容..
if(isset($_POST['post_info']))
{
pinLogin(); //calls and runs the function
foreach($_POST['post_board_id'] as $data)
{
echo '<pre>'; var_dump($data); echo '</pre>'; //just retrieving the arrays
}
}
function pinLogin() // logs into website with headers
{
$ch = // ch is a variable from cURL
$cookie = '/cookie.txt'; //cookie session
$url = 'http://www.website.com/'; // website to post data to
pinPost($ch, $cookie, $url);
}
function pinPost($ch, $cookie, $url) // posts the information below to the website above
{
$boardId = // here needs to be the board id that needs to be runned for each array..
$postDesc = $_POST['post_description'];
$postUrl = $_POST['post_url'];
$postImage = $_POST['post_image'];
}
基本上,当您按下 post_info
按钮时,它会运行 pinLogin()
函数,该函数会使用信息运行 pinPost()
从表单发送的东西是 $_POST['post_board_id']
;是一个数组,var_dump
结果如下:
string(18) "136234026166934321"
string(18) "286119451265381250"
string(18) "106468047379795203"
string(18) "468022654964640361"
string(18) "409757334785529893"
string(18) "409757334785575605"
string(18) "490681390589888313"
我基本上需要将每个数组作为 $boardId
发布在 pinPost()
中,然后为所有的运行 pinPost()
其他数组。为了让它真正容易理解,这正是我想要做的。
当我点击发布按钮时,它会抓取第一个数组string(18) "136234026166934321"
,将其作为$boardId提交给
,然后它会回到开头并运行第二个数组,pinPost()
string(18) "286119451265381250"
直到所有数组都已提交,几乎它只是将相同的信息发布到不同的板,这正是我想要做的。
我已经尝试做一个 for 循环并在里面运行 pinPost()
但我无法将板 ID 传递给 pinPost()
函数所以它不会跑。
最佳答案
因此,您可以使用几种不同的技术来实现这一目标。我的偏好是使用 array_walk 因为它的回调接受数组的键和值。 <强> array_map 也可以在这里使用,尽管它只能在其回调中使用数组的值。
$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];
}
这使您的 pinPost
函数成为 $postBoards
数组的每个元素的回调函数,并且一旦您调用 pinLogin()< 就会按顺序为每个数组条目运行
.
关于php - 如何为数组中的每个元素调用相同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23823271/