How to extract information from an Array in Mysql?

Asked

Viewed 173 times

-2

Well, I’d like to know how to pull the information from array from my database, follow as it is in my database:

inserir a descrição da imagem aqui

In short, I’d like to know how I extract this information: {'en' => 'Holiday Promotion', 'en' => 'Vacation Promotion''} when it comes to PHP.

Code you were using to extract from the database:

$stm = $DB->readDB('news', 'WHERE Featured = "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;
  • This looks like a form, not a database. It would be better to show the SELECT performed, and the raw information.

  • When you fill out the form with your language, the system automatically translates into languages you add to your website, so when you send it to the database, it sends it as if it were a dict, Now I need to extract this dict pro site database with PHP, this is what I wish.

  • Is and about the => I fixed it, he’s like this now: 'pt':'Título'.

  • No, I do not wish to gambiarra, so I came for help, because in python i have the script, but I don’t know how to go through php, now help me so if you have the right shape, please.

  • Maybe json_encode and json_decode were better for your case (even so, maybe you still have a better way). json_encode to transform the array into a textual format, with pairs, and json_decode to recover. PHP also has serialize and unserialize

  • I didn’t understand one thing, in your screenshot you have 2 data between { }, but in your save code you have only a couple of name and value. If you’re only gonna get a pair back, you could just do something like $title= json_decode( $key['Title'] ); so you shall have $title['pt'] being "holiday promotion"

  • Take an example: https://ideone.com/fqM520

  • Since they invoked the word gambiarra nor would it be necessary to change anything at the bank, see https://ideone.com/cw9wuT

  • with one more language https://ideone.com/163fCa

  • Leo, if I didn’t ask you too much, could you put the contents of these 3 links in a comic one in an online notebook and send me the link? Why doesn’t my Chrome want to open these links

  • @Akatsuki test on your machine a separate script with these lines: <?php | $campo = '{"PT":"o rato","EN":"the mouse"}'; | $array = json_decode($campo, true); // true é array, false é objeto | print_r($array); | echo 'Português '.$array["PT"].PHP_EOL; | echo 'Inglês '.$array["EN"].PHP_EOL; - is what I had in my IDEONE, is that there you already see the result - remember to break the lines where I put the vertical bar (|)

  • blz Leo, Bacco worked yes this yours getting like this: Array ( [PT] => o rato [EN] => the mouse ) Português o rato Inglês the mouse

  • 1

    Mine is the same thing, Leo made a source of the code I had already sent and added the replace of the =>, that’s all.

  • Guys Thanks for everything EVEN, it worked perfectly: Português: Promoção de Férias

Show 9 more comments

1 answer

1


  • Convert the string to JSON format using the function str_replace(), replacing => for : (its string is a mixture of PHP array and JSON format).
  • Then you can use the function json_decode() with the second parameter defined as true, to obtain an associative array

see 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 would be save in bank in a format JSON valid, then you would only need to use the json_decode

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

thus avoiding the replace

  • And I will save soon so as not to have the replace

  • @Akatsuki, ok, mission accomplished! I will remove the answer

  • Leave it there You’ll know someone needs it in the future.

  • Thanks little brother, it worked perfectly: Português: Promoção de Férias

Browser other questions tagged

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