Create a array of array keys within a serialised array.

I am querying a WP DB to get the following:

userid
collection_name
collection_data (serialized array)

Read More

I run the following foreach loop:

foreach($result as $row){
$row->collection_name;
$cleandata = unserialize($row->collection_data);

The print_r of $cleandata gives me the following arrays:

Array
(
[7] => Array
    (
        [ExerciseID] => 7
        [Description] => Description?!
        [Sets] => 12
        [Reps] => 12
        [Load] => 12
        [Rest] => 12
        [Tempo] => 12
    )

)

From this I need to create an array of ExerciseID’s so I can put them into the following WP_query.

$the_query = new WP_Query(array('post__in' => $ids, 'post_type' => 'exercise','posts_per_page' =>'4000')); ?>

I have tried running another foreach within the foreach $result as $row however I am getting pretty stuck with it.

Many thanks in advance.

Related posts

Leave a Reply

2 comments

  1. Try this-

    $ids = array();
    foreach($result as $row)
    {
      $row->collection_name;
      $cleandata = unserialize($row->collection_data);
    
      foreach($cleandata as $data)
      {
          $ids[] = $data['ExerciseID'];
      }
    }