如何从数据库中的此序列化数组PHP获取价格数据

时间:2019-06-29 10:37:53

标签: php mysql database wordpress shortcode

嗨,我正在制作WordPress出租别墅网站,我尝试按季节制作价格表。数据库中的价格已序列化,我想用短码分别绘制价格。有可以帮助的人吗?该如何生成简码?

这些是数据库中别墅的价值

Option

我使用此简码; (但这不起作用)

('meta_id' => '3307','post_id' => '426','meta_key' => 'mphb_season_prices','meta_value' => 'a:12:{i:0;a:2:{s:6:"season";s:3:"252";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:1;a:2:{s:6:"season";s:3:"253";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:2;a:2:{s:6:"season";s:3:"254";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:3;a:2:{s:6:"season";s:3:"255";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:493;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:4;a:2:{s:6:"season";s:3:"256";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:493;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:5;a:2:{s:6:"season";s:3:"257";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:708;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:6;a:2:{s:6:"season";s:3:"258";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:893;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:7;a:2:{s:6:"season";s:3:"259";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:893;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:8;a:2:{s:6:"season";s:3:"260";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:708;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:9;a:2:{s:6:"season";s:3:"261";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:565;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:10;a:2:{s:6:"season";s:3:"262";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:465;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:11;a:2:{s:6:"season";s:3:"263";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}}')

-建议-

建议1)

function fiyatlar_shortcode_function() {
$var    = get_post_meta( get_the_ID(), 'mphb_season_prices', true);
$var    = unserialize($var);
$season = 252; // 252 january, 253 february, 254 march...

   $foo = array_column($var, 'price', 'season');

}          

print_r($foo[$season]['prices'][0]);

add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

建议2)

function fiyatlar_shortcode_function() {
$var    = get_post_meta( get_the_ID(), 'mphb_season_prices', true);
$var    = unserialize($var);
$season = 252; // 252 january, 253 february, 254 march...
    $foo = array_column($var, 'price', 'season');
return ($foo[$season]['price']['prices']); 
}

add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

//Output: Error Message;
Warning: array_column() expects parameter 1 to be array, boolean given in /home/

1 个答案:

答案 0 :(得分:0)

尝试以下操作,让我知道。

function fiyatlar_shortcode_function() {
    $var    = get_post_meta( get_the_ID(), 'mphb_season_prices', true);
    $var    = unserialize($var);
    $season = 252; // 252 january, 253 february, 254 march...

       foreach ($var as $key=> $value){
                $season_array[] = $var[$key]['season']; 
                $prices_array[]=  $var[$key]['price']['prices'][0];
                $array_combo  = array_combine($season_array,$prices_array);

            }          

        return $array_combo[$season];
        }

        add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

提琴

http://phpfiddle.org/main/code/k9gq-4abf