【问题标题】:wordpress data serialize in corresponding orderwordpress 数据按相应顺序序列化
【发布时间】:2014-09-10 14:40:47
【问题描述】:

在 wordpress 中,我将数据存储为序列化方法,对于标题,第一行名为 title[1],第二行为 title[2],第三行为 title[3],依此类推。此方法对所有字段(姓名、电话)都相同。现在将数据存储为序列化后,我想获取数据。所以要获取我的脚本是这样的

$results = $wpdb->get_results("SELECT * FROM `table` where `id` = 4");

现在当我在做 print_r($results);我得到这样的结果

Array
(
    [0] => stdClass Object
        (
            [id] => 4
            [title] => a:3:{i:1;s:8:"title1";i:2;s:8:"title2";i:0;s:0:"title3";}
            [name] => a:3:{i:1;s:8:"name1";i:2;s:8:"name2";i:0;s:0:"name3";}
            [phone] => a:3:{i:1;s:8:"123";i:2;s:8:"324";i:0;s:0:"648";}

        )

)

要像这样使用我使用过的 unserilize 和 foreach 的数据

foreach($results as $result) {
    $titles = unserialize($result->title);
    $names = unserialize($result->name);
    $phones = unserialize($result->phone);

    foreach($titles as $title) {
        echo $title;
    }
}

但这没有意义,因为我想要同一行的所有属性,如(id、title、name、phone),这意味着对于第一行,它应该显示第一个元素的所有值。所以数据应该像这样显示

Id  Title    Name    Phone
4   title1   name1   123
4   title2   name2   324
4   title3   name3   648

那么有人可以告诉我该怎么做吗?

【问题讨论】:

    标签: php arrays wordpress serialization


    【解决方案1】:

    这是假设每个数组中有相同数量的项目。该 ID 也不可用。

    <table>
        <thead>
            <tr>
                <th>ID</td>
                <th>Title</th>
                <th>Name</th>
                <th>Phone</th>
            </tr>
        </thead>
        <tbody>
    <?php   
    foreach($results as $result) {
        $titles = unserialize($result->title);
        $names = unserialize($result->name);
        $phones = unserialize($result->phone);
    
        $i=0;
        foreach($titles as $title) {
            echo "<tr>\n";
            echo "  <td>4</td>\n"; // This isn't in your serialized data.  I'll let you work out where it comes from.
            echo '  <td>'. $titles[$i] .'</td>'. "\n";
            echo '  <td>'. $names[$i] .'</td>'. "\n";
            echo '  <td>'. $phones[$i] .'</td>'. "\n";
            echo "</tr>\n";
    
            $i++;
        }
    }
    ?>
        </tbody>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多