简体   繁体   English

PHP遍历数组以获取帐户数据

[英]php iterating over an array to fetch account data

I have this page set up that logs into a site for me and downloads fresh content to my server. 我设置了此页面,该页面可以为我登录一个网站并将新内容下载到我的服务器上。 I would like it to do this for a number of accounts, continuing to do so indefinitely as long as my browser has the page open. 我希望它能对多个帐户执行此操作,只要我的浏览器已打开页面,它就会无限期地继续执行此操作。

In the script, I create the connection like this: $connection = new Connection('myusername','pass'); 在脚本中,我这样创建连接: $connection = new Connection('myusername','pass');

That said, I would like to take my creds out, and then place variables there. 就是说,我想拿出我的信誉,然后在其中放置变量。 above that I would like to declare an array of username/password combos, but I dont know how to access the indexes. 上面我想声明一个用户名/密码组合数组,但是我不知道如何访问索引。 The array would be similar to this: 该数组将类似于以下内容:

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

And so the loop should be something like 所以循环应该像

<?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
}
?> 

The lines with the asterisks are the lines I am unsure of. 带星号的线是我不确定的线。 With the array above, are the elements stored as $accounts[0][0] == 'foo', $accounts[0][0] == 'bar' ? 在上面的数组中,元素是否存储为$ accounts [0] [0] =='foo',$ accounts [0] [0] =='bar'?

Suggestions? 建议?

In this case, a foreach loop seems more appropriate (and easier): 在这种情况下, foreach循环似乎更合适(更容易):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM