2016-04-15 47 views
0

我經常必須從大數組中挑選某些值。雖然我理解我這樣做的方式可能不是絕對正確的方式,但在這個系統中,我應該這樣做。PHP將數組遞歸地導出爲'水平'代碼

實施例(簡化的)陣列:

$data = array (
    'api' => 
    array (
    'update_id' => 94594942, 
    'message' => 
    array (
     'message_id' => 141, 
     'from' => 
     array (
     'id' => 1251597, 
     'first_name' => 'Testuser', 
    ), 
     'chat' => 
     array (
     'id' => '124630', 
     'title' => 'TESTGROUP', 
     'type' => 'group', 
    ), 
     'date' => '1460712410', 
     'text' => 'tryout', 
     'valid' => true, 
    ), 
    'list' => 
    array (
     0 => 'one', 
     1 => 'two', 
     2 => 'three', 
    ), 
), 
); 

我所需的導出/轉儲視爲純字符串:

$result = " 
    data['api']['update_id'] = 94594942; 
    data['api']['message']['message_id'] = 141; 
    data['api']['message']['from']['id'] = 1251597; 
    data['api']['message']['from']['first_name'] = 'Testuser'; 
    data['api']['message']['chat']['id'] = '124630'; 
    data['api']['message']['chat']['title'] = 'TESTGROUP'; 
    data['api']['message']['chat']['type'] = 'group'; 
    data['api']['message']['date'] = '1460712410'; 
    data['api']['message']['text'] = 'tryout'; 
    data['api']['message']['valid'] = true; 
    data['api']['list'][] = 'one'; 
    data['api']['list'][] = 'two'; 
    data['api']['list'][] = 'three'; 
"; 

我稱它是 '水平' 數組表示 - 我不很清楚它的官方名稱是什麼。

我試着用json_encode()和var_export()轉換數組,並手動解析它,但它似乎不是正確的方式來做到這一點,並給我頭痛。有小費嗎?

+0

基本上你是想你多轉換夢詩離子數據記錄到單個記錄?您應該創建變換器類,將您的結構化數據映射到單維度鍵 - >值數組。 –

+0

我已更新第二代碼部分。我的結果應該是一個純文本字符串。我想你可以把它稱爲'var_export($ data,true);'用不同的模板。 – EDP

+1

你到底在做什麼?爲什麼首先需要出口?你稍後再導入它嗎? –

回答

1

您可以使用下面的功能:

<?php 
 
$data = array (
 
    'api' => 
 
    array (
 
    'update_id' => 94594942, 
 
    'message' => 
 
    array (
 
     'message_id' => 141, 
 
     'from' => 
 
     array (
 
     'id' => 1251597, 
 
     'first_name' => 'Testuser', 
 
    ), 
 
     'chat' => 
 
     array (
 
     'id' => '124630', 
 
     'title' => 'TESTGROUP', 
 
     'type' => 'group', 
 
    ), 
 
     'date' => '1460712410', 
 
     'text' => 'tryout', 
 
     'valid' => true, 
 
    ), 
 
    'list' => 
 
    array (
 
     0 => 'one', 
 
     1 => 'two', 
 
     2 => 'three', 
 
    ), 
 
), 
 
); 
 

 
function convertHorizontal($parent, $source) { 
 
    if (!is_array($source)) { 
 
     return "$parent = " . var_export($source, true) . "\n"; 
 
    } 
 
    
 
    $result = ''; 
 
    foreach($source as $key => $value) { 
 
     $result .= convertHorizontal("{$parent}[\"$key\"]", $value); 
 
    } 
 

 
    return $result; 
 
} 
 

 
$result = convertHorizontal("\$data", $data); 
 
print $result; 
 
$result = convertHorizontal("\$data", $data); 
 
print $result

,你會得到以下結果:

$data["api"]["update_id"] = 94594942 
 
$data["api"]["message"]["message_id"] = 141 
 
$data["api"]["message"]["from"]["id"] = 1251597 
 
$data["api"]["message"]["from"]["first_name"] = 'Testuser' 
 
$data["api"]["message"]["chat"]["id"] = '124630' 
 
$data["api"]["message"]["chat"]["title"] = 'TESTGROUP' 
 
$data["api"]["message"]["chat"]["type"] = 'group' 
 
$data["api"]["message"]["date"] = '1460712410' 
 
$data["api"]["message"]["text"] = 'tryout' 
 
$data["api"]["message"]["valid"] = true 
 
$data["api"]["list"]["0"] = 'one' 
 
$data["api"]["list"]["1"] = 'two' 
 
$data["api"]["list"]["2"] = 'three'

+0

正是我想要的!只有五行工作代碼相比,我自己的頁面長度的不可持續的垃圾。 – EDP