2016-08-22 1 views
-2

Gibt es eine Möglichkeit für den Array_Walk_recursive, den Namen des Arrays anstelle von index zurückzugeben?array_walk_recursive zum Zurückgeben des Array-Namens anstelle der Indexnummer

function flatten(array $array) { 
    $return = array(); 
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; }); 
    return $return; 
} 

mein Ergebnis ist so etwas wie dieses

Array 
(
    [0] => 888 
    [1] => TY7452 
    [2] => 63214 
    [3] => 0 
    [4] => Upper 
) 

ich hoffe, können die Indizes in den Array-Namen wie die erste sollte Namen zweite sollte Zahl dann ändern. Und Leute noch eine Frage ist, nachdem ich in der Lage war, den Namen herauszuziehen, ist es möglich für mich, implode zu einer Art wie einen Pfad des Array-Namen zu setzen, der die Indexnummer ersetzen wird, die ich gerade bekomme? zB car.model.number

mein Array

$trading = [ 
    'id' => 888, 
    'case_number' => 'KO2017-987', 
    'property' => [ 
     'id' => 78563, 
     'propertyType' => [ 
      'id' => 1, 
      'name' => 'Residential' 
     ], 
     'address' => [ 
      'block' => '85', 
      'street' => 'Jalan Serjana', 
      'subpremise' => '#07-05', 
      'building' => 'TM Block', 
      'country_code' => 'MY' 
     ], 
     'askingPrice' => '650000.00', 
     'photos' => [ 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg', 
       'is_default' => 1 
      ], 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg', 
       'is_default' => 0 
      ], 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg', 
       'is_default' => 0 
      ] 
     ] 
    ], 
]; 

Gewünschtes Ergebnis

Array

(
    [id] => 888 
    [case_number] => KO2017-987 
    [property.id] => 78563 
    [property.propertyType.id] => 1 
    [property.propertyType.name] => Residential 
    [property.address.block] => 85 
    [property.address.street] => Jalan Serjana 
    [property.address.subpremise] => #07-05 
    [property.address,building] => TM Block 
    [property.address.country_code] => MY 
    [property.askingPrice] => 6500000.00 
    [property.photos.0.url] => https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg 
    [property.photos.o.is_default] => 1 
    [property.photos.1.url] => https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg 
    [property.photos.1.is_default] => 0 
    [property.photos.2.url] => https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg 
    [property.photos.2.is_default] => 0 
) 
+3

Ihre Frage ist wirklich hart verstehen. Was ist ein "Array-Name"? Können Sie ein Beispiel für ein Array angeben, das Sie an "flatten" und das gewünschte Ergebnis übergeben möchten? – smarx

+2

^Eingabefeld und gewünschter Ausgang Array erforderlich –

+0

in Ordnung so etwas wie dieser [ $ Handel = [ 'id' =>, 'case_number' => 'TX2015-0123', 'Eigentum' => [ ' ID '=> 69134, ' Eigenschaftstyp '=> [ ' ID '=> 1, ' Name '=>' Wohn ' ] ] ]; –

Antwort

0

Geben Sie diesem einen Versuch:

function flatten(array $array, $prefix="") { 
    $result = Array(); 
    array_walk($array, function ($value, $key) use ($array, $prefix, &$result) { 
     $path = $prefix ? "$prefix.$key" : $key; 
     if (is_array($value)) { 
      $result = array_merge($result, flatten($value, $path)); 
     } else { 
      $result[$path] = $value; 
     } 
    }); 

    return $result; 
} 

$trading = [ 
    'id' => 888, 
    'case_number' => 'KO2017-987', 
    'property' => [ 
     'id' => 78563, 
     'propertyType' => [ 
      'id' => 1, 
      'name' => 'Residential' 
     ], 
     'address' => [ 
      'block' => '85', 
      'street' => 'Jalan Serjana', 
      'subpremise' => '#07-05', 
      'building' => 'TM Block', 
      'country_code' => 'MY' 
     ], 
     'askingPrice' => '650000.00', 
     'photos' => [ 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg', 
       'is_default' => 1 
      ], 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg', 
       'is_default' => 0 
      ], 
      [ 
       'url' => 'https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg', 
       'is_default' => 0 
      ] 
     ] 
    ], 
]; 

print_r(flatten($trading)); 

// Output: 

// Array 
// (
//  [id] => 888 
//  [case_number] => KO2017-987 
//  [property.id] => 78563 
//  [property.propertyType.id] => 1 
//  [property.propertyType.name] => Residential 
//  [property.address.block] => 85 
//  [property.address.street] => Jalan Serjana 
//  [property.address.subpremise] => #07-05 
//  [property.address.building] => TM Block 
//  [property.address.country_code] => MY 
//  [property.askingPrice] => 650000.00 
//  [property.photos.0.url] => https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg 
//  [property.photos.0.is_default] => 1 
//  [property.photos.1.url] => https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg 
//  [property.photos.1.is_default] => 0 
//  [property.photos.2.url] => https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg 
//  [property.photos.2.is_default] => 0 
//) 
+0

Danke mann das ist genial –

Verwandte Themen