如何使用正确的格式序列化和反序列化数组

时间:2015-10-27 07:10:05

标签: php arrays

我的情况如下:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600,700);
@import url(http://fonts.googleapis.com/css?family=Montserrat:400,700);

body {
  font-family: 'Montserrat', sans-serif;
  font-weight: 600;
  color: #434343;
  background: #373a3c

}

.navbar-dark .navbar-brand, .navbar-dark .navbar-brand:focus, .navbar-dark .navbar-brand:hover{
  color: #5cb85c;
  font-size: 20px;
  margin-right: 60px;
}
.card-inverse .card-blockquote, .card-inverse .card-footer, .card-inverse .card-header, .card-inverse .card-title, .navbar-dark .navbar-nav .active>.nav-link, .navbar-dark .navbar-nav .active>.nav-link:focus, .navbar-dark .navbar-nav .active>.nav-link:hover, .navbar-dark .navbar-nav .nav-link.active, .navbar-dark .navbar-nav .nav-link.active:focus, .navbar-dark .navbar-nav .nav-link.active:hover, .navbar-dark .navbar-nav .nav-link.open, .navbar-dark .navbar-nav .nav-link.open:focus, .navbar-dark .navbar-nav .nav-link.open:hover, .navbar-dark .navbar-nav .open>.nav-link, .navbar-dark .navbar-nav .open>.nav-link:focus, .navbar-dark .navbar-nav .open>.nav-link:hover{
    color: #5cb85c;
}

.navbar{
  border-radius: 0px;
  width: 100%;
} 

/************************
  1. Basic Classes
************************/
a {
  color: #259625;
  background: transparent;
  border: none;
  text-decoration: none;
  cursor: pointer;
  -ms-word-wrap: break-word;
  word-wrap: break-word;
  -webkit-transition: 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  -moz-transition: 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  -o-transition: 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transition: 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  -webkit-transition-property: background-color, color;
  -moz-transition-property: background-color, color;
  -o-transition-property: background-color, color;
  transition-property: background-color, color;
}
a:active,
a:hover,
a:focus,
.btn:active:focus,
.btn:focus {
  text-decoration: none;
  outline: none;
  outline: 0px auto -webkit-focus-ring-color;
  outline-offset: 0px;
  color: #52d052;
}
a:focus,
a:active,
a:hover,
button::-moz-focus-inner,
input[type="reset"]::-moz-focus-inner,
input[type="button"]::-moz-focus-inner,
input[type="submit"]::-moz-focus-inner,
select::-moz-focus-inner,
input[type="file"] > input[type="button"]::-moz-focus-inner {
  border: 0;
  outline: 0;
  color: #3c7c3c;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
}
iframe {
  border: none;
}
hr {
  border-width: 2px 0 0;
  margin: 30px 0;
}
img {
  max-width: 100%;
  height: auto;
}
p {
  font-size: 14px;
  line-height: 20px;
  font-weight: normal;
  -ms-word-wrap: break-word;
  word-wrap: break-word;
}
dl dt {
  font-weight: bold;
}
dd {
  margin-left: 1.0786666666666667em;
  font-weight: normal;
}
#wrapper {
  clear: both;
  overflow-x: hidden;
  background: #ffffff;
}


.banner-side{
margin: 30px auto; 
display:block;
}




.no-padding {padding: 0px;}
.no-margin {margin: 0px;}
.padding-left-zero {padding-left: 0;}
.padding-right-zero {padding-right: 0;}




/* =====================
    4.   Right-side
======================== */

/* 4.1 Top Bar */
.top-bar {
  background: #edeff0;
  float: left;
  width: 100%;
}


/* ==============================================
 Main Content
=============================================== */
.main-content {
  background: #edeff0;
  padding: 80px 85px;
  float: left;
  width: 100%;
}
.bg-box-shadow {
  -webkit-box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.06);
  box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.06);
}


/* 4.3 Main Slider */
#slider .carousel-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  padding-left: 30px;
  padding-bottom: 40px;
  right: 0;
  text-align: left;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.25) 40%, rgba(255, 255, 255, 0.02) 60%);
}


