1

I am getting started with php, and I was wondering if it was possible to explode a string into an array with named variables. The set up is that I have some data I am reading in from a text file, and I want to break it up into rows first and then break it up into individual pieces of data.

 Data1  |  Data2  |  Data3  |
 ----------------------------
|   x   |    y    |    z    |
|   p   |    q    |    r    |

So I am trying to end up with something like:

data {
   row1 {
       data1: x
       data2: y
       data3: z
   row2 {
       data1: p
       data2: q
       data3: r
   }
}

and I would like be able to access the data using the names of the variables if possible:

$r1d1 = data[row1]['data1'];

4
  • 2
    look into associative arrays. Commented Mar 1, 2012 at 18:03
  • there is no built in function but you can do it manually. What have you done so far? Commented Mar 1, 2012 at 18:06
  • What is the exact structure of your data in the file? You've got some options, such as storing the data in already parsable way, like JSON / XML, or parse it with regular expressions and explodes Commented Mar 1, 2012 at 18:35
  • @Col.Shrapnel There is a built in function named extract Commented Mar 1, 2012 at 18:44

3 Answers 3

3

If you want to explode a string into an associative array, you can use the list function.

// Initialize data_list
$data_list = array();

// Remove delimiter at start and end of string
$string = trim('|   x   |    y    |    z    |', '|');

$data = array();
list($data['data1'],$data['data2'],$data['data3']) = explode('|',$string);

$data_list[] = $data;

You would want to have it wrapped into a foreach loop to process each line of the file. In the end the $data_list would contain all the data.

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

Comments

0

Explanation by code

<?php

// data to convert
$string = '| Data1  |  Data2  |  Data3  |
 ----------------------------
|   x   |    y    |    z    |
|   p   |    q    |    r    |';

// container to collect data in
$data = array();

// split the string into lines
$lines = explode("\n", $string);
// pull first line from the array
$names = array_shift($lines);
// remove delimiters from beginning and end
$names = trim($names, '| ');
// split at | while ignoring spaces and empty results
$names = preg_split('/\s*\|\s*/', $names);
// remove --------------- line
array_shift($lines);
// walk remaining lines
foreach ($lines as $line) {
    // container to collect data of row in
    $row = array();
    // remove delimiters from beginning and end
    $line = trim($line, '| ');
    // split at |
    $line = explode('|', $line);
    foreach ($line as $i => $value) {
        // identify key by looking up in $names
        $key = $names[$i];
        // remove spaces
        $row[$key] = trim($value);
    }
    // add row to data set
    $data[] = $row;
}

var_dump($data);

will result in

$data = array(
    0 => array(
        'Data1' => 'x',
        'Data2' => 'y',
        'Data3' => 'z',
    ),
    1 => array(
        'Data1' => 'p',
        'Data2' => 'q',
        'Data3' => 'r',
    ),
);

Comments

-1

You can extract them PHP Extract()

extract($your_array, EXTR_PREFIX_ALL, 'prefix_if_needed');

then use

    echo '<pre>'; 
      var_export(array_diff(get_defined_vars(), array(array())));  
    echo'</pre>'; 

To see your new variable names ;)

Hope this helps.

Comments

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.