简体   繁体   English

循环遍历关联数组的数组

[英]Cycling Through Arrays of Associative Arrays

I have this (I'm only showing three records, there are many, many more) 我有这个(我只显示三个记录,还有很多很多)

Array
(
[0] => Array
    (
        [name] => Johnson, John
        [telephonenumber] => 555.555.555
        [department] => Department A
    )

[1] => Array
    (
        [name] => Johnson, Bill
        [telephonenumber] => 555.555.4444
        [department] => Department B
    )

[2] => Array
    (
        [name] => Johnson, Carry
        [telephonenumber] => 555.555.3333
        [department] => Department C
    )
)

There will be multiple members of Department A, B, etc. and I need to loop through this data and spit out only members of Department A. I have tried: 将有部门A,部门B等的多个成员,我需要遍历此数据并仅吐出部门A的成员。我已经尝试过:

if ($phoneList['department'] == 'Falmouth') {
    echo $phoneList['name'] . '<br>';
    echo $phoneList['telephonenumber'] . '<br>';
    echo $phoneList['department'] . '<br><br>';
}

But I am getting errors because I think $phoneList['department'] doesn't exist (shouldn't be $phoneList[0]['department'] )? 但是我收到错误消息是因为我认为$phoneList['department']不存在(不应该是$phoneList[0]['department'] )吗?

Either way, that won't help... how can I search through all 90 of these arrays and only print out those with Department A status? 无论哪种方式,都无济于事...我如何搜索所有90个阵列,仅打印出具有A部门身份的阵列?

$phoneList is the variable being passed to my view (using codeigniter, ldap and php) $ phoneList是传递给我的视图的变量(使用codeigniter,ldap和php)

You could use foreach : 您可以使用foreach

foreach($phoneList as $item)
{
  if($item['department'] == 'Falmouth')
  {
    echo $phoneList['name'] . '<br>';
    echo $phoneList['telephonenumber'] . '<br>';
    echo $phoneList['department'] . '<br><br>';
  }
}

I'm pretty sure Josh has it right, you should be using something like: 我很确定Josh正确,您应该使用类似以下内容的方法:

foreach( $phoneList as $item) {
    if( $item['department'] == 'Falmouth') {
        echo $item['name'] . '<br>';
        echo $item['telephonenumber'] . '<br>';
        echo $item['department'] . '<br><br>';
    }
}

You could even replace the inner part of the foreach loop with a call to implode() : 您甚至可以用对implode()的调用来替换foreach循环的内部部分:

foreach( $phoneList as $item) {
    if( $item['department'] == 'Falmouth') {
        echo implode( '<br>', $item) . '<br><br>';
    }
}
try
foreach($phoneList as $key => $data)
{

    if($data['department'] == 'DepartmentA')
    {
        ...
    }

}
$testNeedle = 'DepartmentS';
foreach( array_filter( $phonelist,
                       function($arrayEntry) use ($testNeedle) {
                           return $arrayEntry['department'] === $testNeedle;
                       }
         ) as $phoneEntry) {
    var_dump($phoneEntry);
}

For these type of questions, 'array_walk' can be used as well. 对于这些类型的问题,也可以使用“ array_walk”。 You should call the below written function as - 您应该将以下书面函数称为-

array_walk($phoneList, 'print_department');

The function is: 该函数是:

function print_department($phonelist){

    // Printing the items
    if($phonelist['department'] == 'Falmouth'){
        echo $phonelist['name']. '<br>';
        echo $phonelist['telephonenumber']. '<br>';
        echo $phonelist['department']. '<br>';
    }
}

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

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