如何在PHP中解析数组时避免错误?

时间:2017-05-03 14:46:01

标签: php parsing foreach

我正在尝试将用户数据从Drupal数据库迁移到Wordpress。我正在尝试解析Drupal中的数据列并重新组织它,以便我可以将其导入到Wordpress数据库中我需要的位置。 Drupal中的数据列是一个序列化字符串:

OpenIdConnectAuthenticationNotifications

我一直收到这个错误:

a:12:{s:23:"profile_membership_type";s:4:"Full";s:21:"ms_membership_add_new";s:0:"";s:18:"ms_membership_mpid";s:0:"";s:25:"ms_membership_amount_paid";s:0:"";s:32:"ms_membership_transaction_number";s:0:"";s:30:"ms_membership_current_payments";i:1;s:26:"ms_membership_max_payments";s:0:"";s:24:"ms_membership_start_date";a:3:{s:4:"year";s:4:"2014";s:5:"month";s:1:"2";s:3:"day";s:2:"13";}s:27:"ms_membership_should_expire";b:0;s:24:"ms_membership_expiration";a:3:{s:4:"year";s:4:"2014";s:5:"month";s:1:"2";s:3:"day";s:2:"13";}s:20:"ms_membership_status";i:3;s:7:"contact";i:1;}

到目前为止,这是我的代码:

Warning: Invalid argument supplied for foreach()

4 个答案:

答案 0 :(得分:2)

在迭代前检查if条件

if(is_array($original_array) && !empty($original_array)){
    foreach ($original_array as $key => $value) {
       echo $value['profile_membership_type'];
    }
}

答案 1 :(得分:0)

只需投放到array

foreach ( (array) $original_array as $key => $value) {
    echo $value['profile_membership_type'];
}

答案 2 :(得分:0)

您可以轻松检查您正在迭代的变量是否为数组;

if(isset($original_array) && is_array($original_array)) {
    foreach ( $original_array as $key => $value) {
       echo $value['profile_membership_type'];
    }
}

编辑包括检查以确保变量存在。

答案 3 :(得分:0)

你有一个php序列化的字符串。

我认为你问题中的前导f是一个拼写错误,因为其余的是一个有效的序列化数组:

$array = unserialize('a:12:{s:23:"profile_membership_type";s:4:"Full";s:21:"ms_membership_add_new";s:0:"";s:18:"ms_membership_mpid";s:0:"";s:25:"ms_membership_amount_paid";s:0:"";s:32:"ms_membership_transaction_number";s:0:"";s:30:"ms_membership_current_payments";i:1;s:26:"ms_membership_max_payments";s:0:"";s:24:"ms_membership_start_date";a:3:{s:4:"year";s:4:"2014";s:5:"month";s:1:"2";s:3:"day";s:2:"13";}s:27:"ms_membership_should_expire";b:0;s:24:"ms_membership_expiration";a:3:{s:4:"year";s:4:"2014";s:5:"month";s:1:"2";s:3:"day";s:2:"13";}s:20:"ms_membership_status";i:3;s:7:"contact";i:1;}');

var_dump($array);
/*
array (size=12)
  'profile_membership_type' => string 'Full' (length=4)
  'ms_membership_add_new' => string '' (length=0)
  'ms_membership_mpid' => string '' (length=0)
  'ms_membership_amount_paid' => string '' (length=0)
  'ms_membership_transaction_number' => string '' (length=0)
  'ms_membership_current_payments' => int 1
  'ms_membership_max_payments' => string '' (length=0)
  'ms_membership_start_date' => 
    array (size=3)
      'year' => string '2014' (length=4)
      'month' => string '2' (length=1)
      'day' => string '13' (length=2)
  'ms_membership_should_expire' => boolean false
  'ms_membership_expiration' => 
    array (size=3)
      'year' => string '2014' (length=4)
      'month' => string '2' (length=1)
      'day' => string '13' (length=2)
  'ms_membership_status' => int 3
  'contact' => int 1
*/

请注意,只有一个profile_membership_type元素,因此无需循环:

echo $array['profile_membership_type']; // Full