简体   繁体   English

如何在php中遍历数组内部的数组?

[英]How to loop through an array inside of array in php?

I'm trying to loop through an array inside of an array, so that I can display either phone numbers or extension number. 我正在尝试遍历数组内部的数组,以便可以显示电话号码或分机号。 For example: If a user has both (phone number and extension number) then I should ONLY display phone number, but sometimes a user has only a extension number then I should display the extension number. 例如:如果用户同时拥有(电话号码和分机号码),那么我应该只显示电话号码,但是有时用户只有分机号码,那么我应该显示分机号码。

And here's my code: 这是我的代码:

  <table style="padding: 40px;margin-left: -10px;margin-top:-38px;display: inline-block;">
            <div style="margin-top:16px;margin-left:10px;">
                <input type="checkbox" id="checkAll"/>
            </div>
            <div style="padding:20px;">
                @foreach($resultArray as $key => $value)

                    @foreach($value as $key2 => $value2)
                        @if(is_array($value2))
                            @foreach($value2 as $key3 => $value3)
                                <?php
                                   // echo var_dump($value3);

                                if (in_array($value3['phoneNumber'], $value3)) {
                                    if (strlen($value3['phoneNumber']) === 11) {
                                        $value3['phoneNumber'] = ltrim($value3['phoneNumber'], 1);
                                    }
                                }
                                else{
                                    $value3['phoneNumber'] = $value3['extension'];
                                }



                                ?>

                                <tr>
                                    <td>
                                        <input class="input_checkbox" type="checkbox"
                                               id="{{$key3}}customer-name-checkbox" name="{{$key3}} "
                                               value="yes"><span style="padding-left:40px;"></span>
                                    </td>
                                    <td>{{$value3['firstName']}}  {{$value3['lastName']}}</td>
                                    <td>{{$value3['phoneNumber']}}}</td>
                                    <td><input style="margin-left:60px;float: right;" type="email" class="styled-text  rounded" name="{{$key3}}" id="{{$key3}}customer-name-inputField" placeholder="" value=""/><br/><br/>
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    @endforeach
                @endforeach
            </div>
        </table>

Can someone tell me what I'm doing wrong please? 有人可以告诉我我在做什么错吗? Thank you so much in advance!! 提前非常感谢您!!

From your requirements, I think you need something like this: 根据您的要求,我认为您需要这样的东西:

<?php
foreach ($resultArray['searchUserResults']['searchUserResult'] as $key => $data)
{
    if (isset($data['phoneNumber']))
        echo $data['phoneNumber'];

    else if (isset($data['extension']))
        echo $data['extension'];
}
?>

This will output either of the two, but only phoneNumber if both are present. 这将输出两者之一,但如果两者都存在,则仅输出phoneNumber

You do not need all the nested foreach loops to accomplish this. 您不需要所有嵌套的foreach循环即可完成此操作。 Instead you iterate over the sub-array only. 相反,您仅迭代子数组。

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

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