PHP / Codeigniter error "Array to string conversion"

1

I made a foreach to add values to an insert but suddenly there was a strange Array to string conversion error and I have no idea what that 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."')";
            }   
        }


    }
    
asked by anonymous 18.02.2014 / 15:41

4 answers

3

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

Here is 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);
}
    
18.02.2014 / 15:47
2

Use count () or sizeof () to get size of arrays. In your code you can cache the value of the $ branch array size outside the loop to avoid calling the count () function at each 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 just because of the last comma, and you do not have any 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, ',');
    
18.02.2014 / 15:51
1

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

In this case, I recommend giving a print_r () or a var_dump () in $ _POST ['branches'] and $ _POST ['data'] and check what format this json is in order to handle it properly .

Another thing I advise is to use the CodeIgniter Active Record ( Here ) to do this insert, and perform insert handling BEFORE, not during execution of it.

I mention this because it is preferable to treat before or mount the structure of what will be inserted earlier than during the insertion process.

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

I hope I have helped

    
18.02.2014 / 15:56
1

A smaller version of 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)        
            $insert.="('".$key."','".$value."'),";                     
        $insert = substr($insert,0,strlen($insert)-1); // retirar a ultima virgula;         
}
    
18.02.2014 / 17:22