从中反序列化数组和输出数据

时间:2014-08-14 17:57:30

标签: php arrays wordpress serialization

我从数据库中收到以下内容。

 Array
  (
 [0] => stdClass Object
    (
        [id] => 1
        [userid] => 2
        [collection_name] => Programm Title
        [collection_data] => a:2:{i:2;a:7:{s:10:"ExerciseID";s:1:"2";s:11:"Description";s:11:"Description";s:4:"Sets";s:3:"123";s:4:"Reps";s:3:"123";s:4:"Load";s:3:"123";s:4:"Rest";s:3:"123";s:5:"Tempo";s:3:"123";}i:3;a:7:{s:10:"ExerciseID";s:1:"3";s:11:"Description";s:0:"";s:4:"Sets";s:0:"";s:4:"Reps";s:0:"";s:4:"Load";s:0:"";s:4:"Rest";s:0:"";s:5:"Tempo";s:0:"";}}
    )`

)

我想反序列化collection_data,它会给我一个这样的数组:

Array
(
[434] => Array
    (
        [ExerciseID] => 434
        [Description] => 
        [Sets] => 
        [Reps] => 
        [Load] => 
        [Rest] => 
        [Tempo] => 
    )

)

然后,我希望运行以下命令来查询数据库中的练习ID。

  $ids = array();

  if(empty($newdata))
 {echo"<p>There is something wrong with that link, please speak to your coach!</p>";}
 else
 {
foreach($newdata as $el)
 {
  $ids[] = $el['ExerciseID'];
 }
 }
// START OF THE QUERY USING THE EXERCISE ID'S FOR DISPLAYING IN THE COLLECTION
$the_query = new WP_Query(array('post__in' => $ids, 'post_type' =>   'exercise','posts_per_page' =>'40')); ?>

 <?php if ( $the_query->have_posts() && $ids ) : ?>

 <!-- the loop to get the psots that are in the array / collection -->
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

  <!--   <?php the_post_thumbnail('collection_thumb'); ?> -->
 <div class="well">

 <?php $exid = get_the_ID(); ?>
 <?php 
  $description = $newdata[$exid]['Description']; 
  $sets = $newdata[$exid]['Sets'];
  $reps = $newdata[$exid]['Reps'];
  $rest = $newdata[$exid]['Rest'];
  $load = $newdata[$exid]['Load'];
  $tempo = $newdata[$exid]['Tempo'];
 ?>
        <h3><?php the_title(); ?></h3>
 <?php

                $exerciseID = get_the_ID();
                $blogUrl = get_site_url();
                global $wpdb;
                $query = "
                SELECT * 
                FROM imagemap
                WHERE exercise = '".$exerciseID."'";

                $result = $wpdb->get_results($query); 

                foreach($result as $row)
                 {
                  echo '<div class="imageGridImages">';
                  // echo "<img src=".$blogUrl. "/image/thumb/".$row->id. ".jpg />";
                  echo '<div class="image display" style="background-image: url('  .$blogUrl.'/image/thumb/' .$row->id. '.jpg);"></div>';

                  echo '</div>';
                 }

                  // echo $row->id." ".$row->exercise." ".$row->source." <br>";

                // echo $query;
                // var_dump($result)

                ?>

这应该工作的方式是从数据库接收保存的数据反序列化集合,然后显示集合。我在整个网站上都做得很好,但这似乎令我感到困惑。

我一直试图使用像$newdata = maybe_unserialize($result['collection_data']);这样的东西但是这并没有给我任何快乐。

非常感谢提前。

2 个答案:

答案 0 :(得分:0)

我可以使用以下方法访问数组中的序列化数据:

$result[0]->collection_data

答案 1 :(得分:0)

您可以提取数据

http://php.net/manual/en/function.unserialize.php

$unserialize =  unserialize ($result[0]->collection_data); 
var_dump($unserialize);