#slider .carousel-caption span {
  background: #5B4282;
  padding: 10px 20px;
  font-weight: 400;
}
#slider .carousel-caption h3 {font-size: 24px;}
.owl-pagination {
  margin-top: -60px;
  position: absolute;
  right: 100px;
}


.slider-side-post .post {
  position: relative;
  z-index: 10;
}
.slider-side-post .post-info {
  color: #ffffff;
  bottom: 0;
  padding: 0 30px 20px 30px;
  position: absolute;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.25) 40%, rgba(255, 255, 255, 0.02) 60%);
  width: 100%;
}


/* ==============================================
 4.4  Main Body Post
=============================================== */
.main-post-body {
  margin-left: -13px;
  margin-right: -13px;
  margin-top: 80px;
}
.latest-post .padding-normal {padding: 0 30px 0 0;}
.main-content .single-post > .post-bottom-content {margin-top: 0;margin-right: 0;margin-left: 0;}
.latest-post .post-bottom-content {margin-bottom: 0;}
.post-bottom-content {
  position: relative;
  background: #ffffff;
  margin-bottom: 60px;
  margin-left: 13px;
  margin-right: 13px;
  padding-bottom: 21px;
  min-height: 348px;
}
.post-bottom-content:hover {
  -webkit-box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.15);
  box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.15);
}
.post-bottom-content h3 {
  color: #222222;
  font-size: 16px;
  padding: 25px 30px 0px 30px;
}

.post-bottom-content p{
  color: #222222;
  padding: 10px 30px;
}

.post-bottom-content a {
  color: #259625;
  font-size: 14px;
  padding: 10px 30px;

}


.post-bottom-content a:hover{
  color: #52d052;
    text-decoration: underline;
}

如果我序列化,这意味着它将如下所示:

$permis =array(
 'employee' => array(
      'myprofile' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
        ),
        'view emp' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0'
        )
    )
);

如果我反序列化意味着它将如下所示:

a:1:{s:8:"employee";a:2:{s:9:"myprofile";a:9:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";}s:8:"view emp";a:13:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";s:5:"notes";s:1:"0";s:7:"onboard";s:1:"0";s:8:"offboard";s:1:"0";s:6:"charts";s:1:"0";}}}

我需要的是我的unserialize数组将如下所示:

Array ( [employee] => Array ( [myprofile] => Array ( [default] => 0 [personal] => 0 [job] => 0 [leave] => 0 [permission] => 0 [bonus & commision] => 0 [document] => 0 [emergency contact] => 0 [benifits] => 0 ) [view emp] => Array ( [default] => 0 [personal] => 0 [job] => 0 [leave] => 0 [permission] => 0 [bonus & commision] => 0 [document] => 0 [emergency contact] => 0 [benifits] => 0 [notes] => 0 [onboard] => 0 [offboard] => 0 [charts] => 0 ) ) )

如何以正确的方式做到这一点。

请帮帮我

2 个答案:

答案 0 :(得分:1)

这是你正确的数组。如果要使用结构打印数组

<?php 
echo "<pre>",print_r($array),"</pre>";
?>

阅读手册http://php.net/manual/en/function.serialize.php,因为它将被解释。这是一个简单的在线生成器,您可以测试您的数组是否对sirialize http://php.fnlist.com/php/serialize

有效

修改

 <?php 

    $permis =array(
     'employee' => array(
          'myprofile' => array(
            'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
            ),
            'view emp' => array(
            'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0'
            )
        )
    );

    $serialized = serialize($permis);
    echo "<pre>",print_r($serialized),"</pre>"; //echo serialized array

将输出

  

