Loop foreach error: syntax error, Unexpected 'foreach' (T_FOREACH), expecting ')

Asked

Viewed 902 times

-4

Can anyone tell me what I did wrong in this job? can’t find the error.

public function inserir_fotos() {

    $fotos = $this->input->post('fotos');
    $data = array(

    foreach ( $fotos as $item => $value){
        array(
            'idImovel' => $this->input->post('idImovel'),
            'imgImovel' => $value
        ),
    }
    );

        return $this->db->insert_batch('fotos', $data);

}
  • Beyond the array that is declared inside the foreach without assignment to any variable?

  • 1

    Typo, you put a foreach within an array, as far as I know, this does not exist in PHP. To add items to an array use $data[] = or array_push($data, ...).

  • I suggest using sublime text with the Phplinter extension installed. Then you will see what is wrong before saving the code :)

  • Very fast guys on the trigger!!! Thanks even!!!!

  • A hint next time you ask something, add the error message, in case I already edited, the error is: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'

2 answers

4

You can’t put foreach, for, while inside an array directly, as far as I know, I’ve never seen a programming language that allows/supports such a thing.

In php you should use this:

$data = array('a', 'b');
$data[] = 'foo';

print_r($data);//output: array('a', 'b', 'foo');

The code should be:

public function inserir_fotos() {

    $fotos = $this->input->post('fotos');
    $data = array();

    foreach ( $fotos as $item => $value){
        $data[] = array(
            'idImovel' => $this->input->post('idImovel'),
            'imgImovel' => $value
        );
    }

    return $this->db->insert_batch('fotos', $data);

}
  • That way at each iteration he wouldn’t overwrite the content of $data[]?

  • @Richarddias No, the [] the keys in front are equivalent to the push array_push, if you read the documentation of the push array_push you will notice that they talk about the []. http://php.net/manual/en/function.array-push.php#refsect1-Function.array-push-Description -- Note: If you use push() array_push() to add an element to the array, it is best to use $array[] = because this way there is no call to a function.

  • Vlw @Guilhermenascimento, thanks for the tip.

  • Very good @Guilhermenascimento I think I was traveling even in the function...

  • Glad we could help @Jacielplacidino. If one of the answers satisfied you, mark it as correct, as shown on the tour: http://answall.com/tour. by clicking on the "sign"

1


You cannot use a repeat loop within a variable statement. The right is for you to create the variable and change its value, in this case adding items in the array.

Try to do as below:

public function inserir_fotos() {

    $fotos = $this->input->post('fotos');
    $data = array();

    foreach ( $fotos as $item => $value){
        array_push($data, array(
            'idImovel' => $this->input->post('idImovel'),
            'imgImovel' => $value
        ));
    }

    return $this->db->insert_batch('fotos', $data);

}
  • upon request, thank you very much!!!

  • If you helped solve your problem, choose an answer as the correct one

  • I chose your answer because it was the one I used in Script but I understood a lot!

Browser other questions tagged

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