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'];

share|improve this question

2  
look into associative arrays. – Matt K Mar 1 at 18:03
there is no built in function but you can do it manually. What have you done so far? – Your Common Sense Mar 1 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 – Alex Mar 1 at 18:35
@Col.Shrapnel There is a built in function named extract – ehime Mar 1 at 18:44
feedback

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
feedback

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.

share|improve this answer
feedback

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',
    ),
);
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.