jarvis Posted June 16, 2023 Share Posted June 16, 2023 Hi, I'm hoping someone can help. I've got the following array: Array ( [blockName] => acf/testimonials [attrs] => Array ( [name] => acf/testimonials [data] => Array ( [title] => [_title] => field_6017f48066e05 [title_tag] => H1 [_title_tag] => field_6017f48d66e06 [testimonials_0_testimonial] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque id scelerisque ante. Sed nibh tortor, laoreet vitae risus a, ultricies congue ex. Nulla iaculis, libero et feugiat efficitur, lorem turpis blandit nisi, vel convallis enim ex eget eros. [_testimonials_0_testimonial] => field_6017f4631cdc0 [testimonials_0_name] => [_testimonials_0_name] => field_6017f4691cdc1 [testimonials_0_company] => [_testimonials_0_company] => field_6017f46e1cdc2 [testimonials_1_testimonial] => Maecenas fermentum fermentum nibh quis maximus. Vivamus non tellus sed odio convallis aliquet. Quisque volutpat metus vel quam facilisis ullamcorper. [_testimonials_1_testimonial] => field_6017f4631cdc0 [testimonials_1_name] => [_testimonials_1_name] => field_6017f4691cdc1 [testimonials_1_company] => [_testimonials_1_company] => field_6017f46e1cdc2 [testimonials] => 2 [_testimonials] => field_6017f44c1cdbf [add_background_colour] => 0 [_add_background_colour] => field_6017f4ad66e07 [image] => [_image] => field_6017f72f66e09 [overlay_colour] => Light [_overlay_colour] => field_6017f75066e0a [overlay_opacity] => 0 [_overlay_opacity] => field_6017f76d66e0b ) [mode] => edit ) [innerBlocks] => Array ( ) [innerHTML] => [innerContent] => Array ( ) ) This comes from the following code: $blocks = parse_blocks($post->post_content); foreach ($blocks as $block) { echo '<pre>'; print_r($block); echo '</pre>'; if ($block['blockName'] == 'acf/testimonials') { $testimoainal[] = $block['attrs']['data']['testimonials_0_testimonial']; } } echo '<pre>'; print_r($testimoainal); echo '</pre>'; What I'm trying to do, is create an array with just the testimonials. I can see how to get the first one but what I can't work out is how I could loop through to grab the others, especially as I don't know how many there might be. So can't leave it hard coded I wonder how I can achieve this please? Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/ Share on other sites More sharing options...
ginerjm Posted June 16, 2023 Share Posted June 16, 2023 Is an array really necessary? A db would give you something manageable and useable a persistency rather than all of the levels of data you are currently displaying. Forgive me and my personal gripe - just don't see the fascination with arrays that some posters have. Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609783 Share on other sites More sharing options...
requinix Posted June 16, 2023 Share Posted June 16, 2023 What's this parse_blocks doing? Because if it's returning a whole bunch of information you don't want and the parts you do want aren't in the format you need, then shouldn't that be adjusted? Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609786 Share on other sites More sharing options...
jarvis Posted June 19, 2023 Author Share Posted June 19, 2023 @ginerjm unfortunately, this is how the data is stored and is beyond my control. I'm just having to work with what's already there @requinix parse_blocks returns the data as an array. From what I can tell, there is no way to limit what's returned, it just gets everything Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609809 Share on other sites More sharing options...
requinix Posted June 19, 2023 Share Posted June 19, 2023 1 hour ago, jarvis said: parse_blocks returns the data as an array. From what I can tell, there is no way to limit what's returned, it just gets everything parse_blocks isn't a built-in function. Something is providing it, likely either your own code or some library you installed. And if it's your own code then you can change it... But anyway, I'm not sure given your description, but it sounds like you need a foreach loop on that array which looks for any keys starting with "testimonials_". Extract the number and the last part and use those to build a different array - perhaps one looking like array( 0 => array( testimonial => ... name => ... company => ... ) ) Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609811 Share on other sites More sharing options...
jarvis Posted June 19, 2023 Author Share Posted June 19, 2023 Thanks @requinix This was my solution: foreach ($blocks as $block) { if ($block['blockName'] == 'acf/testimonials') { foreach($block['attrs']['data'] as $key => $value) { if (str_starts_with($key,'testimonials_') && str_ends_with($key,'_testimonial')) { $testimoainal[] = $value . "<br>"; } } } } echo '<pre>'; print_r($testimoainal); echo '</pre>'; Perhaps not overly pretty but does the job Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609815 Share on other sites More sharing options...
ginerjm Posted June 19, 2023 Share Posted June 19, 2023 10 hours ago, jarvis said: @ginerjm unfortunately, this is how the data is stored and is beyond my control. I'm just having to work with what's already there @requinix parse_blocks returns the data as an array. From what I can tell, there is no way to limit what's returned, it just gets everything You seem to contradict yourself. First you say 'this is how the data is stored' then you say that a function 'returns the data as an array'. So is it NOT stored as an array? And just how is it stored - as a table or a flat file or some other method? It's been 3 days since you asked for help yet nobody is able to manage that for you yet. Perhaps you need to work on that data storage algorithm. Quote Link to comment https://forums.phpfreaks.com/topic/316980-get-values-from-array/#findComment-1609816 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.