Array inside the foreach returns last column of the database

Asked

Viewed 32 times

0

The problem is that this way, return only the last column of the database

public function Acentos ( $string ) {

   $Acentos = $this->SelectDados(
       "*",
       "table",
       "",
       array ()
   );

   foreach ( $Acentos as $List ) {

       $table = array (

          $List['slug'] => utf8_encode ( $List['nome'] )

        );

    }

    return strtr ( $string, $table );
}

I tried to do so

$Dados = array ();

foreach ( $Acentos as $List ) {

    $table = array (

       $Dados[] = $List['slug'] => utf8_encode ( $Dados[] = $List['nome'] )

    );

 }

1 answer

1

You are declaring a new array every time you pass the foreach, try using an example push array

public function Acentos ( $string ) {

   $table = array();
   $Acentos = $this->SelectDados(
       "*",
       "table",
       "",
       array ()
   );

   foreach ( $Acentos as $List ) {

       array_push($table,

          $List['slug'] => utf8_encode ( $List['nome'] )

        );

    }

    return strtr ( $string, $table );
}

References

array_push > https://www.php.net/manual/en/function.array-push.php

array > https://www.php.net/manual/en/language.types.array.php

Scopes > https://www.php.net/manual/en/language.variables.scope.php

  • Parse error</b>: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' $List['slug'] => utf8_encode ( $List['nome'] )

Browser other questions tagged

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