Jump to content

Array Layout Help


tobeyt23

Recommended Posts

Ok I am having a brain fart and/or not enough coffee! See the below array:

Array
(
    [0] => Array
        (
            [breed_name] => Boxer
            [dog_name] => Fred
        )

    [1] => Array
        (
            [breed_name] => Labrador Retriever
            [dog_name] => Roxy
        )

    [2] => Array
        (
            [breed_name] => Labrador Retriever
            [dog_name] => Semper
        )

    [3] => Array
        (
            [breed_name] => Mixed Breed
            [dog_name] => Bronchito
        )

    [4] => Array
        (
            [breed_name] => Mixed Breed
            [dog_name] => Fonz
        )

    [5] => Array
        (
            [breed_name] => Mixed Breed
            [dog_name] => Triton
        )

)

I need to lay this out like so:

 

Boxer - Fred 
Labrador Retriever - Roxy, Semper 
Mixed Breed - Bronchito, Fonz, Triton

 

Any help would be appreciated as i am just banging my head currently!

Link to comment
Share on other sites

If you stored this into a dbms table you could write a query to organize the data in any fashion you might need today and in the future. If this is meant to be any kind of static data, I highly recommend that you re-think your storage plan. Sure you can do this, but the better approach is as I am suggesting.

Link to comment
Share on other sites

Would have been nice if you mentioned that.

 

Do your query to order the results by breed. Then loop thru the results echo'ing the breed and name on the first record. Then on the following records echo the name until you see a change in breed at which time you begin a new row with the breed name and the new dog name. And repeat. You'll have to save the 'last-breed' name after each record and use it to compare against the next record every time.

 

A very common approach to your problem.

Link to comment
Share on other sites

Feeling magnanimous today:

$q = 'select breed_name, dog_name from dogs_table
order by breed_name';
$qrslts = $pdo->query($q);
if (!$qrslts)
{
echo "Could not execute query";
exit();
}
$last_breed = 'xxx';
while(list($breed, $name) = $qrslts->fetch(PDO::FETCH_NUM))
{
if ($breed <> $last_breed)
{
if ($last_breed <> 'xxx')
echo '<br>';
echo "$breed - $name";
}
else
echo ", $name";
$last_breed = $breed;
}

 

 

(too bad the forum doesn't recognize the tabs in this code.)

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.