嘿大家所以我试图循环遍历现有数组,然后在满足某些条件的情况下将该数组中的数据添加到新数组中。这是我当前的代码,但它不起作用。我不知道如何去做我想做的事。
$allClientArr = $authPartner->getmyClients();
foreach($allClientArr as $client){
if($client->get('status') == "AC"){
$clientArr += $client;
}
}
我在页面中使用了clientArr的元素。
答案 0 :(得分:3)
这不是你在PHP中的表现。您的代码应如下所示:
$allClientArr = $authPartner->getmyClients();
$clientArr = array(); // make sure you define $clientArr as an array
foreach($allClientArr as $client){
if($client->get('status') == "AC"){
$clientArr[] = $client; // this is how you add element to array
}
}
答案 1 :(得分:1)
该行
$clientArr += $client;
将尝试将值添加到变量中。如果您要将$client
添加到$clientArr
,请尝试
$clientArr[] = $client;
答案 2 :(得分:1)
如果您使用的是PHP 5.5,那么我有两个答案:
$allClientArr = $authPartner->getmyClients();
$clientArr = array(); // make sure you define $clientArr as an array
foreach($allClientArr as $client){
if($client->get('status') == "AC"){
$clientArr[] = $client; // this is push the variable $client into the array $clientArr[]
}
}
但如果您使用php 5.4,请执行:
$allClientArr = $authPartner->getmyClients();
$clientArr = array(); // make sure you define $clientArr as an array
foreach($allClientArr as $client){
if($client->get('status') == "AC"){
array_push($clientArr[], $client); // this is push the variable $client into the array $clientArr[] in another word it add it in the end of the array
}
}