There is a cycle:

foreach ($attribute_groups as $attribute_group) { echo $attribute_group['name']; foreach ($attribute_group['attribute'] as $attribute) { echo $attribute['name']; echo $attribute['text']; } } 

I know that in the $attribute_group array, how can I check the value of $attribute_group['name'] before the loop? For example, you need the if ($attribute_group['name']) condition to do one, if not, then another, etc. I know that the $attribute_group array is declared in a loop, but how do I get $attribute_group['name'] before this loop? Separate cycle? Then tell me how to implement it correctly, thank you.

Updated question

In general, I decided to check the condition in the loop, this is how I want to do

 <?php if($attribute_groups) { ?> <?php foreach ($attribute_groups as $attribute_group) { ?> <?php if ($attribute_group['name']=='Технические характеристики') { ?> <div class="tab-pane" id="tab-harakter"> <table class="table"> <?php foreach ($attribute_group['attribute'] as $attribute) { ?> <tr> <td><?php echo $attribute['name']; ?></td> <td><?php echo $attribute['text']; ?></td> </tr> <?php } ?> </table> </div> <?php } else { ?> <p>Технические характеристики отсутствуют</p> <?php } ?> <?php if ($attribute_group['name']=='Документы') { ?> <div class="tab-pane" id="tab-doc"> <table class="table"> <?php foreach ($attribute_group['attribute'] as $attribute) { ?> <tr> <td><?php echo $attribute['name']; ?></td> <td><?php echo $attribute['text']; ?></td> </tr> <?php } ?> </table> </div> <?php } else { ?> <p>Документация отсутствует</p> <?php } ?> <?php if ($attribute_group['name']=='Сертификаты') { ?> <div class="tab-pane" id="tab-sertif"> <table class="table"> <?php foreach ($attribute_group['attribute'] as $attribute) { ?> <tr> <td><?php echo $attribute['name']; ?></td> <td><?php echo $attribute['text']; ?></td> </tr> <?php } ?> </table> </div> <?php } else { ?> <p>Сертификаты отсутствуют</p> <?php } ?> <?php } ?> <?php } ?> 

But it displays incorrectly on the page, I blame it on the fact that I am doing a wrong check in the loop, tell me what is wrong?

  • Not a very correct question. Given that $attribute_group is a member of $attribute_groups values ​​of $attribute_group['name'] will be several. Which one do you want to check? If each is only in a cycle, if there is some specific $i -th (for example, the first one), then contact directly the $attribute_groups[$i]['name'] index - tutankhamun
  • no, I need every $attribute_group['name'] , depending on this value, I output the relevant information, but my cycle is dynamic and therefore the values ​​of $attribute_group['name'] may not be there, so I can't do it . I will come home a little specific question. - Denis
  • Yes, the question needs to be specified. - Ipatiev

5 answers 5

If you know in advance the value that needs to be checked, then you can:

The value that we check: 'testValue';

 foreach ($attribute_groups as $attribute_group) { if( in_array('testValue', $attribute_group) ) // Делаем что tо } } 
  • your method is not suitable, because array $attribute_groups is an array of arrays - Denis
  • You write this code at the beginning of the first foreach. And everything will work. Only not "$ attribute_groups" as I have written, but "$ attribute_group" - user190134
  • I corrected the code - user190134
  • Well, in general, it was originally necessary to check the value before the loop, this code does not solve this problem, and how does in_array differ from a simple value check, such as $attribute_group['name']=='Сертификаты' did not understand the behavior - Denis

An example that is close to your code:

 <?php $attribute_groups = [ ['name'=> 'group 1', 'attribute'=>['name'=>'attr1', 'text'=>'some text 1']], ['name'=> 'group 2', 'attribute'=>['name'=>'attr2', 'text'=>'some text 2']] ] ?> <?php foreach($attribute_groups as $attribute_group): ?> <!-- проверим для примера, является ли элемент name пустым--> <?php if(!empty ($attribute_group['name'])){?><!--if--> <!--если да, то--> <?php foreach ($attribute_group['attribute'] as $attribute) { ?> <tr> <td><?php echo $attribute; ?></td> <br/> </tr> <?php } ?> <?php } else { ?><!--else--> <!-- если нет, то--> <?php } ?><!--endif--> <?php endforeach ?> 

    I hope correctly understood the task. Until which cycle do you need to handle the name ? And why do you need to go there at all? You can add test data for $ attribute_groups to the question

     <?php foreach ($attribute_groups as $attribute_group) : ?> <p><b><?php echo $attribute_group['name']; ?></b></p> <?php if($attribute_group['attribute']['name']): ?> <!-- Тут что-то делаем --> <?php endif;?> <tr> <td><?php echo $attribute['name']; ?></td> <td><?php echo $attribute['text']; ?></td> </tr> <?php endforeach; ?> 
      1. Perhaps at the entrance here is a structure - arrays in an array with finite elements.
      2. When debugging a structure, do not complicate the code, but debug its model in advance.
      3. In TYPE, the function "foreach (array as key => element) {}" is written: element):?>
      4. The same rule applies to the operator conditions: ""

        To get independent values ​​of the array elements inside the foreach , in its parameters the element key is specified, for example, under the name $key , i.e.

         <?php foreach ($attribute_groups as $key =>$attribute_group) { 

        // - Code using $ key, $ attribute_groups [$ key], $ attribute_group and any other arrays

         }