0

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!!

10
  • Never use error squelching: @. Especially, while you try to debug your code. Commented Jun 6, 2016 at 19:04
  • 1
    @vp_arth the @ is part of Blade (Laravel) Commented Jun 6, 2016 at 19:05
  • Ouch, 'm sorry. Ugly syntax :) Commented Jun 6, 2016 at 19:06
  • What exactly is your issue? As in, what are you seeing with your current code and what did you expect to see? Commented Jun 6, 2016 at 19:06
  • 1
    In your original code you need to use array_key_exists or isset instead of in_array($value3['phoneNumber'], $value3). Commented Jun 6, 2016 at 19:17

1 Answer 1

2

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.

You do not need all the nested foreach loops to accomplish this. Instead you iterate over the sub-array only.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.