【问题标题】:Unserialize array and output data from within it反序列化数组并从中输出数据
【发布时间】:2014-08-14 17:57:29
【问题描述】:

我正在从数据库中收到以下信息。

 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']); 之类的东西,但这并没有给我带来任何快乐。

提前非常感谢。

【问题讨论】:

    标签: php arrays wordpress serialization


    【解决方案1】:

    我可以通过以下方式访问数组中的序列化数据:

    $result[0]->collection_data
    

    【讨论】:

      【解决方案2】:

      你可以提取数据

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

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

      【讨论】:

        猜你喜欢
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多