Good day!

I have the following variables from the database:
1. a: 1: {i: 0; i: 53;} 2. a: 2: {i: 0; i: 52; i: 1; i: 53;} 3. a: 1: {i: 0 ; i: 53;} 4. a: 1: {i: 0; i: 54;}

They contain the numbers 52, 53, 54, 55 and 56.

I need to catch them and show the user a normal look. 1. BBB 2. AAA, BBB 3. BBB 4. CCC

I understand that you can stupidly do - If the line contains 53, then show the BBB. It's clear. But when there are combinations, then I start to blunt. Do not write all the possible options ... please tell me what and how to use?

  • one
    nothing is clear - etki
  • I will try to explain more clearly. I have this column: row [2] a: 1: {i: 0; i: 52;} a: 1: {i: 0; i: 52;} a: 1: {i: 0; i: 52;} a: 1: {i: 0; i: 52;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a: 2: {i: 0; i: 52; i: 1; i: 53;} a : 2: {i: 0; i: 52; i: 1; i: 53;} a: 1: {i: 0; i: 52;} I need each row of this column to be replaced by a letter. If the line contains the numbers 52 and 53, then AAA and BBB would be written. If the line is only the number 52, then only AAA. If 52 and 54, then AAA and CCC. - Nxnx

3 answers 3

$str = 'a:2:{i:0;i:52;i:1;i:54;}'; $data = unserialize($str); // десериализуем $map = [52 => 'AAA', 53 => 'BBB', 54 => 'CCC']; foreach ($data as $k) { echo $map[$k]."\n"; // сопоставляем со строками } 

Conclusion:

 AAA CCC 
  • Thank you very much, the most it. And I got into the jungle)) - Nxnx

It is difficult to understand the essence of your question, if I understood correctly, you need to get the data from the string, and since a string is data that serialize ; to get a readable format, you need to use the unserialize() function:

 <?php $str1 = 'a:1:{i:0;i:53;}'; $str2 = 'a:1:{i:0;i:53;}'; echo unserialize($str1); echo unserialize($str2); /* результат $str1 array ( 0 => 53, ) результат $str2 array ( 0 => 52, 1 => 53, ) ...... */ ?> 

    I thought of it. Crooked probably, but it works. If someone tells me how to write correctly, I will be very grateful.

     <?php $a = 'a:2:{i:0;i:52;i:1;i:53;}'; if (strpos($a, '52') !== false) // жесткое сравнение { $ct1 = ААА; echo '52 Найдено'; } else { echo '52 Не найдено'; } if (strpos($a, '53') !== false) { $ct2 = БББ; echo '53 Найдено'; } else { echo '53 Не найдено'; } if (strpos($a, '54') !== false) { $ct3 = ВВВ; echo '54 Найдено'; } else { echo '54 Не найдено'; } if (strpos($a, '55') !== false) { $ct4 = ГГГ; echo '55 Найдено'; } else { echo '55 Не найдено'; } if (strpos($a, '56') !== false) { $ct5 = ДДД; echo '56 Найдено'; } else { echo '56 Не найдено'; } $CTA = $ct1.'/ '.$ct2.'/ '. $ct3.'/ '. $ct4.'/ '.$ct5; echo $CTA; ?> 

    • In the next answers have already suggested: unserialize you to help - Dmitriy Simushev