1

I've got this array (print from point [A] in code below):

Array
(
    [codigo] => 
    [titulo] => Array
        (
            [0] => musi
            [1] => bach
        )
    [titulo_operador] => OR
    [resumo] => 
    [autor] => 
    [ano_min] => 
    [ano_max] => 
    [tipo] => 
    [categoria] => 
    [palavra_chave] => 
    [idioma] => 
)

in which I perform these operations... (code is intentionally not-optimized, it could be leaner)

if (!empty($data['titulo'])) {
    if (is_array($data['titulo'])) {
        $query .= " AND (";
        //print_r($data); point [A]
        foreach ($data['titulo'] as $value) {
            $query_temp .= " ".$data['titulo_operador']." aid.name LIKE '%".$value."%'";
        }
        //print_r($data); point [B]
        $query_temp = trim($query_temp, " ".$data['titulo_operador']." ");
        $query .= $query_temp;
        unset($query_temp);
        $query .= ")";
    } elseif (is_string($data['titulo'])) {
        $query .= " AND aid.name LIKE '%".$data['titulo']."%'";
    }
}

After the foreach, $data['idioma'] is set to 'bach'. This is the print_r in point [B]:

Array
(
    [codigo] => 
    [titulo] => Array
        (
            [0] => musi
            [1] => bach
        )
    [titulo_operador] => OR
    [resumo] => 
    [autor] => 
    [ano_min] => 
    [ano_max] => 
    [tipo] => 
    [categoria] => 
    [palavra_chave] => 
    [idioma] => bach
)

I've removed the $query_temp statement, leaving an empty foreach there and this still happens, so I'm pretty sure there's something in the foreach there... Does foreach in a subarray alter anything in parrent array too?

Edit: I assigned other elements to $data['titulo'] and $data['idioma'] always gets the value of the last element.

2
  • This seems very unlikely to be a PHP bug. Can you replicate the issue in e.g. http://3v4l.org/ ? Commented May 3, 2015 at 18:13
  • Thanks for the heads up. While copying my code, this issue indeed didn't happen. It made me realize problem was elsewhere. Commented May 3, 2015 at 18:23

1 Answer 1

1

Found the solution while trying to replicate the issue on eval, thanks to the tip of Jon Stirling. Problem was not on that block of code. This block, that was running just after the array creation, was causing the issue:

foreach ($data as $key => &$value) {
    $data[$key] = conferir($data[$key]);
}

Removing that reference on this foreach statement prevented that odd behavior on the other foreach:

foreach ($data as $key => $value) {
    $data[$key] = conferir($data[$key]);
}

Once again, thanks, Jon Stirling, for pointing me to 3v4l. Copying my code made me find the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.