0

I have this table, resulting from a SELECT + INNER && LEFT JOIN (this is the spiegation of the NULL values).

cookery         id_cookery  detail       id_detail
Locale          1            NULL        NULL
Regionale       2            NULL        NULL
Regionale       2            Abruzzese   1
Nazionale       3            NULL         NULL
Internazionale  4            NULL         NULL
Internazionale  4            Africana    3
Internazionale  4            Americana   82

My framework put this result in this array:

Array
        (
            [0] => Array
                (
                    [cookery] => Locale
                    [id_cookery] => 1
                    [detail] => 
                    [id_detail] => 
                )

            [1] => Array
                (
                    [cookery] => Regionale
                    [id_cookery] => 2
                    [detail] => 
                    [id_detail] => 
                )

            [2] => Array
                (
                    [cookery] => Regionale
                    [id_cookery] => 2
                    [detail] => Abruzzese
                    [id_detail] => 1
                )

            [3] => Array
                (
                    [cookery] => Nazionale
                    [id_cookery] => 3
                    [detail] => 
                    [id_detail] => 
                )

            [4] => Array
                (
                    [cookery] => Internazionale
                    [id_cookery] => 4
                    [detail] => 
                    [id_detail] => 
                )

            [5] => Array
                (
                    [cookery] => Internazionale
                    [id_cookery] => 4
                    [detail] => Africana
                    [id_detail] => 3
                )

            [6] => Array
                (
                    [cookery] => Internazionale
                    [id_cookery] => 4
                    [detail] => Americana
                    [id_detail] => 82
                )

        )

By the way, I need to create an array with this aspect, grouping the "cookery" in only 4 field (Locale, Regionale, Nazionale and Internazionale) and IF the cookery has "detail", putting them sub his key.

A sort of:

Array (locale => '',
regionale => Abruzzese
nazionale => '',
Internazionale => Africana, Americana)

(probably I did not write correctly this Array, but I hope that you understand).

1) I tried first of all to save in a temp array only the different id_cookery, to use later

 if (!in_array($value['id_cookery'],$tmp))
            {
                $tmp[] = $value['id_cookery']);
            }

But sincerly I don't know what I can do with it...

2) I tried to save in another array only the values != NULL

if ($value['detail']!='')
                {
                    $temp[$key][] = $value['cookery'];
                    $temp[$key][] = $value['detail'];
                    $temp[$key][] = $value['id_cookery'];
                }

But at this time... How I can merge the arrays and obtain my goal? Or can you help, following any road?

Thank you very, very much.

PS How I can post a mysql table to view correctly? I saw in others answer that posted Mysql table seems a jpeg from terminal... :)

2 Answers 2

2

Probably you want something like this:

$input = your framework array;
$output = array();
foreach($input as $row){
    if(!isset($output[$row['cookery']])){
        $output[$row['cookery']] = array();
    }
    if($row['detail']){
        $output[$row['cookery']][] = $row['detail'];
    }
}

var_dump($output);

PS. the screenshots are actualy done in a terminal! They use the mysql command line

Sign up to request clarification or add additional context in comments.

4 Comments

mysql_* is deprecated :)
yeah... sometimes relying on frameworks "detaches" you from real world :)
You must forgive me. I'm using a framework and it extract from mysql with the array that I ve posted just now. Please look at the edited question. Thank you!
Edited to use an array as input :)
1

I think this is what you are looking for:

$answer = array();

//Loop your rows here

if(false == array_key_exists($value['cookery'], $answer)){
    $answer[$value['cookery']] = array();
}

if($value['detail'] != null && false == array_key_exists($value['detail'], $answer[$value['cookery']])){
    $answer[$value['cookery']][] = $value['cookery']
}

//end loop here

Note: This will allow not allow duplicate values in the detail.

However, this results in

Array (locale => Array(),
regionale => Array(Abruzzese),
nazionale => Array(),
Internazionale => Array(Africana, Americana))

I would then change them to a string (if that is what you desire with an implode loop.

$finalAnswer = array();
foreach($answer as $key => $answerEntry){
    $finalAnswer[$key] = implode(",",$answerEntry);
}

1 Comment

This is perfect. Only to change $answer[$value['cookery']][] = $value['cookery'] with $answer[$value['cookery']][] = $value['detail']; Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.