HenryDev HenryDev - 6 months ago 14
PHP Question

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.

Here's a picture of my table if I do echo print_r($value3);

enter image description here

Here's my array of 10 customers:

(
[status] => Array
(
[resultCode] => 00000
[context] => COMPLETED
)

[searchUserResults] => Array
(
[searchUserResult] => Array
(
[0] => Array
(
[userId] => AllieCraig
[phoneNumber] => 13039453156
[extension] => 3156
[lastName] => Craig
[firstName] => Allie
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[1] => Array
(
[userId] => 3038003968
[extension] => 1010
[lastName] => Abbott_withAReallyLongLastName
[firstName] => Kim_withAReallyLongFirstNamexx
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[2] => Array
(
[userId] => 3038003943
[phoneNumber] => 13038003943
[extension] => 3943
[lastName] => Liz
[firstName] => Voytko
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[3] => Array
(
[userId] => 3038003970
[phoneNumber] => 13038003970
[extension] => 3970
[lastName] => Spree
[firstName] => Ed
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[4] => Array
(
[userId] => 3038003941
[phoneNumber] => 13038003941
[extension] => 3941
[lastName] => Janos
[firstName] => Ben
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[5] => Array
(
[userId] => kcypher3969
[phoneNumber] => 13038003969
[extension] => 3969
[lastName] => Cypher
[firstName] => Keanan
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[6] => Array
(
[userId] => 4085121974
[phoneNumber] => 14085121974
[extension] => 1974
[lastName] => Wainio
[firstName] => Steve
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[7] => Array
(
[userId] => 4085121976
[phoneNumber] => 14085121976
[extension] => 1976
[lastName] => LastName
[firstName] => FirstName
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[8] => Array
(
[userId] => hgranados
[phoneNumber] => 13038003945
[lastName] => Granados
[firstName] => Henry
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[9] => Array
(
[userId] => mthan200
[phoneNumber] => 13038003942
[extension] => 3942
[lastName] => Than devName
[firstName] => My devName
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

[10] => Array
(
[userId] => pshrestha3944
[phoneNumber] => 13038003944
[extension] => 3944
[lastName] => Shrestha
[firstName] => Prerana
[externalAccountId] => JimEckles
[groupName] => JimEckles
[externalEnterpriseId] => JimEckles
)

)

)

)


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

Answer

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.