1

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?

wharfdale
  • 1,148
  • 6
  • 23
  • 53

1 Answers1

0

I believe I came out with a solution, I got your idea but I'm afraid you have some issues in your code.

I've just refactored them, and the most important, I didn't get it how exactly are you going to populate your new $performer_urls array without overwriting keys, that's why I chose to incremental keys just using [ ] syntax.

First things first, yes PHP is right complaining about your $key undefined variable :-) example:

enter image description here

Then I refactored the wrong parts (as I got it), here it comes the full code:

<?php
$event['lineups'] = [
    [
        'id' => '22007301-f49f-442d-b93f-4c7ce5cbc8de',
        'name' => 'MC Bassman',
        'facebook_page_url' => 'https://www.facebook.com/bassmansdc/',
        'instagram_page_url' => 'https://www.instagram.com/mcbassman_sdc/',
        'official_website_url' => '',
        'position' => 1,
    ],
    [
        'id' => 'f4c41b6f-33a1-4da0-b7fa-ffdfbd84724d',
        'name' => 'Indika',
        'facebook_page_url' => 'https://www.facebook.com/INDIKAMCR/',
        'instagram_page_url' => 'https://www.instagram.com/indikamcr/',
        'official_website_url' => null,
        'position' => 2,
    ],
];

$default_keys = [
    'facebook_page_url',
    'instagram_page_url',
    'official_website_url',
];

$performer_urls = [];

foreach ($event['lineups'] as $lineups) {
    foreach ($lineups as $key => $value) {
        if (in_array($key, $default_keys) && !empty($value)) {
            $performer_urls[] = $value;
        }
    }
}

var_dump($performer_urls);

I've got this:

array(4) {
  [0] =>
  string(36) "https://www.facebook.com/bassmansdc/"
  [1] =>
  string(40) "https://www.instagram.com/mcbassman_sdc/"
  [2] =>
  string(35) "https://www.facebook.com/INDIKAMCR/"
  [3] =>
  string(36) "https://www.instagram.com/indikamcr/"
}
  • My code is within another foreach loop which loops through each event. Foreach event, output structured data. This probably clears up the idea that it seems things will be over-ridden. I can't have just 1 array with all urls. – wharfdale May 01 '19 at 08:23
  • `array(2)` - bassmansdc, mcbassman_sdc. `array(2)` - INDIKAMCR, indikamcr – wharfdale May 01 '19 at 08:24