解析数据集

时间:2017-07-06 14:27:16

标签: php arrays wordpress parsing

使用Wordpress函数wp_load_alloptions()我得到一个包含所有网站选项数据的大型数组。我选择了一部分我需要从中全局获取数据。

$all_options = wp_load_alloptions();
$my_options = array();
foreach( $all_options as $name => $value ) {
   if(stristr($name, 'options')) $my_options[$name] = $value;
}

echo '<pre>';
   print_r($my_options['sl_options_options']);
echo '</pre>';

返回:

s:15:"popup_form_text";s:0:"";s:14:"popup_form_URL";s:124:"url";s:15:"popup_form_open";s:2:"14";s:16:"popup_form_close";s:2:"23";s:15:"evergage_toggle";s:2:"On";s:12:"optim_toggle";s:2:"On";s:13:"soasta_toggle";s:3:"Off";}

有没有办法可以解析这个数据集来分解每个部分。各个部分的例子:

s:12:"optim_toggle";s:2:"On";

2 个答案:

答案 0 :(得分:0)

你想要这样的东西吗? :

preg_split('/;|,/', 's:15:"popup_form_text";s:0:"";s:14:"popup_form_URL";s:124:"url";s:15:"popup_form_open";s:2:"14";s:16:"popup_form_close";s:2:"23";s:15:"evergage_toggle";s:2:"On";s:12:"optim_toggle";s:2:"On";s:13:"soasta_toggle";s:3:"Off";}');

输出:

array(15) {
  [0] =>
  string(22) "s:15:"popup_form_text""
  [1] =>
  string(6) "s:0:"""
  [2] =>
  string(21) "s:14:"popup_form_URL""
  [3] =>
  string(11) "s:124:"url""
  [4] =>
  string(22) "s:15:"popup_form_open""
  [5] =>
  string(8) "s:2:"14""
  [6] =>
  string(23) "s:16:"popup_form_close""
  [7] =>
  string(8) "s:2:"23""
  [8] =>
  string(22) "s:15:"evergage_toggle""
  [9] =>
  string(8) "s:2:"On""
  [10] =>
  string(19) "s:12:"optim_toggle""
  [11] =>
  string(8) "s:2:"On""
  [12] =>
  string(20) "s:13:"soasta_toggle""
  [13] =>
  string(9) "s:3:"Off""
  [14] =>
  string(1) "}"
}

您可以轻松优化它。

答案 1 :(得分:0)

使用unserialize我能够像我想要的那样打印出数据。

序列化数组:

s:15:"popup_form_text";s:0:"";s:14:"popup_form_URL";s:124:"url";s:15:"popup_form_open";s:2:"14";s:16:"popup_form_close";s:2:"23";s:15:"evergage_toggle";s:2:"On";s:12:"optim_toggle";s:2:"On";s:13:"soasta_toggle";s:3:"Off";}

使用unserialize

$unserialized_array = unserialize(s:15:"popup_form_text";s:0:"";s:14:"popup_form_URL";s:124:"url";s:15:"popup_form_open";s:2:"14";s:16:"popup_form_close";s:2:"23";s:15:"evergage_toggle";s:2:"On";s:12:"optim_toggle";s:2:"On";s:13:"soasta_toggle";s:3:"Off";});

显示新阵列:

print_r(unserialize($unserialized_array));

数组现在显示为:

Array(
   [popup_form_toggle] => On
   [popup_form_postids] => 51819, 36057, 36061, 36044, 44375, 36073, 48713, 17, 34612, 15908, 51054, 41102, 894
   [popup_form_text] => 
   [popup_form_URL] => https://home-c8.incontact.com/inContact/ChatClient/ChatClientPatron.aspx?poc=5ca78194-54ab-4833-8685-934a9d0305cc&bu=4594385
   [popup_form_open] => 14
   [popup_form_close] => 23
   [evergage_toggle] => On
   [optim_toggle] => On
   [soasta_toggle] => Off
)