I am trying to loop through these array items and grab data from just the url
items, adding all that collected data to a new array.
Steps I am trying to achieve:
1. Loop through the $event['lineups']
(4 items).
2. Loop through the data inside the event (6 items each)
3. Grab the following from each:
facebook_page_url
, instagram_page_url
, official_website_url
array (size=4)
0 =>
array (size=6)
'id' => string '22007301-f49f-442d-b93f-4c7ce5cbc8de' (length=36)
'name' => string 'MC Bassman' (length=10)
'facebook_page_url' => string 'https://www.facebook.com/bassmansdc/' (length=36)
'instagram_page_url' => string 'https://www.instagram.com/mcbassman_sdc/' (length=40)
'official_website_url' => string '' (length=0)
'position' => int 1
1 =>
array (size=6)
'id' => string 'f4c41b6f-33a1-4da0-b7fa-ffdfbd84724d' (length=36)
'name' => string 'Indika' (length=6)
'facebook_page_url' => string 'https://www.facebook.com/INDIKAMCR/' (length=35)
'instagram_page_url' => string 'https://www.instagram.com/indikamcr/' (length=36)
'official_website_url' => null
'position' => int 2
My attempt and code:
Set which keys we need to grab data from:
$default_keys = [
'facebook_page_url',
'instagram_page_url',
'official_website_url',
];
Create a new array to add data to:
$performer_urls = [];
Loop through each $event['lineups']
item in array: array (size=4)
foreach( $event['lineups'] as $lineups ) {
Loop through each value in array: array (size=6)
foreach( $lineups as $lineup ) {
Check if data exists, if so, update the $performer_urls
array with a key and data.
if ( isset( $lineup[ $key ] ) && ! empty( $lineup[ $key ] ) ) {
$performer_urls[$key] = $event['lineups'][ $key ];
}
Full code so far:
$default_keys = [
'facebook_page_url',
'instagram_page_url',
'official_website_url',
];
$performer_urls = [];
foreach( $event['lineups'] as $lineups ) {
foreach( $lineups as $lineup ) {
// Example: $contact_details['address_line_1']
if ( isset( $lineup[ $key ] ) && ! empty( $lineup[ $key ] ) ) {
// Update array (example): $address['address_line_1'] = $contact_details['address_line_1']
$performer_urls[$key] = $event['lineups'][ $key ];
}
}
}
var_dump($performer_urls);
Notice: Undefined variable: key in
The errors I am getting now are referring to the undefined $key
variable being used but hopefully you can see what I am trying to achieve here and almost there?