Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question

1 Answer 1

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

share|improve this answer
    
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
    
In your case it may be best to just do this: pastebin.com/afiF9WHg –  César Dec 15 '13 at 17:47
    
Thanks. I think so, and not echo the comma if it's the last iteration (using: stackoverflow.com/questions/1070244/…). 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.