一个:1:{S:8:&#34;雇员&#34 ;;一个:2:{S:9:&#34;我的资料&#34 ;;一个:9:{S:7:&# 34;默认&#34 ;; S:1:&#34; 0&#34 ;; S:8:&#34;个人&#34 ;; S:1:&#34; 0&#34 ;; S:3: &#34;工作&#34 ;; S:1:&#34; 0&#34 ;; S:5:&#34;离开&#34 ;; S:1:&#34; 0&#34 ;; S: 10:&#34;许可&#34 ;; s:1:&#34; 0&#34 ;; s:17:&#34;奖金&amp;佣金&#34 ;; S:1:&#34; 0&#34 ;; S:8:&#34;文件&#34 ;; S:1:&#34; 0&#34 ;; S:17:&# 34;紧急联系&#34;; s:1:&#34; 0&#34 ;; s:8:&#34; benifits&#34 ;; s:1:&#34; 0&#34 ;;} s: 8:&#34;查看emp&#34 ;; a:13:{s:7:&#34;默认&#34 ;; s:1:&#34; 0&#34 ;; s:8:&#34 ;个人&#34 ;; S:1:&#34; 0&#34 ;; S:3:&#34;工作&#34 ;; S:1:&#34; 0&#34 ;; S:5:& #34;离开&#34 ;; S:1:&#34; 0&#34 ;; S:10:&#34;权限&#34 ;; S:1:&#34; 0&#34 ;; S:17 :&#34;奖金&amp;佣金&#34 ;; S:1:&#34; 0&#34 ;; S:8:&#34;文件&#34 ;; S:1:&#34; 0&#34 ;; S:17:&# 34;紧急联系&#34;; s:1:&#34; 0&#34 ;; s:8:&#34; benifits&#34 ;; s:1:&#34; 0&#34 ;; s:5 :&#34;笔记&#34 ;; S:1:&#34; 0&#34 ;; S:7:&#34;车载&#34 ;; S:1:&#34; 0&#34 ;;小号:8:&#34;非车载&#34 ;; S:1:&#34; 0&#34 ;; S:6:&#34;图表&#34 ;; S:1:&#34; 0&#34; ;}}}

 $unserialized = unserialize($serialized);
    echo "<pre>",print_r($unserialized),"</pre>"; //echo unserialized array
    ?>

将输出

  

阵列(    &#39;雇员&#39; =&GT;阵列(         &#39;我的资料&#39; =&GT;阵列(           &#39;默认&#39; =&GT; &#39; 0&#39;,&#39;个人&#39; =&GT; &#39; 0&#39;,&#39;工作&#39; =&GT; &#39; 0&#39;,&#39;离开&#39; =&GT; &#39; 0&#39;,&#39;许可&#39; =&GT; &#39; 0&#39;,&#39;奖金&amp;佣金&#39; =&GT; &#39; 0&#39;,&#39;文件&#39; =&GT; &#39; 0&#39;,&#39;紧急联系&#39; =&GT; &#39; 0&#39;,&#39; benifits&#39; =&GT; &#39; 0&#39;           )           &#39;查看emp&#39; =&GT;阵列(           &#39;默认&#39; =&GT; &#39; 0&#39;,&#39;个人&#39; =&GT; &#39; 0&#39;,&#39;工作&#39; =&GT; &#39; 0&#39;,&#39;离开&#39; =&GT; &#39; 0&#39;,&#39;许可&#39; =&GT; &#39; 0&#39;,&#39;奖金&amp;佣金&#39; =&GT; &#39; 0&#39;,&#39;文件&#39; =&GT; &#39; 0&#39;,&#39;紧急联系&#39; =&GT; &#39; 0&#39;,&#39; benifits&#39; =&GT; &#39; 0&#39;,&#39;笔记&#39; =&GT; &#39; 0&#39;,&#39;板载&#39; =&GT; &#39; 0&#39;,&#39; offboard&#39; =&GT; &#39; 0&#39;,&#39;排行榜&#39; =&GT; &#39; 0&#39;           )       )   );

我检查了两次。数组是正确的,反序列化也是正确的。 http://php.fnlist.com/php/serialize 如果你想回显一个数组的键,可以使用foreach或类似的东西

<?php
echo $unserilized['employee']['myprofile']['personal'];

&GT?; 将输出

  

0,因为它是键的值

答案 1 :(得分:1)

数组已正确反序列化,您的问题是您尝试以不正确的方式访问元素。

当您撰写$val2['default']时,它会查找&#39;默认&#39;在顶级关键,您只有员工&#39;。

要访问&#39;默认&#39;对于任何一个员工,您都需要逐级对其进行索引,如下所示:

$val2['employee']['myprofile']['default']