1

Using this sample multidimensional array (of a palette which contains colours, which in turn contains their respective shades), let’s say I would like to display the colours in an imploded list (comma-separated) and, if applicable, its respective shades in brackets, also in an imploded (comma-separated) list.

I can easily implode the inner array (shades), but cannot figure out how to do that with the outer array (colours) given it contains the array of shades which must be run through for each colour.

I’ve seen there are several solutions for imploding a multidimensional array, but these seem to be without requiring running through a possible inner array for each. Perhaps there is another method by which to separate the entries with a comma?

And while I’m on the subject, is there a way of replacing the last comma of an imploded string with ‘and’?

Thanks in advance.

$sql =  "SELECT DISTINCT colour_id, colour_nm, colour_url
            FROM palettecolours
            INNER JOIN colour ON colourid = colour_id
            WHERE paletteid = '$palette_id'";
while ($row = mysqli_fetch_array($result))
{
    $colour = '<a href = "/colour/' . $row['colour_name'] . '">' . $row['colour_url'] . '</a>';
    $colours[$row['colour_id']] = array('colour' => $colour, 'shades' => array());
}

$sql =  "SELECT colourid, shade_name, shade_url
            FROM palettecolours
            INNER JOIN shade ON shadeid = shade_id
            WHERE paletteid = '$palette_id'";
while ($row = mysqli_fetch_array($result))  
{
    $shade = '<a href = "/shade/' . $row['shade_name'] . '">' . $row['shade_url'] . '</a>';
    $colours[$row['colourid']]['shades'][] = array('shade' => $shade);
}

<?php foreach ($colours as $colour): ?>
<?php echo $colour['colour']; ?>
<?php if(!empty($colour['shades'])) { ?>(<?php echo implode(", ", $colour['shades']); ?>)<?php } ?>
<?php endforeach; ?>

CURRENT DISPLAY:- Red (Magenta, Burgundy, Crimson) Blue Green Yellow (Egyptian Cotton, Magnolia) White (Soft Moon)

DESIRED OUTCOME:- Red (Magenta, Burgundy, Crimson), Blue, Green, Yellow (Egyptian Cotton, Magnolia), White (Soft Moon)

Andy Gout
  • 302
  • 2
  • 19

2 Answers2

0

How about recursive functions? Something like

function array_implode_recursive($glue, $data, $before = '(', $after = ')') {
    //Loop through every child and check whether it is an array or not and implode it if so
    foreach($data as &$element) {
        if (is_array($element)) {
            $element = $before . array_implode_recursive($glue, $element) . $after;
        }
    }
    //It's really safe to erase this variable as sometimes PHP has fun with them
    unset($element);

    return implode($glue, $data);
}

Use it like this

$mydata = implode_recursive(', ', $data);
$mydata = implode_recursive(', ', $data, '[', ']');

Hope it helped

César
  • 370
  • 1
  • 9
  • Sorry to be really thick, but can you advise me as to how I'd apply this to the above example as I'm having some problems getting it to work. Are the respective piece of $data given to the functions the outer and inner arrays respectively? – Andy Gout Dec 14 '13 at 11:52
  • Thanks. I think so, and not echo the comma if it's the last iteration (using: http://stackoverflow.com/questions/1070244/how-to-determine-the-first-and-last-iteration-in-a-foreach-loop). And handily with this method I can also change the comma to 'and' if it's the penultimate iteration. – Andy Gout Dec 16 '13 at 19:35
0

Since you know what your array looks like and it appears to have keys you could try something similar to what I've done here.

Community
  • 1
  • 1
Mike
  • 1,853
  • 3
  • 45
  • 75