【问题标题】:php iterating over an array to fetch account dataphp遍历数组以获取帐户数据
【发布时间】:2014-06-11 22:36:00
【问题描述】:

我设置了这个页面,它可以为我登录网站并将新内容下载到我的服务器。我希望它为多个帐户执行此操作,只要我的浏览器打开页面,就可以无限期地继续执行此操作。

在脚本中,我创建这样的连接:$connection = new Connection('myusername','pass');

也就是说,我想取出我的信用,然后将变量放在那里。上面我想声明一个用户名/密码组合数组,但我不知道如何访问索引。该数组将类似于:

<?php
$accounts = array(
"foo" => "bar",
"bar" => "foo!",
"foo" => "foo"
"bar" => "bar"
//100   => -100,
//-100  => 100,
);
//var_dump($array);
?>

所以循环应该类似于

<?php
for ($i=0; $i<=count($accounts); $i++) {
//set the first value to username, *****
//set the second value to password *****
//create my connection based on these variables
//do the rest of my steps
}
?> 

带星号的行是我不确定的行。使用上面的数组,元素是否存储为 $accounts[0][0] == 'foo', $accounts[0][0] == 'bar' ?

建议?

【问题讨论】:

    标签: php arrays loops indexing iteration


    【解决方案1】:

    在这种情况下,foreach 循环似乎更合适(也更容易):

    foreach ($accounts as $username => $password) {
        $connection = new Connection($username, $password);
        // Further processing...
    }
    

    【讨论】:

    • 现在看起来非常明显。我已经实施了它,并进行了测试。像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2023-02-10
    • 1970-01-01
    相关资源
    最近更新 更多