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)

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.

Solutions

Try this-

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

  foreach($cleandata as $data)
  {
      $ids[] = $data['ExerciseID'];
  }
}

If you don't have PHP >= 5.5 and so can't use array_column(), then use

$result = array_map(
    function($value) { 
        return $value['ExerciseID'];
    },
    $cleanData
);
Tags: PHP / Arrays / Wordpress

Similar questions

Name keys within array from WordPress meta fields
i am trying to code a calendar script within wordpress and custom post types. works fine so far. I use several custom fields (ACF) to call my desired parameters (event title, start_date, end_date, etc.) I call the results by get_posts(). but I have a problem with naming my keys within the array. after the foreach operation I can get the desired val...
Using WP Query to search within ALL keys in meta query
What is the best way to create a meta query that searches through ALL meta fields (keys)? What I'm trying to do is something such as: Does that make sense? What is the proper way of doing it? Thanks
The writing ( with foreach) of the keys of an array of objects to a new array produces fatal error
I have the following problem, i've queried the attached media to a post with: as a result i get an array of objects. But somehow i am unable to get the object keys transfered to a new separate array. The array of objects looks like that for example (i've left out the contained key value pairs): When i try to loop through the array of objects with f...
Compare two different array keys, then do something with the values that dont exist in the 1st array
Arrays seem to be one of those subjects that just doesn't want to click for me. Seemed easier in SQL... I do hope you can help.. I have two arrays that I wish to compare the keys layouts are completely different, I wish to compare *$first_array->place->name* with *$second_array->post_name* then if a value from the first array doesn't exist in the s...
How to compare an array of list of keys with an array of "cat_name" of wordpress post with array_intersect
i have created this code : but it doesn't work. $displaycat display that : Array ( [0] => Accueil [1] => Evénements [2] => hatha-yoga [3] => Sections ). The result of print_r($onesection); is an empty array instead of Array([0] => hatha-yoga. My target is to get one of categories name "hatha-yoga" of one post to generate a link to return to the mai...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.