PHP递归地将数组导出到水平'码

时间:2016-04-15 09:57:16

标签: php arrays multidimensional-array

我经常不得不从大数组中选择某些值。虽然我理解我这样做的方式可能不是绝对正确的方法,但在这个系统中我应该这样做。

示例(简化)数组:

$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()转换数组,然后手动解析它,但它似乎不是正确的方法,让我感到头痛。有什么提示吗?

1 个答案:

答案 0 :(得分: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'