Filter some values from an array in a serialized p

2019-03-01 20:36发布

问题:

I have setup in Wordpress a custom post type ( Artists )...

I have a plugin that serializez the meta information from the Artists .. and i need to filter this information by channel ..: "channel";s:6:"trance" which is also in the serialized meta "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";}}'

The plugin unserializez the data like this :

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

Then goes on to loop through the arrays .... to form a calendar....

i need to filter out by channel type ( trance or techno ) ... i have tried with array_filter but .. had no luck :( .. im pretty green in php

i would have needed something like this :

[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] => 
        )
    )

then filter out only the trance using the shortcode arguments... result should be...

[78] => Array (
    [57fba1b113187b95554339a0737e0309] => Array (
        [dj_name] => DjValue
        [show_name] => ShowValue
        [channel] => trance
        [show_image] => 
        )
    )

Thanks in advanceee

the full code is bellow :

<? 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>';

回答1:

Array filter was a good try, did you do it like this :

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


回答2:

You can do it by array_filter() using any key has value.

For e.g Channel => trance then it will return matched arrays.

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

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

See : Live Demo