Partage
  • Partager sur Facebook
  • Partager sur Twitter

Concaténer deux arrays

Sujet résolu
    4 mai 2021 à 9:42:44

    Bonjour à tous,

    J'ai deux tableaux que je voudrais concaténer suivant un critère bien précis.

    Quand dans array1 l'Id est identique à l'Ide de l'array 2 alors j'ajoute le champ 'ENERGY_ADDED' à l'array 2.

    Voici un aperçu des deux tableaux.

    array (size=280)
      0 => 
        object(stdClass)[183]
          public 'activity_id' => string '7124158' (length=7)
          public 'ENERGY_ADDED' => string '38.9' (length=4)
      1 => 
        object(stdClass)[271]
          public 'activity_id' => string '7125447' (length=7)
          public 'ENERGY_ADDED' => string '39.1' (length=4)
    
    
    
     array (size=51)
      0 => 
        object(stdClass)[185]
          public 'ID' => string '7795415' (length=7)
          public 'STATE_ID' => string '3' (length=1)
          public 'START_AT' => string '2021-04-30 19:03:29' (length=19)
          public 'FINISH_AT' => string '2021-05-01 00:18:26' (length=19)
          public 'DAY_START' => string '30' (length=2)
          public 'DAY_END' => string '1' (length=1)
          public 'DURATION' => string '05:14:57' (length=8)
      1 => 
        object(stdClass)[223]
          public 'ID' => string '7124158' (length=7)
          public 'STATE_ID' => string '3' (length=1)
          public 'START_AT' => string '2021-05-01 00:20:48' (length=19)
          public 'FINISH_AT' => string '2021-05-01 04:31:59' (length=19)
          public 'DAY_START' => string '1' (length=1)
          public 'DAY_END' => string '1' (length=1)
          public 'DURATION' => string '04:11:11' (length=8)
      2 => 
        object(stdClass)[224]
          public 'ID' => string '7125447' (length=7)
          public 'STATE_ID' => string '3' (length=1)
          public 'START_AT' => string '2021-05-01 04:33:57' (length=19)
          public 'FINISH_AT' => string '2021-05-01 04:38:48' (length=19)
          public 'DAY_START' => string '1' (length=1)
          public 'DAY_END' => string '1' (length=1)
          public 'DURATION' => string '00:04:51' (length=8)
      3 => 
        object(stdClass)[225]
          public 'ID' => string '7798104' (length=7)
          public 'STATE_ID' => string '3' (length=1)
          public 'START_AT' => string '2021-05-01 04:47:47' (length=19)
          public 'FINISH_AT' => string '2021-05-01 04:49:48' (length=19)
          public 'DAY_START' => string '1' (length=1)
          public 'DAY_END' => string '1' (length=1)
          public 'DURATION' => string '00:02:01' (length=8)

    Je voudrais éviter de faire des boucles imbriquées ;)

    Merci de votre aide.

    Bonne journée.

    DorBin1528

    • Partager sur Facebook
    • Partager sur Twitter
      5 mai 2021 à 14:35:40

      Salut, je vois pas comment le faire sans parcourir tes deux tableaux et comparer les ids...

      Perso je ferai comme ceci

      <?php
      $array1 = [
          0 => (object)
          [
              'activity_id' => '7124158',
              'ENERGY_ADDED' => '38.9',
          ],
          1 => (object) [
              'activity_id' => '7125447',
              'ENERGY_ADDED' => '39.1',
          ],
      
      
      ];
      
      $array2 = [
          0 =>
              (object)[
                  'ID' => '7795415',
                  'STATE_ID' => '3',
                  'START_AT' => '2021-04-30 19:03:29',
                  'FINISH_AT' => '2021-05-01 00:18:26',
                  'DAY_START' => '30',
                  'DAY_END' => '1',
                  'DURATION' => '05:14:57',
              ],
          1 =>
              (object)[
                  'ID' => '7124158',
                  'STATE_ID' => '3',
                  'START_AT' => '2021-05-01 00:20:48',
                  'FINISH_AT' => '2021-05-01 04:31:59',
                  'DAY_START' => '1',
                  'DAY_END' => '1',
                  'DURATION' => '04:11:11',
              ],
          2 =>
              (object)[
                  'ID' => '7125447',
                  'STATE_ID' => '3',
                  'START_AT' => '2021-05-01 04:33:57',
                  'FINISH_AT' => '2021-05-01 04:38:48',
                  'DAY_START' => '1',
                  'DAY_END' => '1',
                  'DURATION' => '00:04:51',
              ],
      ];
      
      array_map(
          static function ($v) use($array1){
          array_map(
              static function($v2) use ($v){
              if($v->ID === $v2->activity_id){
                  $v->ENERGY_ADDED = $v2->ENERGY_ADDED;
              }
          }, $array1, []);
      
      }, $array2, []);

      Si tu trouves une autre solution je suis curieux de la voir

      @+

      • Partager sur Facebook
      • Partager sur Twitter

      While(true)

        6 mai 2021 à 21:48:58

        Salut Warps73,

        Je n'ai rien de mieux, je vais garder ta solution.

        Merci à toi

        -
        Edité par DorBin1528 6 mai 2021 à 21:50:08

        • Partager sur Facebook
        • Partager sur Twitter

        Concaténer deux arrays

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown