0

The problem.

I have an array of Wordpress advanced custom fields data that I have pulled down from the database using SQL into a PHP array.

$wp_postmeta = array(
  array('meta_id' => '3784','post_id' => '180','meta_key' => 'press_0_title','meta_value' => 'The first title'), // Want 
  array('meta_id' => '3785','post_id' => '180','meta_key' => '_press_0_title','meta_value' => 'field_52d67eb94ac55'), // Dont want 
  array('meta_id' => '3786','post_id' => '180','meta_key' => 'press_0_date','meta_value' => 'November-17'), // Want
  array('meta_id' => '3787','post_id' => '180','meta_key' => '_press_0_date','meta_value' => 'field_52d67f094ac58'), // Dont want
  array('meta_id' => '3837','post_id' => '180','meta_key' => 'press_1_title','meta_value' => 'The second title'), // Want
  array('meta_id' => '3830','post_id' => '180','meta_key' => '_press_1_title','meta_value' => 'field_52d67eb94ac55'), // Dont want
  array('meta_id' => '3839','post_id' => '180','meta_key' => 'press_1_date','meta_value' => 'October-17'), // Want 
  array('meta_id' => '3832','post_id' => '180','meta_key' => '_press_1_date','meta_value' => 'field_52d67f094ac58') // Dont want
);

They include the full of title and date values, but are separated into individual arrays which I need to pull down into their corresponding groups e.g. the date and title from the press_0_, press_1_ etc, as you can see in the solution below, where it returns the data formatted as I need it.

My solution

// This is the array I need to pull the data from.

$wp_postmeta = array(
  array('meta_id' => '3784','post_id' => '180','meta_key' => 'press_0_title','meta_value' => 'The first title'), // Want 
  array('meta_id' => '3785','post_id' => '180','meta_key' => '_press_0_title','meta_value' => 'field_52d67eb94ac55'), // Dont want 
  array('meta_id' => '3786','post_id' => '180','meta_key' => 'press_0_date','meta_value' => 'November-17'), // Want
  array('meta_id' => '3787','post_id' => '180','meta_key' => '_press_0_date','meta_value' => 'field_52d67f094ac58'), // Dont want
  array('meta_id' => '3837','post_id' => '180','meta_key' => 'press_1_title','meta_value' => 'The second title'), // Want
  array('meta_id' => '3830','post_id' => '180','meta_key' => '_press_1_title','meta_value' => 'field_52d67eb94ac55'), // Dont want
  array('meta_id' => '3839','post_id' => '180','meta_key' => 'press_1_date','meta_value' => 'October-17'), // Want 
  array('meta_id' => '3832','post_id' => '180','meta_key' => '_press_1_date','meta_value' => 'field_52d67f094ac58') // Dont want
);

// These are the loops I have run to pull the data.

$counter = 0;
$keys = [];

foreach ($wp_postmeta as $meta) {
  $titleKey = 'press_'.$counter.'_title';
  $dateKey = 'press_'.$counter.'_date';

  $key = [];

  $key['title-key'] = $titleKey;
  $key['date-key'] = $dateKey;

  array_push($keys, $key);

  $counter++;
}

foreach ($keys as $key) {
  $titleKey = $key['title-key'];
  $title;
  $dateKey = $key['date-key'];
  $date;

  foreach ($wp_postmeta as $meta) {
    if ($meta['meta_key'] == $titleKey) {
      $title = $meta['meta_value'];
    }

    if ($meta['meta_key'] == $dateKey) {
      $date = $meta['meta_value'];
    }
  }

  // This is the data I want from the array.

  echo 'title - ' . $title . '<br>';
  echo 'date - ' . $date  . '<br>';
}

// This returns

title - The first title
date - November-17

title - The second title
date - October-17

My Question

Are there any improvements I could make to refactor this to make it cleaner? It would greatly improve my PHP knowledge if possible.

7
  • 2
    pos off-topic here and better for codereview SE Commented Oct 17, 2018 at 11:22
  • note to flag reviewer - should be on code review - but option "should be on another SE" doesn't include it - tried me best to opt for best flag Commented Oct 17, 2018 at 11:27
  • You can start by using array_filter with function checking if meta_key starts with '_' Commented Oct 17, 2018 at 11:31
  • @ThisGuyHasTwoThumbs There's a very, very good reason it's not listed there. Commented Oct 17, 2018 at 11:33
  • @Mast which is ... ? Commented Oct 17, 2018 at 11:36

1 Answer 1

2

You can use array_column to get a single column in the input array with an Optionall index_key.

Try this way.

    $wp_postmeta_info = array_column($wp_postmeta, 'meta_value', 'meta_key');
    for($i = 0; $i < count($wp_postmeta_info); $i++ ){
       if(!isset($wp_postmeta_info['press_'.$i.'_title'])){
            continue;
        }
        $data[] = ['title' => $wp_postmeta_info['press_'.$i.'_title'], 'date' => $wp_postmeta_info['press_'.$i.'_date']];
    }
    return $data;
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for. Thank you for this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.