简体   繁体   English

PHP字符串数组转换

[英]PHP string array casting

I'm trying to find a way to cast the following into a real array. 我正在尝试找到一种将以下内容转换为真实数组的方法。 The data is stored as a string in the system I am working on. 数据以字符串形式存储在我正在使用的系统中。

I have tried (array) $stringArrayData; 我试过了(array) $stringArrayData; but that didn't work. 但这没用。

The string data is as follows: 字符串数据如下:

array(
  4 => array(
    'nid' => 4099,
    'cid' => '4',
    'pid' => '0',
    'form_key' => 'event_details',
    'name' => 'Event Details',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '1',
    'page_num' => 1,
  ),
  5 => array(
    'nid' => 4099,
    'cid' => '5',
    'pid' => '4',
    'form_key' => 'name',
    'name' => 'Name',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '2',
    'page_num' => 1,
  ),
  6 => array(
    'nid' => 4099,
    'cid' => '6',
    'pid' => '4',
    'form_key' => 'description',
    'name' => 'Description',
    'type' => 'textarea',
    'value' => '',
    'extra' => array(
      'description' => 'please include who the event is aimed at',
      'title_display' => 0,
      'private' => 0,
      'resizable' => 0,
      'disabled' => 0,
      'conditional_operator' => '=',
      'cols' => '',
      'rows' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '3',
    'page_num' => 1,
  ),
  21 => array(
    'nid' => 4099,
    'cid' => '21',
    'pid' => '4',
    'form_key' => 'add_an_image',
    'name' => 'Add an image',
    'type' => 'file',
    'value' => '',
    'extra' => array(
      'scheme' => 'public',
      'directory' => 'event-suggestions',
      'title_display' => 'before',
      'private' => 0,
      'progress_indicator' => 'throbber',
      'filtering' => array(
        'size' => '2 MB',
        'types' => array(
          'gif',
          'jpg',
          'png',
        ),
        'addextensions' => '',
      ),
      'conditional_operator' => '=',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '4',
    'page_num' => 1,
  ),
  7 => array(
    'nid' => 4099,
    'cid' => '7',
    'pid' => '4',
    'form_key' => 'venue',
    'name' => 'Venue',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '5',
    'page_num' => 1,
  ),
  23 => array(
    'nid' => 4099,
    'cid' => '23',
    'pid' => '7',
    'form_key' => 'please_select_your_venue',
    'name' => 'Please select your venue',
    'type' => 'select',
    'value' => '',
    'extra' => array(
      'items' => "3257|639 Enterprise Centre\n3267|Alexandra Palace\n126|Alexandra Park Library\n142|Asian Centre\n153|Bernie Grant Arts Centre\n127|Bruce Castle Museum\n3259|Community Use For the Old Station\n128|Coombes Croft Library\n147|Crowland Primary School\n146|Delicia Cafe\n138|Fortismere School\n3385|Haringey Sixth Form Centre\n135|Harmony Gardens Wins!\n149|Highgate Gallery\n129|Highgate Library\n130|Hornsey Library\n154|Jacksons Lane\n148|Kurdish Community Centre\n155|Lauderdale House\n3235|Living Under One Sun Community Allotment\n132|Marcus Garvey Library\n139|Methodist Church Hall\n133|Muswell Hill Library\n150|Northumberland Park Neighbourhood Resource Centre\n141|Open Smart Homes around Haringey\n156|Park Road Leisure Centre\n134|St Ann's Library\n157|Stroud Green and Harringay Library\n145|T Chances\n144|The Beehive Pub\n140|The Old Dairy\n136|The Tagore Centre\n143|Tottenham Community Sports Centre\n158|Tottenham Green Leisure Centre\n152|Tottenham Seventh-Day Adventist Church\n137|Triangle Centre\n151|Victoria Stakes\n159|White Hart Lane Community Sports Centre\n131|Wood Green Central Library\nother|Other\n",
      'options_source' => 'event_venue',
      'multiple' => 0,
      'title_display' => 'none',
      'private' => 0,
      'aslist' => 1,
      'optrand' => 0,
      'other_option' => NULL,
      'other_text' => 'Other...',
      'description' => '',
      'custom_keys' => FALSE,
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '12',
    'page_num' => 1,
  ),
  27 => array(
    'nid' => 4099,
    'cid' => '27',
    'pid' => '7',
    'form_key' => 'other_venue',
    'name' => 'Other Venue',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 'none',
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '14',
    'page_num' => 1,
  ),
  28 => array(
    'nid' => 4099,
    'cid' => '28',
    'pid' => '27',
    'form_key' => 'venue_company_name',
    'name' => 'Venue/Company name',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '15',
    'page_num' => 1,
  ),
  29 => array(
    'nid' => 4099,
    'cid' => '29',
    'pid' => '27',
    'form_key' => 'address_line_1',
    'name' => 'Address Line 1',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '16',
    'page_num' => 1,
  ),
  30 => array(
    'nid' => 4099,
    'cid' => '30',
    'pid' => '27',
    'form_key' => 'address_line_2',
    'name' => 'Address Line 2',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '17',
    'page_num' => 1,
  ),
  31 => array(
    'nid' => 4099,
    'cid' => '31',
    'pid' => '27',
    'form_key' => 'town_city',
    'name' => 'Town/City',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '18',
    'page_num' => 1,
  ),
  32 => array(
    'nid' => 4099,
    'cid' => '32',
    'pid' => '27',
    'form_key' => 'county',
    'name' => 'County',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '19',
    'page_num' => 1,
  ),
  33 => array(
    'nid' => 4099,
    'cid' => '33',
    'pid' => '27',
    'form_key' => 'postcode',
    'name' => 'Postcode',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'before',
      'private' => 0,
      'width' => '20',
      'disabled' => 0,
      'unique' => 0,
      'maxlength' => '7',
      'webform_conditional_field_value' => 'other',
      'webform_conditional_cid' => '23',
      'webform_conditional_operator' => '=',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '20',
    'page_num' => 1,
  ),
  8 => array(
    'nid' => 4099,
    'cid' => '8',
    'pid' => '4',
    'form_key' => 'dates',
    'name' => 'Dates',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '6',
    'page_num' => 1,
  ),
  9 => array(
    'nid' => 4099,
    'cid' => '9',
    'pid' => '8',
    'form_key' => 'starting',
    'name' => 'Starting',
    'type' => 'date',
    'value' => '',
    'extra' => array(
      'timezone' => 'user',
      'title_display' => 'before',
      'private' => 0,
      'datepicker' => 1,
      'year_textfield' => 0,
      'start_date' => '0 years',
      'end_date' => '+2 years',
      'description' => '',
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '7',
    'page_num' => 1,
  ),
  25 => array(
    'nid' => 4099,
    'cid' => '25',
    'pid' => '8',
    'form_key' => 'number_of_occurrences',
    'name' => 'Number of occurrences',
    'type' => 'select',
    'value' => '1',
    'extra' => array(
      'items' => "1|1\n2|2\n3|3\n4|4\n5|5\n6|6\n7|7\n8|8\n9|9\n10|10",
      'multiple' => 0,
      'title_display' => 'before',
      'private' => 0,
      'aslist' => 1,
      'optrand' => 0,
      'other_option' => NULL,
      'other_text' => 'Other...',
      'description' => '',
      'custom_keys' => FALSE,
      'options_source' => '',
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '8',
    'page_num' => 1,
  ),
  11 => array(
    'nid' => 4099,
    'cid' => '11',
    'pid' => '8',
    'form_key' => 'how_often_does_this_event_occur',
    'name' => 'How often does this event occur?',
    'type' => 'select',
    'value' => '',
    'extra' => array(
      'items' => "655|One off\n656|Daily\n657|Weekly\n658|Forthnightly\n659|Monthly\n660|Other\n",
      'options_source' => 'event_frequency',
      'multiple' => 0,
      'title_display' => 'before',
      'private' => 0,
      'aslist' => 1,
      'optrand' => 0,
      'other_option' => NULL,
      'other_text' => 'Other...',
      'description' => '',
      'custom_keys' => FALSE,
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '9',
    'page_num' => 1,
  ),
  13 => array(
    'nid' => 4099,
    'cid' => '13',
    'pid' => '4',
    'form_key' => 'event_contact_details',
    'name' => 'Event Contact Details ',
    'type' => 'textarea',
    'value' => '',
    'extra' => array(
      'description' => 'Please note that these contact details WILL be published with the event details, in case customers want any more information',
      'title_display' => 0,
      'private' => 0,
      'resizable' => 0,
      'disabled' => 0,
      'conditional_operator' => '=',
      'cols' => '',
      'rows' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '7',
    'page_num' => 1,
  ),
  1 => array(
    'nid' => 4099,
    'cid' => '1',
    'pid' => '0',
    'form_key' => 'your_contact_details',
    'name' => 'Your Contact Details',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'description' => 'Please note that your contact details are for internal use only in case we have a query with the details submitted. These details will not be published on the site.',
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '2',
    'page_num' => 1,
  ),
  2 => array(
    'nid' => 4099,
    'cid' => '2',
    'pid' => '1',
    'form_key' => 'full_name',
    'name' => 'Full Name',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'conditional_operator' => '=',
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '1',
    'page_num' => 1,
  ),
  24 => array(
    'nid' => 4099,
    'cid' => '24',
    'pid' => '1',
    'form_key' => 'phone_number',
    'name' => 'Phone Number',
    'type' => 'textfield',
    'value' => '',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'width' => '',
      'maxlength' => '',
      'field_prefix' => '',
      'field_suffix' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '2',
    'page_num' => 1,
  ),
  3 => array(
    'nid' => 4099,
    'cid' => '3',
    'pid' => '1',
    'form_key' => 'email_address',
    'name' => 'Email Address',
    'type' => 'email',
    'value' => '',
    'extra' => array(
      'title_display' => 'inline',
      'private' => 0,
      'disabled' => 0,
      'unique' => 0,
      'conditional_operator' => '=',
      'width' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '3',
    'page_num' => 1,
  ),
  14 => array(
    'nid' => 4099,
    'cid' => '14',
    'pid' => '0',
    'form_key' => 'admission_details',
    'name' => 'Admission Details',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '3',
    'page_num' => 1,
  ),
  15 => array(
    'nid' => 4099,
    'cid' => '15',
    'pid' => '14',
    'form_key' => 'cost',
    'name' => 'Cost',
    'type' => 'select',
    'value' => '',
    'extra' => array(
      'items' => "616|Free Event\n628|Under £10\n630|£10 or over\n",
      'options_source' => 'event_cost',
      'multiple' => 0,
      'title_display' => 'before',
      'private' => 0,
      'aslist' => 1,
      'optrand' => 0,
      'other_option' => NULL,
      'other_text' => 'Other...',
      'description' => '',
      'custom_keys' => FALSE,
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '3',
    'page_num' => 1,
  ),
  16 => array(
    'nid' => 4099,
    'cid' => '16',
    'pid' => '14',
    'form_key' => 'concessions_etc',
    'name' => 'Concessions etc',
    'type' => 'textarea',
    'value' => '',
    'extra' => array(
      'description' => 'Please add any additional pricing details, including any available concessions',
      'title_display' => 0,
      'private' => 0,
      'resizable' => 0,
      'disabled' => 0,
      'conditional_operator' => '=',
      'cols' => '',
      'rows' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '4',
    'page_num' => 1,
  ),
  17 => array(
    'nid' => 4099,
    'cid' => '17',
    'pid' => '0',
    'form_key' => 'type_of_event',
    'name' => 'Type of event',
    'type' => 'fieldset',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'conditional_operator' => '=',
      'description' => '',
      'conditional_component' => '',
      'conditional_values' => '',
    ),
    'mandatory' => '0',
    'weight' => '4',
    'page_num' => 1,
  ),
  18 => array(
    'nid' => 4099,
    'cid' => '18',
    'pid' => '17',
    'form_key' => 'event_category',
    'name' => 'Event Category',
    'type' => 'select',
    'value' => '',
    'extra' => array(
      'items' => "615|Advice, jobs and training\n629|Art and crafts\n646|Children and families\n653|Cinema/film\n654|Comedy\n619|Community events\n648|Energy efficiency scheme\n649|Food market\n618|Health and well-being\n647|Literature\n636|Local history\n623|Music\n632|Nature and wildlife\n652|Other\n622|Play session\n639|Sports\n642|Theatre and dance\n",
      'options_source' => 'event_category',
      'multiple' => 1,
      'title_display' => 'none',
      'private' => 0,
      'aslist' => 0,
      'optrand' => 0,
      'other_option' => NULL,
      'other_text' => 'Other...',
      'description' => '',
      'custom_keys' => FALSE,
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '4',
    'page_num' => 1,
  ),
  19 => array(
    'nid' => 4099,
    'cid' => '19',
    'pid' => '17',
    'form_key' => 'please_give_details',
    'name' => 'Please give any other details',
    'type' => 'textarea',
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'private' => 0,
      'resizable' => 0,
      'disabled' => 0,
      'webform_conditional_field_value' => "Other\n652",
      'webform_conditional_cid' => '18',
      'webform_conditional_operator' => '=',
      'cols' => '',
      'rows' => '',
      'description' => '',
      'attributes' => array(),
      'conditional_component' => '',
      'conditional_operator' => '=',
      'conditional_values' => '',
    ),
    'mandatory' => '1',
    'weight' => '5',
    'page_num' => 1,
  ),
)

If you have the option, preferably, you should store the items individually - It will be much easier to change parts of the data, and will allow you to abstract the language you're using away from the data storage 如果可以的话,最好单独存储项目-更改部分数据会容易得多,并且可以从数据存储中抽象出使用的语言

If that's not an option, consider storing the data in a different way - Maybe serialize the data before you save it to the DB, and then unserialize it when you retrieve it. 如果不是这种选择,请考虑以其他方式存储数据-也许先序列化数据,然后再将其保存到数据库中,然后在检索时反序列化。 Unserialize keeps the data type and structure. 反序列化保留数据类型和结构。

If you can't do that, then you would probably have to parse the string and build a new array from that, or eval the entire string to a variable and then use the variable. 如果无法做到这一点,则可能必须解析字符串并从中构建一个新数组,或者将整个字符串评估为一个变量,然后使用该变量。

Try this: 尝试这个:

 eval("\$array = " . $stringArrayData);

Then, 然后,

print_r($array);
# try this:
# fileA:array.txt(file contains the strings)
# fileB:

  $ar_source = include 'array.txt';
  var_dump($ar_source );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM