Comparer 2 tableaux PHP aider

  • damien41630
  • Born
  • Born
  • No Avatar
  • Inscription: Mai 29, 2009
  • Messages: 4
  • Status: Offline

Message Juin 3rd, 2009, 12:55 pm

J'ai l'deux matrices suivantes, contracta $ et $ contractb. J'ai besoin d'un moyen de comparer les deux tableaux et return $ contractc. Fondamentalement, si un numéro de téléphone de cellules existent dans $ contractb j'ai besoin de prendre cette inscription et de remplacer l'entrée en $ contracta donnant une sortie comme $ contractc. J'ai essayé les fonctions intégrées, mais ils comparent chaque valeur me donnant ainsi les doubles entrées pour un seul numéro.


Code: [ Select ]
$contracta
Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
    [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
    [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
    [4] => Array ( [Cell Number] => 6067913446 [Contract Date] => N/A [Renew] =>add)
    [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
    [6] => Array ( [Cell Number] => 6067917882 [Contract Date] => N/A [Renew] =>add )
    [7] => Array ( [Cell Number] => 6067919042 [Contract Date] => N/A[Renew] =>add) )
  1. $contracta
  2. Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
  3.     [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
  4.     [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
  5.     [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
  6.     [4] => Array ( [Cell Number] => 6067913446 [Contract Date] => N/A [Renew] =>add)
  7.     [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
  8.     [6] => Array ( [Cell Number] => 6067917882 [Contract Date] => N/A [Renew] =>add )
  9.     [7] => Array ( [Cell Number] => 6067919042 [Contract Date] => N/A[Renew] =>add) )


Code: [ Select ]
$contractb
Array ( [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew] => N/A)
    [1] => Array ( [Cell Number] => 6067917882 [Contract Date] =>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew] =>add) )
  1. $contractb
  2. Array ( [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew] => N/A)
  3.     [1] => Array ( [Cell Number] => 6067917882 [Contract Date] =>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew] =>add) )



Code: [ Select ]
$contractc
Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
    [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
    [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
    [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew]=> N/A)
            [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067917882 [Contract Date]=>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew]=>add) )
  1. $contractc
  2. Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
  3.     [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
  4.     [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
  5.     [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
  6.     [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew]=> N/A)
  7.             [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
  8.     [1] => Array ( [Cell Number] => 6067917882 [Contract Date]=>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew]=>add) )
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Juin 3rd, 2009, 12:55 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13458
  • Loc: Florida
  • Status: Offline

Message Juin 3rd, 2009, 1:47 pm

Je voudrais une boucle sur un tableau et remplacer les indices numériques avec des indices qui utilisent le numéro de téléphone.

Code: [ Select ]
foreach($contracta as $key => $val)
{
    $contracta["_{$val['Cell Number']}"] = $val;
    unset($contracta[$key]);
}
  1. foreach($contracta as $key => $val)
  2. {
  3.     $contracta["_{$val['Cell Number']}"] = $val;
  4.     unset($contracta[$key]);
  5. }


Ensuite, je boucle sur contractb $ $ contracta et mettre à jour à chaque itération.

Code: [ Select ]
foreach($contractb as $key => $val)
{
    $_key = "_{$val['Cell Number']}";
    if(empty($contracta[$_key]))
    {
        $contracta[$_key] = $val;
    }
    else
    {
        foreach($val as $__key => $__val)
        {
            $contracta[$_key][$__key] = $__val;
        }
    }
}
  1. foreach($contractb as $key => $val)
  2. {
  3.     $_key = "_{$val['Cell Number']}";
  4.     if(empty($contracta[$_key]))
  5.     {
  6.         $contracta[$_key] = $val;
  7.     }
  8.     else
  9.     {
  10.         foreach($val as $__key => $__val)
  11.         {
  12.             $contracta[$_key][$__key] = $__val;
  13.         }
  14.     }
  15. }
Strong with this one, the sudo is.
  • damien41630
  • Born
  • Born
  • No Avatar
  • Inscription: Mai 29, 2009
  • Messages: 4
  • Status: Offline

Message Juin 4th, 2009, 6:55 am

Cela semble fonctionne, je vais faire quelques tests supplémentaires aujourd'hui. Mon autre question que devient cette imprimées dans une table une fois que j'ai mes valeurs final. J'étais en utilisant cette fonction auparavant, mais il ne fonctionne plus.

Code: [ Select ]
function array2table($array, $recursive = false, $null = ' ')
{

    // Sanity check
    if (empty($array) || !is_array($array)) {
        return false;
    }

    if (!isset ($array[0]) || !is_array ($array[0])) {
        $array =($array[0]);
    }

    // Start the table
    $table = "<table>\n";

    // The header
    $table .= "\t<tr>";
    // Take the keys from the first row as the headings
    foreach (array_keys($array[0]) as $heading) {
        $table .= '<th>' . $heading . '</th>';
    }
    $table .= "</tr>\n";

    // The body
    foreach ($array as $row) {
        $table .= "\t<tr>" ;
        foreach ($row as $cell) {
            $table .= '<td>';

            // Cast objects
            if (is_object($cell)) { $cell = $cell; }
                    
            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . array2table($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                 // htmlspecialchars((string) $cell) :
                    $cell :
                    $null;
            }

            $table .= '</td>';
        }

        $table .= "</tr>\n";
    }

    $table .= '</table>';
    $cc['table'] = $table;
    return $table;
}
  1. function array2table($array, $recursive = false, $null = '&nbsp;')
  2. {
  3.     // Sanity check
  4.     if (empty($array) || !is_array($array)) {
  5.         return false;
  6.     }
  7.     if (!isset ($array[0]) || !is_array ($array[0])) {
  8.         $array =($array[0]);
  9.     }
  10.     // Start the table
  11.     $table = "<table>\n";
  12.     // The header
  13.     $table .= "\t<tr>";
  14.     // Take the keys from the first row as the headings
  15.     foreach (array_keys($array[0]) as $heading) {
  16.         $table .= '<th>' . $heading . '</th>';
  17.     }
  18.     $table .= "</tr>\n";
  19.     // The body
  20.     foreach ($array as $row) {
  21.         $table .= "\t<tr>" ;
  22.         foreach ($row as $cell) {
  23.             $table .= '<td>';
  24.             // Cast objects
  25.             if (is_object($cell)) { $cell = $cell; }
  26.                     
  27.             if ($recursive === true && is_array($cell) && !empty($cell)) {
  28.                 // Recursive mode
  29.                 $table .= "\n" . array2table($cell, true, true) . "\n";
  30.             } else {
  31.                 $table .= (strlen($cell) > 0) ?
  32.                  // htmlspecialchars((string) $cell) :
  33.                     $cell :
  34.                     $null;
  35.             }
  36.             $table .= '</td>';
  37.         }
  38.         $table .= "</tr>\n";
  39.     }
  40.     $table .= '</table>';
  41.     $cc['table'] = $table;
  42.     return $table;
  43. }
Moderator Remark: Added [code] tags

Afficher de l'information

  • Total des messages de ce sujet: 3 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 158 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC