0

I regularly have to pick certain values out of a large array. While I understand my way of doing this is probably not the absolute right way, but in this system it is the way I should do it.

Example (simplified) 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',
    ),
  ),
);

My desired export/dump as a plain string:

$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';
 ";

I'm calling it a 'horizontal' array representation - I don't quite know what's the official name for it.

I've tried to convert the array with json_encode() and var_export(), and manually parse it, but it doesn't seem to be the right way to do it and gives me headaches. Any tips?

5
  • Basically you are trying to convert your multi-dimensional data to a single record? You should create transformer class which maps your structured data to a single dimension key->value array. Commented Apr 15, 2016 at 9:59
  • I've updated the 2nd code part. My result should be a plain text string. I guess you could call it a var_export($data, true); with a different template. Commented Apr 15, 2016 at 10:04
  • 1
    What is it exactly you are trying to do? Why is the exporting required in the first place? Do you import it later? Commented Apr 15, 2016 at 10:06
  • Why not simply json_encode/json_decode? Commented Apr 15, 2016 at 10:14
  • @LauriOrgla 5,000 lines of deeply multi dimensional data where I just need to take out 3, to hardcode them into a simple script. Anyway, accepted answer below is exactly what I needed. Commented Apr 15, 2016 at 10:23

1 Answer 1

1

You can use following function:

<?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

And you get following 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'

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I wanted! Just five lines of working code compared with my own page length of unsustainable rubbish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.