How to extract information from an Array in MySQL?

-1

Well, I would like to know how to pull the information from array from my database, it follows how it is in my database:

Insummary,I'dliketoknowhowtoextractthisinformation:{'pt'=>'HolidayPromotion','en'=>'VacationPromotion'}atthetimeofpassingPHP.

Codeyouwereusingtoextractfromthedatabase:

$stm=$DB->readDB('news','WHEREFeatured="yes" ORDER BY ID, RAND() DESC LIMIT 1');
foreach ($stm as $key) {
    $title = eval($key['Title'][0]);
    $url = $Translate->set('href')['news'].'loja/'.$key['ID'].'-'.$Modulo->tirarAcentos($title, true);

Code you were using to save to the database:

$stm = $DB->createDB('news', 'Title, SubTitle, Category, Message, Featured', '"{'.U_LANG.':'.$title.'}", "{'.U_LANG.':'.$subtitle.'}", $category, $message, "yes"');
return $stm;
    
asked by anonymous 08.10.2018 / 00:02

1 answer

1
  • Convert the string to JSON format using the str_replace () , substituting% with% with% with% (its string is a mixture of PHP array and JSON format).
  • Then you can use the json_decode () with the second parameter set to => , to get an associative array

see it working here

    $retornoBanco= '{"pt" => "Promoção de Férias","en" => "Vacation Promotion"}';
    $retornoBanco=str_replace('=>',':',$retornoBanco);

    $array = json_decode($retornoBanco, true);

    print_r($array);

    echo 'Português '.$array["pt"].PHP_EOL;
    echo 'Inglês    '.$array["en"].PHP_EOL;
  

Ideally it would save to the bank in a valid : format, so you would only need to use true

 {"pt":"Promoção de Férias","en":"Vacation Promotion"}
  

thus avoiding replace

    
08.10.2018 / 01:24