0

The problem is that this way is only returning 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 like this

$Dados = array ();

foreach ( $Acentos as $List ) {

    $table = array (

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

    );

 }

With array_push gives the following error

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 );
 }

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')'

As I understand it array_push does not accept special characters => how to fix this?

mehid
  • 47
  • 6
  • Use `$table[] = …` to add a new element to that array. – 04FS Oct 29 '19 at 09:41
  • `syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')'` – mehid Oct 29 '19 at 09:44
  • _“As I understand it array_push does not accept special characters `=>`”_ - this has nothing to do with array_push in particular, but with you not being aware of context. This syntax is for use _inside_ of an array context - so either `array( … )` or `[ … ]` – 04FS Oct 29 '19 at 09:47

1 Answers1

1

The error

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')'

means that PHP expected something different than =>.

Cause:

You're creating an array without the array keyword:

foreach ( $Acentos as $List ) {

        array_push ( $Table,

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

     );

  }

You must define the new array passed to array_push as this:

foreach ( $Acentos as $List ) {

        array_push ( $Table,

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

     );

  }

Complete code:

public function Acentos ( $string ) {

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

    foreach ( $Acentos as $List ) {

        array_push ( $Table,

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

     );

  }

  return strtr ( $string, $Table );
 }
user2342558
  • 5,567
  • 5
  • 33
  • 54