Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    158

    Conforno di 2 array associativi x chiave e modifica dei valori

    Ho due array associative $Prodotti_1 e $Prodotti_2 del tipo:

    Supponiamo che $Prodotti_1 contiene:

    key -> Val

    3 =>2
    5 =>1
    7 =>3
    1=>2


    Mentre $Prodotti_2 contiene:

    9 =>3
    5 =>2

    Se la chiave del secondo array è gia presente nel primo sommo i valori, altrimenti agiungo un elemento all'array, in pratica devo ottenere un array del tipo:

    3 =>2
    5 =>3 ( 3 è la Somma dei valori degli elementi con la stessa chiava in questo caso Key=5)
    7 =>3
    1 =>2
    9 =>3 (Nuovo elemento che non era presente in $Prodotti_1)


    Come fare???

  2. #2
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    La funzione è presa dai commenti di array_merge_recursive
    Codice PHP:
    <?php
    $prodotti1 
    = array(3=>2,5=>1,7=>3,1=>2);
    $prodotti2 = array(9=>3,5=>2);

    echo 
    '<pre>';
    print_r($prodotti1);
    print_r($prodotti2);


    function 
    array_merge_recursive_keep_keys$arrElement1 $arrElement2 $intCount ) {

           
    $arrNew = array();
          
           
    $arrElement1Keys array_keys$arrElement1 );
           
    $arrElement2Keys array_keys$arrElement2 );
          
           
    $arrDifKeys1 array_diff$arrElement1Keys$arrElement2Keys );
           
    $arrDifKeys2 array_diff$arrElement2Keys$arrElement1Keys );
           
    $arrInter    array_intersect$arrElement1Keys $arrElement2Keys );

           foreach( 
    $arrDifKeys1 as $strKey1)
           {
               
    $arrNew$strKey1 ] = $arrElement1$strKey1 ];
           }
           foreach( 
    $arrDifKeys2 as $strKey2)
           {
               
    $arrNew$strKey2 ] = $arrElement2$strKey2 ];
           }
           foreach( 
    $arrInter as $strInterKey )
           {
               if( 
    is_array$arrElement1$strInterKey ] ) && is_array$arrElement2$strInterKey ] ) )
               {
                   
    $intCount++;
                   
    $arrNew$strInterKey ] = array_merge_recursive_keep_keys$arrElement1$strInterKey ] , $arrElement2$strInterKey ] , $intCount );
               }
               elseif( 
    is_array$arrElement1$strInterKey ] ) || is_array$arrElement2$strInterKey ] ) )
               {
                   
    $arrNew$strInterKey ][]    =  $arrElement1$strInterKey ];
                   
    $arrNew$strInterKey ][]    =  $arrElement2$strInterKey ];
               }
               else
               {
                   
    $arrNew$strInterKey ] = array();
                   
    $arrNew$strInterKey ][] = $arrElement1$strInterKey ];
                   
    $arrNew$strInterKey ][] = $arrElement2$strInterKey ];
               }
           }
           return 
    $arrNew;
    }    

    $unisci array_merge_recursive_keep_keys($prodotti1,$prodotti2);

    print_r($unisci);

    foreach(
    $unisci as $k => $v) {
        if (!
    is_array($v) )
        
    $finale[$k] = $v;
        else {
            
    $tot 0;
            foreach(
    $v as $value)
                
    $tot+=$value;
            
    $finale[$k] = $tot;
        }
    }

    ksort($finale);
    reset($finale);
    print_r($finale);
    ?>

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    158
    Grazzie MILLE!!!! Funziona perfettamente... ma è possibile che non ci sia un modo piu semplice???

  4. #4
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    Originariamente inviato da Yeye
    Grazzie MILLE!!!! Funziona perfettamente... ma è possibile che non ci sia un modo piu semplice???
    Sicuramente ci sarà. Finchè qualcuno non posta di meglio accontentati.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.