Thomas_Hoadley
Thomas_Hoadley

Reputation: 178

Pulling data from arrays PHP

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.

Upvotes: 0

Views: 106

Answers (1)

localroot
localroot

Reputation: 556

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;

Upvotes: 2

Related Questions