Get all values from a multidimensional array into a new array

So I am getting an array of values from an Advanced Custom Fields repeater field (WordPress)
I’ve named the the array $VideoCategorys

// check if the repeater field has rows of data
if( have_rows('video_headers_list') ):

    // Get an array of the items in the list
    $VideoCategorys = get_field('video_headers_list');

else :

    // no rows found

endif;

What I’d then like to do is take the values from the array and enter them into a foreach loop which will then query posts (working in WordPress) for it’s respective category (the values from the $VideoCategorys are the same as some posts categories)

<?php
//Now I'd like to do a for loop in which I get the value for each array
foreach ($VideoCategorys as $Category) {

    query_posts('category_name=$Category'); 
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    echo '<h2>'; the_title(); echo '<h2>';
    endwhile; endif; 
    wp_reset_query(); 

}

?>    
<?php wp_reset_query(); ?>

However, I just realized that $VideoCategorys = get_field(‘video_headers_list’);
is giving me a multidimensional array. This is what I get when I do <?php var_dump($VideoCategorys); ?>

array (size=3)
  0 => 
    array (size=1)
      'video_category_name' => string 'Stretching Videos' (length=17)
  1 => 
    array (size=1)
      'video_category_name' => string 'Running Videos' (length=14)
  2 => 
    array (size=1)
      'video_category_name' => string 'Jumping Videos' (length=14)

MY QUESTION IS How do I get the multidimensional array values (stretching videos, running videos, jumping videos) into an array that I can use in the foreach loop?

query_posts('category_name=' . $Category['video_category_name']);

1 Like

This works but it keeps another issue. I’m going to show you the updated code now.
I don’t get why the header with the class VideoSecHeaderH2 is returning the value of ARRAY?
And can someone tell me why I keep getting the scroll bar when I add code on here? How do I avoid that?

//Now I'd like to do a for loop in which I get the value for each array
	foreach ($VideoCategorys as $Category) {
						
		$CategoryName = $Category; 
		$CategoryNameHeader = $Category;
						
						query_posts('category_name=' . $CategoryName['video_category_name']); 
                        if ( have_posts() ) : 
						
			echo '<section class="col-xs-12 NoPaddingLeftandRight">';
			 $CategoryNameHeader = $Category;
			echo '<h2 class="VideoSecHeaderH2 col-xs-12 NoPaddingLeftandRight">'; echo $CategoryNameHeader; echo '</h2>';
							
							while ( have_posts() ) 
							: the_post(); 
							 echo '<section class="col-lg-6 NoPaddingLeftandRight">';
								 echo '<h2 class="col-xs-12 NoPaddingLeftandRight"> <a class="col-xs-12 NoPaddingLeftandRight" href='; the_permalink(); echo'>'; the_title();  echo '</a> </h2>';
								 echo '<div class="embed-container">'; the_field('youtube_video'); echo '</div>';
							 echo '</section>';	 
							endwhile; 
							
							echo '</section>';
						
						else: 
						
						
						endif; 
                        wp_reset_query(); 
						
					}
				
				?>    
                <?php wp_reset_query(); ?>

Because $Category is an array.
The array is assigned to 2 new variables (thus they too are arrays)

$CategoryName = $Category; 
$CategoryNameHeader = $Category;

This line gets one member of the array - “video_category_name”

query_posts('category_name=' . $CategoryName['video_category_name']);

This line does not get any particular member of the array, but the entire array itself.

echo '<h2 class="VideoSecHeaderH2 col-xs-12 NoPaddingLeftandRight">'; echo $CategoryNameHeader; echo '</h2>';

*the code boxing only expands to a limited size to help keep individual posts from getting too wide / long.
While seeing a post of interest without scrollbars would be nice. having a topic with long posts would not be so nice if trying to get to a particular post in the topic.

To not have scrollbars, the code needs to be narrow / short enough to fit in the allotted size.
Which of course is not an ideal solution, but IMHO is a fair compromise.

Well I thought that $Category in this foreach ($VideoCategorys as $Category) is the value within the array. I was thinking that since I assigned $CategoryNameHeader the value of the array that I could output it when I called that variable later. Why can’t I do that? Also how can I call it later?

I used this to get the headers/categories but it still doesn’t make since to me why $Category is an array? Wait. Is it because $Category is an array of another array?

echo '

'; echo $CategoryName['video_category_name']; echo '

';

An array can hold various things. In this case one of them is another array. eg.

$arr = array(
       "type" => "clothing"
      , "second" => 2
      , "name" => array(
                  "first" => "Abercrombie"
                 , "last" => "Fitch"
                 )
);

So you could do $arr['type'] or $arr['second'] but $arr['name'] would not work, and return “array”.

Those need to be like $arr['name']['first'] and $arr['name']['last']

I think I get what you’re saying but this did work and is returning the values from the array.
echo $CategoryName[‘video_category_name’]; echo ‘’;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.