V
V
vaflya2019-02-17 06:19:41
PHP
vaflya, 2019-02-17 06:19:41

Count() and sizeof() not working with multidimensional array?

array$data:

$data['films']
  $data['films'][$i]['title']
  $data['films'][$i]['cover']
  $data['films'][$i]['actors']
  $data['films'][$i]['genres']
$data['genres']
$data['genre']

php
$vars = $data['films'][$i];

  $count = count($vars['genres']);
  if ($count >0) {
  
    for ($i = 0; $i < $count; $i++) {
      echo '<a class="entry__tag" href="/'.$vars['genres'][$i]['url'].'">'.$vars['genres'][$i]['name'].'</a>, ';
    }
  }

The code does not work and loads the processor wildly. The page is not rendered. I tried sizeof(), the reason may be that not all movies have $vars['genres'].

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2019-02-17
@vaflya

Is it okay that you use the $i variable in the main loop and in the inner one?
They somehow overwrite each other, and at the output, FIG knows what is happening, most likely an eternal cycle.
foreach to help, both for the main loop and for the inner loop shown here.
Forget about for $i and that's it, an extra hassle.

D
Dmitry, 2019-02-17
@Compolomus

php.net/manual/en/function.count.php
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the number of array elements. This is especially useful for counting all elements of multidimensional arrays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question