PHP/Codeigniter error "Array to string Conversion"

Asked

Viewed 1,855 times

1

I did a foreach to go adding values to an Insert but suddenly an error appeared Array to string Conversion strange and have no idea what it is.

public function distribuicaoprodutos(){
        $filiais=json_decode($_POST['filiais'],TRUE);
        $dados=json_decode($_POST['dados'],TRUE);
        $insert="INSERT INTO movimentacao (Filial, Quantidade) VALUES ";
        $i=0;
        foreach($filiais as $key => $value)
        {
            $i++;
            if($value!="" && strlen($filiais)>$i) //linha de erro
            {
                $insert.="('".$key."','".$value."'),";  
            }elseif($value!="" && strlen($filiais)==$i){
                $insert.="('".$key."','".$value."')";
            }   
        }


    }
  • Which line of error?

  • changed the question...

4 answers

3


To measure the size of an array, the function to be used is sizeof, and not strlen (that measures string size).

Follow a suggested change to your code:

public function distribuicaoprodutos()
{
    $filiais = json_decode($_POST['filiais'], true);
    $dados   = json_decode($_POST['dados'], true);
    $insert  = "INSERT INTO movimentacao (Filial, Quantidade) VALUES ";
    foreach ($filiais as $key => $value)
    {
        if (!empty($value)) {
            $insert .= "('" . $key . "', '" . $value . "'), ";
        }
    }
    $insert = substr($insert, 0, -2);
}
  • About the sizeof you are right but why you gave use to if is because in the filias array have empty fields and I can not register them

  • Got it. I made another edition based on your comment :)

2

Use the Count() or sizeof() to get arrays size.
In your code you can cache the value of the $branch array size out of the loop to avoid calling the Count() function every iteration.

Test like this:

 $i=0;
 $filiaisTamanho = count($filiais);
 foreach($filiais as $key => $value){
            $i++;
            if($value!="" && $filiaisTamanho>$i) //linha de erro
            {
                $insert.="('".$key."','".$value."'),";  
            }elseif($value!="" && $filiaisTamanho==$i){
                $insert.="('".$key."','".$value."')";
            }   
        }

But in fact, if it’s only because of the last comma, and there’s no more code that needs the counter $i, then you can use it like this:

foreach($filiais as $key => $value){
    if ($value != "") $insert.= "('".$key."','".$value."'),";
}
$insert= rtrim($insert, ',');

1

He’s saying you’re trying to use an array as a string The data coming via POST is in json format, so far, okay, but what is the structure of this JSON ? That is of paramount importance to address this problem.

In this case, I recommend giving a print_r() or a var_dump() in $_POST['affiliates'] and $_POST['data'] and check what format this json is in so that it can be treated properly.

Another thing I advise is to use Codeigniter’s Active Record ( Here ) to do this, and perform the treatment of the BEFORE, and not during the execution of it.

I say this because it is preferable to treat before or assemble the structure than it will be inserted before during the insertion process.

If you post this json or more or less its structure, maybe I can be more precise in my help

I hope I’ve helped

  • 1

    sorry but that’s not it... the mistake was that I was using strlen instead of sizeof, but still it was worth..

1

A version of your shortest code.

public function distribuicaoprodutos(){
        $filiais=json_decode($_POST['filiais'],TRUE);
        $dados=json_decode($_POST['dados'],TRUE);
        $insert="INSERT INTO movimentacao (Filial, Quantidade) VALUES  ";
        foreach($filiais as $key => $value)        
            $insert.="('".$key."','".$value."'),";                     
        $insert = substr($insert,0,strlen($insert)-1); // retirar a ultima virgula;         
}
  • friend, missed if checking if the value is empty...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.