Bonjour j'ai un tableau qui me retourne les données suivantes:

array(
    (int) 0 => array(
        'id' => '590',
        'day' => 'Monday',
        'Morning_opened' => '08:00',
        'Morning_closed' => '09:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ),
    (int) 1 => array(
        'id' => '591',
        'day' => 'Tuesday',
        'Morning_opened' => '00:00',
        'Morning_closed' => '00:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ) 
)

et j'aimerai l'indexer par le nom du modele associé pour obtenir quelque chose de ce genre

(int) 0 => array(
        'Opening' => array(
            'id' => '590',
            'day' => 'Monday',
            'Morning_opened' => '08:00',
            'Morning_closed' => '09:00',
            'Afternoon_opened' => '00:00',
            'Afternoon_closed' => '00:00',
            'company_id' => '57',
            'agency_id' => '0'
        )
    ),
    (int) 1 => array(
        'Opening' => array(
            'id' => '591',
            'day' => 'Tuesday',
            'Morning_opened' => '00:00',
            'Morning_closed' => '00:00',
            'Afternoon_opened' => '00:00',
            'Afternoon_closed' => '00:00',
            'company_id' => '57',
            'agency_id' => '0'
        )
    ),
    (int) 2 => array(
        'Opening' => array(
            'id' => '592',
            'day' => 'Wednesday',
            'Morning_opened' => '00:00',
            'Morning_closed' => '00:00',
            'Afternoon_opened' => '00:00',
            'Afternoon_closed' => '00:00',
            'company_id' => '57',
            'agency_id' => '0'
        )
    ),

quelqu'un aurait il une idée je sais qu'il faut le faire dans un afterFind mais j'ai pas d"idée !

3 réponses


Bonjour,

Voici un petit morceau de code qui permet de faire ce que tu désire, si j'ai bien compris ta problématique:

$array = array(
    (int) 0 => array(
        'id' => '590',
        'day' => 'Monday',
        'Morning_opened' => '08:00',
        'Morning_closed' => '09:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ),
    (int) 1 => array(
        'id' => '591',
        'day' => 'Tuesday',
        'Morning_opened' => '00:00',
        'Morning_closed' => '00:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ) 
);
var_dump($array);
foreach ($array as $k => $v) {
    unset($array$k]);
    $array$k]"Opening"] = $v;
}
var_dump($array);

Cordialement.

ndiayejp
Auteur

Merci pour la réponse effectivement mais si je veux avoir l'effet inverse
array(
(int) 0 => array(
'Opening' => array(
'id' => '625',
'day' => 'Monday',
'Morning_opened' => '08:00',
'Morning_closed' => '13:00',
'Afternoon_opened' => '00:00',
'Afternoon_closed' => '00:00',
'company_id' => '57',
'agency_id' => '0'
)
),
(int) 1 => array(
'Opening' => array(
'id' => '626',
'day' => 'Tuesday',
'Morning_opened' => '00:00',
'Morning_closed' => '00:00',
'Afternoon_opened' => '00:00',
'Afternoon_closed' => '00:00',
'company_id' => '57',
'agency_id' => '0'
)
),

c'est dire enlever le 'opening sur chaque résultat pour avoir '

(int) 0 => array(
        'id' => '590',
        'day' => 'Monday',
        'Morning_opened' => '08:00',
        'Morning_closed' => '09:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ),
    (int) 1 => array(
        'id' => '591',
        'day' => 'Tuesday',
        'Morning_opened' => '00:00',
        'Morning_closed' => '00:00',
        'Afternoon_opened' => '00:00',
        'Afternoon_closed' => '00:00',
        'company_id' => '57',
        'agency_id' => '0'
    ) .

Salut !

Voici l'inverse (si c'est ce que tu voulais)

<?php
$array = array(
    '0' => array(
        "Opening" => array(
            'id' => '590',
            'day' => 'Monday',
            'Morning_opened' => '08:00',
            'Morning_closed' => '09:00',
            'Afternoon_opened' => '00:00',
            'Afternoon_closed' => '00:00',
            'company_id' => '57',
            'agency_id' => '0'
        ),
    ),
    '1' => array(
        "Opening" => array(
            'id' => '591',
            'day' => 'Tuesday',
            'Morning_opened' => '00:00',
            'Morning_closed' => '00:00',
            'Afternoon_opened' => '00:00',
            'Afternoon_closed' => '00:00',
            'company_id' => '57',
            'agency_id' => '0',
        ),
    ) 
);

foreach ($array as $k => $v) {
    $value = $array$k]"Opening"];
    unset($array$k]);
    $array$k] = $value;
}
echo "<pre>";
var_dump($array);
echo "</pre>";
?>