an array of this nature

array() { [0] => { array(3) { ["pk"]=> string(8) "Abonents" ["fields"]=> array(11) { ["sms"]=> string(12) "+71234567890" ["name"]=> string(52) "Петров Петя Петрович" } } 

array of rest api curl takes from billing, I try to do a search by phone number and output the values ​​of that fields array in which there is this script number like this:

 `<?php <style type="text/css"> table { border-collapse: collapse; } table th, table td { padding: 0 5px; border: 1px solid #000; text-align: center; } </style> <table> <tr> <td rowspan="2"></td> <td colspan="4"></td> </tr> <tr> <td>ФИО</td> <td>Номер телевона</td> <td>Номер договора</td> <td>Аккаунт ID</td> </tr> <?php $phone = "+79122075335"; foreach ($extract as $key => $value) { if($value == $phone) { echo '<tr> <td>'.$key['fields']['create_date'].'</td> <td>'.$key['fields']['name'].'</td> <td>'.$key['fields']['sms'].'</td> <td>'.$key['fields']['contract_number'].'</td> <td>'.$key['fields']['account_id'].'</td> <td></td> </tr>'; } } ?> </tbody> </table>` 

but I have an empty table, please tell me where is the error, I'm new to php ...

  • try $ value ['fields'] ['create_date'] etc. - Kirill Korushkin
  • Thanks works now I fasten the button, the button issues an empty table - Mikhail Pelevin
  • button <html> <head> <meta charset = "utf-8"> <form action = "name.php" method = "post"> <p> Search by phone number, format: + 7xxxxxxxxxx </ p> < p> Enter the number: <input type = "text" name = "phone" /> </ p> <p> <input type = "submit" /> </ p> </ form> </ html> in the script I use $ _POST variable - Mikhail Pelevin

1 answer 1

In your case, $ value is:

 [ "pk"=> "Abonents", "fields" => [ "sms" => "+71234567890", "name" => "Петров Петя Петрович", ] ] 

If you are looking for the number that lies in the sms then the condition should be

 if($value['fields']['sms'] == $phone) {...} 

If these values ​​may not be in the array, then do not forget to add a check for their presence.

$ key in your case will be equal to 0, because this is exactly the key of the array for which you are doing foreach. Therefore, even if you find something, the table will still be empty. Should be replaced by:

 echo '<tr> <td>'.$value['fields']['create_date'].'</td> <td>'.$value['fields']['name'].'</td> <td>'.$value['fields']['sms'].'</td> <td>'.$value['fields']['contract_number'].'</td> <td>'.$value['fields']['account_id'].'</td> <td></td> </tr>'; 

PS: Read this article ( en ) to find out how to solve such problems in the future.