string(1) "1" ["...">

Looping a multidimensional array in php

I have a multidimensional array like this:

array(2) {
  [1]=>
  array(3) {
    ["eventID"]=>
    string(1) "1"
    ["eventTitle"]=>
    string(7) "EVENT 1"
    ["artists"]=>
    array(3) {
      [4]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 1"
        ["description"]=>
        string(13) "artist 1 desc"
        ["links"]=>
        array(2) {
          [1]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist1.com"
          }
          [6]=>
          array(2) {
            ["URL"]=>
            string(24) "http://www.artist1-2.com"
          }
        }
      }
      [5]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 8"
        ["description"]=>
        string(13) "artist 8 desc"
        ["links"]=>
        array(1) {
          [8]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist8.com"
          }
        }
      }
      [2]=>
      array(2) {
        ["ime"]=>
        string(8) "ARTIST 5"
        ["opis"]=>
        string(13) "artist 5 desc"
        ["links"]=>
        array(1) {
          [9]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist5.com"
          }
        }
      }
    }
  }
  [2]=>
  array(3) {
    ["eventID"]=>
    string(1) "2"
    ["eventTitle"]=>
    string(7) "EVENT 2"
    ["artists"]=>
    array(3) {
      [76]=>
      array(2) {
        ["name"]=>
        string(9) "ARTIST 76"
        ["description"]=>
        string(14) "artist 76 desc"
        ["links"]=>
        array(1) {
          [13]=>
          array(2) {
            ["URL"]=>
            string(23) "http://www.artist76.com"
          }
        }
      }
      [4]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 4"
        ["description"]=>
        string(13) "artist 4 desc"
        ["links"]=>
        array(1) {
          [11]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist4.com"
          }
        }
      }
    }
  }
}

I would like to draw the html output as follows:

-

EVENT 1
ARTIST 1
artist 1 descending http://www.artist1.com , http://www.artist1-2.com

ARTIST 8
artist 8 descending http://www.artist8.com

ARTIST 5
artist 5 descending http://www.artist5.com

-

EVENT 2
ARTIST 76
artist 76 descending http://www.artist76.com

ARTIST 4
artist 4 descending http://www.artist4.com

-

and etc.

, , , // ..
, ! =)

.

+3
3

foreach . (, , , !), :

foreach ($mainArray as $event)
{
  print $event["eventTitle"];

  foreach ($event["artists"] as $artist)
  {
     print $artist["name"];
     print $artist["description"];

     $links = array();
     foreach ($artist["links"] as $link)
     {
       $links[] = $link["URL"];
     }
     print implode(",", $links);
  }
}
+11

foreach , . :

foreach($array as $value) {
    foreach($value as $key => $val) {
        if($key == "links") {

        }
        /* etc */
    }
}
+6

, - " ", .. , , , . , , , , .

, (.. ). , "ime" "opi" "" "" ;)

, . , , - , , "URL". :

function render_link($link) {
  return "<a href='{$link['URL']}'>{$link['URL']}</a>";
}

, . :

// Input
array('URL' => "http://www.artist1.com")

// Output
"<a href='http://www.artist1.com'>http://www.artist1.com</a>"

'links'. : "render_link" , "array_map", , "implode", :

function render_links($links_array) {
  $rendered_links = array_map('render_link', $links_array);
  return implode(', ', $rendered_links);
}

, :

// Input
array(1 => array('URL' => "http://www.artist1.com"),
      6 => array('URL' => "http://www.artist1-2.com"))

// Output
"<a href='http://www.artist1.com'>http://www.artist1.com</a>, <a href='http://www.artist1-2.com'>http://www.artist1-2.com</a>"

, , "", "" "". , "", , :

function render_artist($artist) {
  // Replace the artist links with a rendered version
  $artist['links'] = render_links($artist['links']);

  // Render this artist details on separate lines
  return implode("\n<br />\n", $artist);
}

, :

// Input
array('name'        => 'ARTIST 1',
      'description' => 'artist 1 desc',
      'links'       => array(
         1          => array(
           'URL'    => 'http://www.artist1.com')
         6          => array(
           'URL'    => 'http://www.artist1-2.com')))

// Output
"ARTIST 1
 <br />
 artist 1 desc
 <br />
 <a href='http://www.artist1.com'>http://www.artist1.com</a>, <a href='http://www.artist1-2.com'>http://www.artist1-2.com</a>"

''. , , array_map implode, :

function render_artists($artists) {
  $rendered = array_map('render_artist', $artists);
  return implode("\n<br /><br />\n", $rendered);
}

( , ;))

, , , :

function render_event($event) {
  unset($event['eventID']);
  $event['eventTitle'] = "<strong>{$event['eventTitle']}</strong>";
  $event['artists']    = render_artists($event['artists']);
  return implode("\n<br />\n", $event);
}

, . , :

function render_events($events) {
  $rendered = array_map('render_event', $events);
  return implode("\n<br /><br />--<br /><br />", $rendered);
}

, , render_events HTML-:

echo render_events($my_data);

, , , . , render_links, render_artists render_events, :

function reduce_with($renderer, $separator, $array) {
  return implode($separator, array_map($renderer, $array));
}

function render_artist($artist) {
  $artist['links'] = reduce_with('render_link', ', ', $artist['links']);
  return implode("\n<br />\n", $artist);
}

function render_event($event) {
  unset($event['eventID']);
  $event['eventTitle'] = "<strong>{$event['eventTitle']}</strong>";
  $event['artists']    = reduce_with('render_artist',
                                     "\n<br /><br />\n",
                                     $event['artists']);
  return implode("\n<br />\n", $event);
}

echo reduce_with('render_event', "\n<br /><br />--<br /><br />", $my_data);

, . , . , :

// Re-usable library code

// Partial application: apply some arguments now, the rest later
function papply() {
  $args1 = func_get_args();
  return function() use ($args1) {
    return call_user_func_array(
      'call_user_func',
      array_merge($args1, func_get_args()));
  };
}

// Function composition: chain functions together like a(b(c(...)))
function compose() {
  $funcs = array_reverse(func_get_args());
  $first = array_shift($funcs);
  return function() use ($funcs, $first) {
    return array_reduce($funcs,
                        function($x, $f) { return $f($x); },
                        call_user_func_array($first, func_get_args()));
  };
}

// Transform or remove a particular element in an array
function change_elem($key, $func, $array) {
  if is_null($func) unset($array[$key]);
  else $array[$key] = $func($array[$key]);
  return $array;
}

// Transform all elements then implode together
function reduce_with($renderer, $separator) {
  return compose(papply('implode', $separator),
                 papply('array_map', $renderer));
}

// Wrap in HTML
function tag($tag, $text) {
  return "<{$tag}>{$text}</{$tag}>";
}

// Problem-specific code

function render_link($link) {
  return "<a href='{$link['URL']}'>{$link['URL']}</a>";
}

$render_artist = compose(
  papply('implode', "\n<br />\n"),
  papply('change_elem', 'links', papply('reduce_with',
                                        'render_link',
                                        ', '));

$render_event = compose(
  papply('implode', "\n<br />\n"),
  papply('change_elem', null,         'eventID'),
  papply('change_elem', 'eventTitle', papply('tag', 'strong')),
  papply('change_elem', 'artists',    papply('reduce_with',
                                             $render_artist,
                                             "\n<br /><br />\n")));

echo reduce_with($render_event, "\n<br /><br />--<br /><br />", $my_data);
+5
source

Source: https://habr.com/ru/post/1719834/


All Articles