Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

array map isnt working in php

I am trying to use this function to convert danish characters to utf

private function process_elements($element){
   $element=  strtolower ( trim ( $element ) );
   $element=  mysql_real_escape_string($element);
    return utf8_encode($element);
}

my array is used without the encoding like this:

Array ( [0] => Desktop [1] => imlive [2] => dk [3] => Danish [4] => Denmark [5] => http://www.google.dk/search?ie=UTF-8&oe=UTF-8&hl=da&q=imlive&adtest=on&gl=DK&glp=1&ip=0.0.0.0&pws=0&noj=1&nomo=1 [6] => ImLive - Fr�kke cam-piger [7] => Tusindvis af rigtige amat�rer live! [8] => Fra private hjem og sovev�relser [9] => dk.imlive.com [10] => ImLive - Fr�kke cam-piger [11] => dk.imlive.com [12] => Tusindvis af rigtige amat�rer live! Fra private hjem og sovev�relser [13] => ImLive - Fr�kke cam-piger - Tusindvis af rigtige amat�rer live! [14] => dk.imlive.com [15] => Fra private hjem og sovev�relser [16] => ImLive - Fr�kke cam-piger [17] => Tusindvis af rigtige amat�rer live! Fra private hjem og sovev�relser [18] => dk.imlive.com )

here is my implementation:

array_map('self::process_elements', $data);

the function is in a class...

later i set such queries. for example:

        mysql_query("SET NAMES 'utf8'");
         $query="INSERT INTO advert
                 SET device='$device',
                 keyword='$keyword',
                 google_domain='$google_domain',
                 language='$language',
                 country='$country',
                 check_url='$check_url',
                 task_id=$this->task_id";

       mysql_query($query) or die(myql_error());

but it throws exceptions that the a characters are wrong.. because before i inpu thtem, they are not utf.. why the array map function doesnt work?!

Upvotes: 0

Views: 567

Answers (3)

Petro Popelyshko
Petro Popelyshko

Reputation: 1379

You cannot change initial values in that array, because array_map() returns an new array with changed values, it is not passed by reference.

$newArray = array_map('self::process_elements', $data);

Upvotes: 1

472084
472084

Reputation: 17885

Take a look at this thread: Passing object method to array_map()

Try:

array_map(array($this, $this->process_elements), $data);

Upvotes: 2

sofl
sofl

Reputation: 1024

Quote from php.net

If you need to call a static method from array_map, this will NOT work:

<?PHP array_map('myclass::myMethod' , $value); ?>

Instead, you need to do this:

<?PHP array_map( array('myclass','myMethod') , $value); ?>

It is helpful to remember that this will work with any PHP function which expects a callback argument.

http://php.net/manual/de/function.array-map.php

Upvotes: 1

Related Questions