Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中分解为命名变量数组?_Php_Arrays_Multidimensional Array_Explode - Fatal编程技术网

如何在php中分解为命名变量数组?

如何在php中分解为命名变量数组?,php,arrays,multidimensional-array,explode,Php,Arrays,Multidimensional Array,Explode,我刚开始学习php,我想知道是否有可能将字符串分解成带有命名变量的数组。设置是,我有一些从文本文件中读取的数据,我想先将其分解为行,然后再将其分解为单独的数据段 Data1 | Data2 | Data3 | ---------------------------- | x | y | z | | p | q | r | 因此,我试图以如下方式结束: data { row1 { data1: x

我刚开始学习php,我想知道是否有可能将字符串分解成带有命名变量的数组。设置是,我有一些从文本文件中读取的数据,我想先将其分解为行,然后再将其分解为单独的数据段

 Data1  |  Data2  |  Data3  |
 ----------------------------
|   x   |    y    |    z    |
|   p   |    q    |    r    |
因此,我试图以如下方式结束:

data {
   row1 {
       data1: x
       data2: y
       data3: z
   row2 {
       data1: p
       data2: q
       data3: r
   }
}
如果可能,我希望能够使用变量名称访问数据:

$r1d1=数据[row1]['data1']

按代码解释

<?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);
你可以提取它们

extract($your_数组,EXTR_PREFIX_ALL,'PREFIX_if_needed')

然后使用

    echo '<pre>'; 
      var_export(array_diff(get_defined_vars(), array(array())));  
    echo'</pre>'; 
echo';
// 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;
变量导出(数组差异(获取定义的变量(),数组(数组())); 回声';
要查看新的变量名;)


希望这有帮助。

如果要将字符串分解为关联数组,可以使用list函数


您可能希望将其包装到foreach循环中,以处理文件的每一行。最后,$data_列表将包含所有数据。

查看关联数组。没有内置函数,但可以手动执行。到目前为止您做了什么?文件中数据的确切结构是什么?您有一些选择,比如以已经可以解析的方式存储数据,比如JSON/XML,或者使用正则表达式和explodes@Col.Shrapnel有一个名为extract的内置函数
// 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;