B
B
bismoney2021-06-28 04:39:04
PHP
bismoney, 2021-06-28 04:39:04

How to fix a bug after updating php 7 and Yii2?

Guys please help, I'm weak...

There is a code that saves the selected products:

// сохраняем открытые продукты
      foreach ($this->openProducts as $keyIdProj => $projProdOpen) {

        foreach ($projProdOpen as $key => $value) {

          for ($x = 0; $x < count(array($value)); $x++) {
            if (!$value[$x]) {
              continue;
            }



            $rec = new ProductsOpen();
            $rec->id_user = $this->id;
            $rec->id_product = $value[$x];
            $rec->save();
            if ($rec->errors) {
              //var_dump($rec->errors);
              exit();
            }

            $openProduct[] = $rec;
          }
        }

      }


Since there are 3 projects so far
var_dump($this->openProducts);

Gives out 3 arrays
It is in one of them that I opened 3 new pages with the IDs that are there.

array (size=3)
  1 => 
    array (size=2)
      'products' => string '' (length=0)
      'packs' => string '' (length=0)
  2 => 
    array (size=2)
      'products' => 
        array (size=3)
          0 => string '175' (length=3)
          1 => string '176' (length=3)
          2 => string '186' (length=3)
      'packs' => string '' (length=0)
  3 => 
    array (size=2)
      'products' => string '' (length=0)
      'packs' => string '' (length=0)


Watching data in a loop

foreach ($this->openProducts as $keyIdProj => $projProdOpen) {
        var_dump($projProdOpen); exit;


It turns out he takes the first and empty

array (size=2)
  'products' => string '' (length=0)
  'packs' => string '' (length=0)


It is logical that:

foreach ($projProdOpen as $key => $value) {

          var_dump($value); exit;


Issues:
:138:string '' (length=0)

And accordingly we get a php7 error that count will not count as empty, it needs an array.

for ($x = 0; $x < count($value); $x++) {

I do not even know how to return everything to work after the update.
How to make it select only filled arrays?


That is, it could be like this:
array (size=3)
  1 => 
    array (size=2)
      'products' => string '' (length=0)
      'packs' => string '' (length=0)
  2 => 
    array (size=2)
      'products' => 
        array (size=3)
          0 => string '175' (length=3)
          1 => string '176' (length=3)
          2 => string '182' (length=3)
      'packs' => string '' (length=0)
  3 => 
    array (size=2)
      'products' => 
        array (size=1)
          0 => string '214' (length=3)
      'packs' => string '' (length=0)


And so too:
array (size=3)
  1 => 
    array (size=2)
      'products' => 
        array (size=5)
          0 => string '150' (length=3)
          1 => string '166' (length=3)
          2 => string '149' (length=3)
          3 => string '169' (length=3)
          4 => string '181' (length=3)
      'packs' => string '' (length=0)
  2 => 
    array (size=2)
      'products' => 
        array (size=5)
          0 => string '175' (length=3)
          1 => string '176' (length=3)
          2 => string '182' (length=3)
          3 => string '183' (length=3)
          4 => string '185' (length=3)
      'packs' => string '' (length=0)
  3 => 
    array (size=2)
      'products' => 
        array (size=1)
          0 => string '214' (length=3)
      'packs' => string '' (length=0)


Also, as far as I know, even if everything is solved, then string count will not be considered to be converted to int?

I ask without stones, I just figure it out and it hit my head to update everything ... 6 hours already ... I have cookies with coffee :)

If something is not clear, I will add ....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2021-06-28
@bismoney

Don't like the fact that php began to beat hands for shitty code? :)
What's the problem with checking if an array comes there?
https://www.php.net/manual/ru/function.is-array.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question