2010-05-01 14 views
9

In questo momento ho un array che ha una sorta di informazione e ho bisogno di creare una tabella da esso. per esempio.Ottieni la chiave dell'array in modo ricorsivo e crea una stringa separata da underscore

Student{ 
     [Address]{ 
       [StreetAddress] =>"Some Street" 
       [StreetName] => "Some Name" 
     } 
     [Marks1] => 100 
     [Marks2] => 50 
    } 

Ora voglio creare tabella di database come che contengono i campi nome:

Student_Address_StreetAddress 
Student_Address_StreetName 
Student_Marks1 
Student_Marks2 

dovrebbe essere ricorsiva in modo da qualsiasi profondità di gamma può creare la stringa nel mio formato.

+2

Potresti almeno ha fornito la matrice in formato PHP. –

+0

DFS è abbastanza facile da imparare: http://en.wikipedia.org/wiki/Depth-first_search – polygenelubricants

risposta

8

(Lavorando su di esso, qui è la matrice per risparmiare la fatica):

$arr = array 
(
    'Student' => array 
    (
     'Address' => array 
     (
      'StreetAddress' => 'Some Street', 
      'StreetName' => 'Some Name', 
     ), 
     'Marks1' => '100', 
     'Marks2' => '50', 
    ), 
); 

Eccolo, utilizza una versione modificata del codice @polygenelubricants:

function dfs($array, $parent = null) 
{ 
    static $result = array(); 

    if (is_array($array) * count($array) > 0) 
    { 
     foreach ($array as $key => $value) 
     { 
      dfs($value, $parent . '_' . $key); 
     } 
    } 

    else 
    { 
     $result[] = ltrim($parent, '_'); 
    } 

    return $result; 
} 

echo '<pre>'; 
print_r(dfs($arr)); 
echo '</pre>'; 

Uscite :

Array 
(
    [0] => Student_Address_StreetAddress 
    [1] => Student_Address_StreetName 
    [2] => Student_Marks1 
    [3] => Student_Marks2 
) 
+0

$ arr = array ( 'Student' => array ( 'Indirizzo' => array ( ' StreetAddress' => 'Alcuni Street', 'della via' => 'Alcuni Nome', ), 'Marks1' => array(), 'Marks2' => '50', ), ); risponderà al risultato indesiderato – Nehal

+0

@Nehal: L'ho aggiornato, provalo ora. –

1

Qualcosa come questo forse?

$schema = array(
    'Student' => array(
     'Address' => array(
      'StreetAddresss' => "Some Street", 
      'StreetName' => "Some Name", 
     ), 
     'Marks1' => 100, 
     'Marks2' => 50, 
    ), 
); 

$result = array(); 

function walk($value, $key, $memo = "") { 
    global $result; 
    if(is_array($value)) { 
     $memo .= $key . '_'; 
     array_walk($value, 'walk', $memo); 
    } else { 
     $result[] = $memo . $key; 
    } 
} 

array_walk($schema, 'walk'); 

var_dump($result); 

So che i globals sono cattivi, ma non riesco a pensare a qualcosa di meglio ora.

1

Qualcosa di simile a questo funziona:

<?php 

$arr = array (
    'Student' => array (
    'Address' => array (
     'StreetAddress' => 'Some Street', 
     'StreetName' => 'Some Name', 
    ), 
    'Marks1' => array(), 
    'Marks2' => '50', 
), 
); 

$result = array(); 

function dfs($data, $prefix = "") { 
    global $result; 

    if (is_array($data) && !empty($data)) { 
     foreach ($data as $key => $value) { 
     dfs($value, "{$prefix}_{$key}"); 
     } 
    } else { 
     $result[substr($prefix, 1)] = $data; 
    } 
} 

dfs($arr); 
var_dump($result); 

?> 

This prints:

array(4) { 
    ["Student_Address_StreetAddress"] => string(11) "Some Street" 
    ["Student_Address_StreetName"] => string(9) "Some Name" 
    ["Student_Marks1"] => array(0) {} 
    ["Student_Marks2"] => string(2) "50" 
} 
+0

+1, ho qualcosa di molto simile a questo (anche se non avevo idea che si chiamasse DFS), il problema che sto affrontando è di restituire l'array senza utilizzare globals o riferimenti. –

+0

$ arr = array ('Student' => array ('Address' => array ('StreetAddress' => 'Some Street', 'StreetName' => 'Some Name',), 'Marks1' => array() , 'Marks2' => '50',),); risposta risposta indesiderata – Nehal

+0

@Nehal: vedere l'ultima versione. – polygenelubricants

0
function getValues($dataArray,$strKey="") 
{ 
    global $arrFinalValues; 

    if(is_array($dataArray)) 
    { 
     $currentKey = $strKey; 
     foreach($dataArray as $key => $val) 
     { 
      if(is_array($val) && !empty($val)) 
      { 
       getValues($val,$currentKey.$key."_"); 
      } 
      else if(!empty($val)) 
      { 
       if(!empty($strKey)) 
        $strTmpKey = $strKey.$key; 
       else 
        $strTmpKey = $key; 
       $arrFinalValues[$strTmpKey]=$val; 
      } 
     } 
    } 
} 
16

È possibile utilizzare la RecursiveArrayIterator e il RecursiveIteratorIterator (per scorrere la matrice ricorsivamente) dal PHP Standard Library (SPL) per rendere questo lavoro relativamente indolore.

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); 
$keys = array(); 
foreach ($iterator as $key => $value) { 
    // Build long key name based on parent keys 
    for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) { 
     $key = $iterator->getSubIterator($i)->key() . '_' . $key; 
    } 
    $keys[] = $key; 
} 
var_export($keys); 

L'esempio sopra emette qualcosa di simile:

array (
    0 => 'Student_Address_StreetAddress', 
    1 => 'Student_Address_StreetName', 
    2 => 'Student_Marks1', 
    3 => 'Student_Marks2', 
) 
+0

Come modificheresti il ​​tuo codice per ottenere i nomi dei campi nell'ordine corretto, ad es.''Student_Address_StreetAddress'' per esempio? Grazie –

Problemi correlati