在序列化post_meta(PHP)中从数组中筛选一些值
我在Wordpress中设置了一个自定义帖子类型(艺术家) 我有一个插件,可以序列化艺术家的元信息。。我需要按频道过滤这些信息。:“频道”;s:6:“恍惚”也在序列化的元“show_data”中 插件取消序列化数据,如下所示:在序列化post_meta(PHP)中从数组中筛选一些值,php,arrays,wordpress,serialization,array-filter,Php,Arrays,Wordpress,Serialization,Array Filter,我在Wordpress中设置了一个自定义帖子类型(艺术家) 我有一个插件,可以序列化艺术家的元信息。。我需要按频道过滤这些信息。:“频道”;s:6:“恍惚”也在序列化的元“show_data”中 插件取消序列化数据,如下所示: foreach ( $djs as $dj ) { $temp = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) ); if ( $temp )
foreach ( $djs as $dj ) {
$temp = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
if ( $temp )
$show_data[ ] = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
} //$djs as $dj
$value = 'trance';
$key = 'channel';
$show_data = array_filter($array, function($itrate) use ($value,$key) {
return $itrate[$key] == $value;
});
然后继续循环遍历数组。。。。要组成日历
我需要按频道类型(trance或techno)过滤掉。。。我已尝试使用数组_过滤器,但。。运气不好:(…我对php非常了解
我需要这样的东西:
[78] => Array (
[57fba1b113187b95554339a0737e0309] => Array (
[dj_name] => DjValue
[show_name] => ShowValue
[channel] => trance
[show_image] =>
)
)
[79] => Array (
[55ebc09f567af40917f650f430339772] => Array (
[dj_name] => Emilian
[show_name] => Emilians Trance
[channel] => techno
[show_image] =>
)
)
然后使用短码参数只过滤出恍惚状态…结果应该是
[78] => Array (
[57fba1b113187b95554339a0737e0309] => Array (
[dj_name] => DjValue
[show_name] => ShowValue
[channel] => trance
[show_image] =>
)
)
提前谢谢你
完整代码如下所示:
<? date_default_timezone_set( 'America/New_York' ); $genre = wpautop( wp_kses_post( $pull_calendar_of_shows_atts[ 'genre' ] ) ); $calendarname = wpautop( wp_kses_post( $pull_calendar_of_shows_atts[ 'calendarname' ] ) );
$args = array(
'numberposts' => -1,
'post_type' => 'artist',
'post_status' => 'publish',
'orderby' => 'menu_order',
'suppress_filters' => false ); $djs = get_posts( $args ); foreach ( $djs as $dj ) {
$temp = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
if ( $temp )
$show_data[ ] = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
} //$djs as $dj
$schedule = array("mon" => array( 'day' => 'Monday' ),
"tue" => array( 'day' => 'Tuesday' ),
"wed" => array( 'day' => 'Wednesday'),
"thu" => array( 'day' => 'Thursday' ),
"fri" => array( 'day' => 'Friday' ),
"sat" => array( 'day' => 'Saturday' ),
"sun" => array( 'day' => 'Sunday' ) );
foreach ( $show_data as $row ) {
foreach ( $row as $id => $data ) {
foreach ( $data as $k => $v ) {
if ( preg_match( '/(mon|tue|wed|thu|fri|sat|sun)(\d)/', $k, $m ) ) {
$schedule[ $m[ 1 ] ][ $m[ 2 ] ][ $data[ 'time' ] ] = array(
'artist' => $data[ 'dj_name' ],
'show' => $data[ 'show_name' ],
'id' => $id,
'channel' => $data[ 'channel'],
'image' => $data[ 'show_image' ]
);
} //preg_match( '/(mon|tue|wed|thu|fri|sat|sun)(\d)/', $k, $m )
} //$data as $k => $v
} //$row as $id => $data } //$show_data as $row
echo '<pre>'. print_r ($perm, true) . '</pre>';
$begin = strtotime( 'today' ); $end = strtotime( '- 1 day', strtotime( '+ 1 month' ) ); $iter = $begin; $olddate = ''; $calendar = array( ); while ( $iter < $end ) {
$time = $iter;
$date = date( 'D n/j', $time );
$week = floor( date( 'd', $time ) / 7 ) + 1 * ( floor( date( 'd', $time ) / 7 ) < date( 'd', $time ) / 7 );
$day = strtolower( date( 'D', $time ) );
foreach ( $schedule as $d => $dd ) {
if ( isset( $dd[ $week ] ) && $d == $day ) {
foreach ( $dd[ $week ] as $k => $v ) {
$calendar[ $date ][ $k ] = array(
'artist' => $v[ 'artist' ],
'show' => $v[ 'show' ],
'channel' => $v[ 'channel'],
'image' => $v[ 'image' ]
);
} //$dd[ $week ] as $k => $v
} //isset( $dd[ $week ] ) && $d == $day
} //$schedule as $d => $dd
$iter = strtotime( '+ 1 day', $iter ); } //$iter < $end $ssd = '<div id="Calendar ' . $calendarname . '"><table class="calendar"> '; foreach ( $calendar as $date => $data ) {
ksort( $data );
print_r ( $data );
$ssd .= '<tr class="newday"> ';
$ssd .= '<td rowspan="' . count( $data ) . '">';
$ssd .= preg_replace( '/(\w+) (.*)/', '<span class="dayofweek">$1</span><br /><span class="date">$2</span>', $date );
$ssd .= '</td> ';
$tr = "";
foreach ( $data as $k => $v ) {
$perma = get_page_by_title( 'Chicane' );
echo $perma->ID;
$t = strtotime( "March 16, 2013 $k:00" );
$d = date( 'g:00a', $t );
if ( $v[ 'image' ] ) {
$imgshow = ' <img class="show_calimg" src=" ' . resizeimagenoecho( $v[ "image" ], 24, auto ) . ' "/>';
} //$v[ 'image' ]
$ssd .= "$tr
<td class='time'>" . $d . "</td>
<td>" . $v[ 'artist' ] . "</td>
<td>" . $v[ 'show' ] . "</td>
<td>" . $imgshow . "</td></tr>";
$tr = "<tr>";
} //$data as $k => $v } //$data as $k => $v //$calendar as $date => $data $ssd .= '</table></div>';
阵列过滤器是一个很好的尝试,您是这样做的:
foreach ( $djs as $dj ) {
$temp = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
if ( $temp )
$show_data[ ] = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
} //$djs as $dj
$value = 'trance';
$key = 'channel';
$show_data = array_filter($array, function($itrate) use ($value,$key) {
return $itrate[$key] == $value;
});
数组过滤器是一个很好的尝试,您是这样做的:
foreach ( $djs as $dj ) {
$temp = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
if ( $temp )
$show_data[ ] = maybe_unserialize( get_post_meta( $dj->ID, 'show_data', true ) );
} //$djs as $dj
$value = 'trance';
$key = 'channel';
$show_data = array_filter($array, function($itrate) use ($value,$key) {
return $itrate[$key] == $value;
});
您可以通过array\u filter()
使用任何具有值的键来实现
例如频道=>trance
则返回匹配的数组
请参阅:您可以通过array\u filter()
使用任何具有值的键来执行此操作
例如频道=>trance
则返回匹配的数组
请参阅:此打印输出:数组()$channel='trance';$show\u data\u filtered=Array\u filter($show\u data,function($fs)use($channel){return$fs['channel']==$channel;});打印($show\u data\u filtered);用$data['channel']替换$data['channel],就像我在编辑器中做的那样,是的,我就是这样使用的(我想)…数组过滤器…我想我遗漏了一些东西HMM也用“trance”替换“transe”这张打印出来:数组()$channel='trance';$show_data_filtered=array_filter($show_data,function($fs)use($channel){return$fs['channel']=$channel;});打印($show_data_data_filtered);$data['channel']替换$data['channel']就像我在我的编辑中所做的一样,是的,这就是我如何使用(我认为)…数组过滤器…我认为我遗漏了一些东西HMM并且用“恍惚”替换“transe”了“show_data”数组输出,比如$array=array([78]=>array([57fba1b113187b95554339a0737e0309]=>array([dj_name]=>DjValue[show_name]=>ShowValue[channel]=>trance[show_image]=>)[79]=>Array([55ebc09f567af40917f650f430339772]=>Array([dj_name]=>Emilian[show_name]=>Emilian trance[channel]=>trance[show_image]=>)…我犯了一个错误,非序列化后的第一个数组…看起来不同:-“show_data”数组输出如下$array=array([78]=>array([57fba1b113187b95554339a0737e0309]=>array([dj_name]=>DjValue[show_name]=>ShowValue[channel]=>trance[show_image]=>))[79]=>Array([55ebc09f567af40917f650f430339772]=>Array([dj_name]=>Emilian[show_name]=>Emilian Trance[channel]=>Trance[show_image]=>)…我犯了一个错误,非序列化后的第一个数组…看起来不一样…:?