mehid
mehid

Reputation: 47

Difficulty converting array to string

how can i convert this array to string?

public static function generos($string) {
   return preg_replace(array("/(natacao)/"),explode(" ","Natação"), $string);
}

public function categoria($string) {
    $this->exp = explode(",", $string);
    foreach ($this->exp as $this->list) :
        return $this->generos(implode(",", $this->list));
    endforeach;
}

echo '<li>'.$this->categoria($rows[] = $this->row['categoria']).'</li>';

table

id  |  categoria  
 1  |  futebol, voleiball

Current results

<li>futebol, voleiball</li>

expected result

<li>futebol</li>
<li>voleiball</li>

Upvotes: 0

Views: 47

Answers (1)

Micheal Don
Micheal Don

Reputation: 48

add your array's value to another variable like this:

<?php
    $dizi=array('mother','father','child'); //This is array

    $sayi=count($dizi);
    for ($i=0; $i<$sayi; $i++) {
        $dizin.=("'$dizi[$i]',"); //Now it is string...
    }
         echo substr($dizin,0,-1); //Write it like string :D
?>

In this code we added $dizi's values and comma to $dizin:

$dizin.=("'$dizi[$i]',");

Now

$dizin = 'mother', 'father', 'child',

It's a string, but it has an extra comma :)

And then we deleted the last comma, substr($dizin, 0, -1);

Output:

'mother','father','child'

Upvotes: 1

Related Questions