从序列化的post_meta(PHP)

时间:2016-01-25 09:50:36

标签: php arrays wordpress serialization array-filter

我在Wordpress中设置了自定义帖子类型(艺术家)......

我有一个插件来序列化来自艺术家的元信息..我需要通过频道过滤这些信息..:"频道"; s:6:"恍惚"这也是序列化元" show_data"

'a:1:{s:32:"57fba1b113187b95554339a0737e0309";a:7:{s:7:"dj_name";s:7:"DjValue";s:9:"show_name";s:9:"ShowValue";s:7:"channel";s:6:"trance";s:10:"show_image";s:0:"";s:4:"time";s:1:"0";s:8:"time_end";s:1:"0";s:4:"mon1";s:1:"1";}}'

插件取消序列化这样的数据:

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

然后继续遍历数组......以形成日历......

我需要通过频道类型(恍惚或技术)过滤掉...我已经尝试过使用array_filter但是......没有运气:( ..我在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>';

2 个答案:

答案 0 :(得分:0)

阵列过滤器是一个很好的尝试,你是这样做的:

$channel = 'trance';
$show_data = array_filter($show_data, function($data) use ($channel) {
    return $data['channel'] == $channel;
});

答案 1 :(得分:0)

您可以array_filter()使用任何有价值的密钥来执行此操作。

对于例如 Channel => trance,它将返回匹配的数组。

$value = 'trance';
$key = 'channel';

$show_data = array_filter($array, function($itrate) use ($value,$key) {
    return $itrate[$key] == $value;
});

请参阅:Live Demo