Jump to content

Array to Table


tobeyt23

Recommended Posts

I have an array and want to generate a table for each data set that has the same product name. Completely drawning a brain fart on this one.

Array
(
    [0] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR
                    [county] => KENT
                    [mean] => 366.67
                    [median] => 375.00
                    [mode] => 375.00
                )

        )

    [1] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR
                    [county] => NEW CASTLE
                    [mean] => 360.00
                    [median] => 375.00
                    [mode] => 375.00
                )

        )

    [2] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR
                    [county] => SUSSEX
                    [mean] => 378.57
                    [median] => 375.00
                    [mode] => 375.00
                )

        )

    [3] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR W MC & UAD
                    [county] => KENT
                    [mean] => 0.00
                    [median] => 0.00
                    [mode] => 0.00
                )

        )

    [4] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR W MC & UAD
                    [county] => NEW CASTLE
                    [mean] => 0.00
                    [median] => 0.00
                    [mode] => 0.00
                )

        )

    [5] => Array
        (
            [product] => Array
                (
                    [name] => 1004 URAR W MC & UAD
                    [county] => SUSSEX
                    [mean] => 0.00
                    [median] => 0.00
                    [mode] => 0.00
                )

        )

    [6] => Array
        (
            [product] => Array
                (
                    [name] => 1073 CONDO
                    [county] => KENT
                    [mean] => 370.83
                    [median] => 375.00
                    [mode] => 375.00
                )

        )

    [7] => Array
        (
            [product] => Array
                (
                    [name] => 1073 CONDO
                    [county] => NEW CASTLE
                    [mean] => 305.65
                    [median] => 375.00
                    [mode] => 375.00
                )

        )

    [8] => Array
        (
            [product] => Array
                (
                    [name] => 1073 CONDO
                    [county] => SUSSEX
                    [mean] => 375.00
                    [median] => 400.00
                    [mode] => 400.00
                )

        )
)

So i want a new table when the ['product']['name'] doesn't match. Any suggestions, thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/283661-array-to-table/
Share on other sites

untested...

 

 

echo "<table border='1'>";
foreach ($mainArray as $i => $array) {
  if ( isset($mainArray[$i-1]) && ($mainArray[$i-i]['product']['name'] != $array['product']['name']) )
    echo "</table><table border='1'>";
  echo "<tr><td>".implode("</td><td>",$array['product'])."</td></tr>";
}
echo "</table>";
Link to comment
https://forums.phpfreaks.com/topic/283661-array-to-table/#findComment-1457228
Share on other sites

A better array structure to start from would have helped. If your array is $data, then this will transform to an easier array for your task

$newdata = array();
foreach ($data as $prod) {
    $newdata[$prod['product']['name']][] = array_slice($prod['product'],1);
}
echo '<pre>',print_r($newdata, true),'</pre>';

/** RESULTING ARRAY *****************************
Array
(
    [1004 URAR] => Array
        (
            [0] => Array
                (
                    [county] => KENT
                    [mean] => 366.67
                    [median] => 375
                    [mode] => 375
                )

            [1] => Array
                (
                    [county] => NEW CASTLE
                    [mean] => 360
                    [median] => 375
                    [mode] => 375
                )

            [2] => Array
                (
                    [county] => SUSSEX
                    [mean] => 378.57
                    [median] => 375
                    [mode] => 375
                )

        )

    [1004 URAR W MC & UAD] => Array
        (
            [0] => Array
                (
                    [county] => KENT
                    [mean] => 0
                    [median] => 0
                    [mode] => 0
                )

            [1] => Array
                (
                    [county] => NEW CASTLE
                    [mean] => 0
                    [median] => 0
                    [mode] => 0
                )

            [2] => Array
                (
                    [county] => SUSSEX
                    [mean] => 0
                    [median] => 0
                    [mode] => 0
                )

        )

    [1073 CONDO] => Array
        (
            [0] => Array
                (
                    [county] => KENT
                    [mean] => 370.83
                    [median] => 375
                    [mode] => 375
                )

            [1] => Array
                (
                    [county] => NEW CASTLE
                    [mean] => 305.65
                    [median] => 375
                    [mode] => 375
                )

            [2] => Array
                (
                    [county] => SUSSEX
                    [mean] => 375
                    [median] => 400
                    [mode] => 400
                )

        )

)
***********************************************/
Link to comment
https://forums.phpfreaks.com/topic/283661-array-to-table/#findComment-1457229
Share on other sites

Just for topic completion I am posting a solution using the revised array format

$tHead = "<table cellpadding='5' border='1' style='border-collapse: collapse'>
    <tr><th>County</th><th>Mean</th><th>Median</th><th>Mode</th></tr>\n";
$tFoot = "</table><br><br>\n";

foreach ($newdata as $name => $arr) {
    echo "<strong>$name</strong><br>$tHead";
    foreach ($arr as $row) {
        echo "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
    }
    echo $tFoot;
}
Link to comment
https://forums.phpfreaks.com/topic/283661-array-to-table/#findComment-1457235
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.