从数组中获取值



获取var_dump的值:

mymeta_url_group => 
 0 => string(39) "a:1:{s:10:"mymeta_url";s:8:"You rock";}" 
 1 => string(40) "a:1:{s:10:"mymeta_url";s:9:"Yeah Sure";}" 

我用过:

$urls= get_post_meta( get_the_ID(), 'mymeta_url_group', false );
 foreach ( $urls as $url) 
 { 
         echo $url["mymeta_url"];
 // You Rock
 //Yeah Sure
 }

现在,由于我在后端添加了重复器和排序器选项,新的var dump显示:

mymeta_url_group => 
 0 => string(102) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}" 
 1 => string(100) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won't you";}}"

现在我怎么能得到值"你摇滚"不,也许"是的,当然"你不会"扩展我以前的解决方案。

PS如果我做var_dump($url["mymeta_url"]);输出为

Arrayarray(2) { ["cmb-field-0"]=> string(8) "You rock" ["cmb-field-1"]=> string(11) "Nope Maybe " } Arrayarray(2) { ["cmb-field-0"]=> string(9) "Yeah Sure" ["cmb-field-1"]=> string(9) "Won't you" }

我不知道get_post_meta()应该做什么,但也许这会给你一些想法:

<?php
/* Setting up data */
$data = array (
  'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}',
  'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won't you";}}'
);
$urls = array();
foreach ($data as $s) {
  $urls[] = unserialize ($s);
}
/* Retrieving data */
foreach ($urls as $url) {
  foreach ($url['mymeta_url'] as $u) {
   echo "$u, ";
  }
  echo "n";
}

我是这么想的:

foreach($array as $array2) { //If it doesn't work change $array to $array['mymeta_url_group']
    $array2 = unserialize($array2);
    foreach($array2['mymeta_url'] as $line) {
        echo $line; //$line is the sentance you're looking for
    }
}

我不确定是否会工作,因为我不得不从var_dump()输出重建数组。

相关内容

  • 没有找到相关文章

最新更新