2017-03-15 272 views
3

我有一个$string在索引数组PHP关联数组

'Name Height Weight 
John 177  142 
Jill 156  123 
Jacob 183  157' 

而且我把它分为以下结构的$array

Array (
    [0] => Array (
     ['Name'] => 'John' 
     ['Height'] => '177' 
     ['Weight'] => '142' 
    ) 
    [1] = > Array (
     ['Name'] => 'Jill' 
     ['Height'] => '156' 
     ['Weight'] => '123' 
    ) 
    [2] = > Array (
     ['Name'] => 'Jacob' 
     ['Height'] => '183' 
     ['Weight'] => '157' 
    ) 
) 

使用下面的代码:

$rows = explode("\n",$string); //creates an indexed array of rows as strings 
$headers = explode("\t",$rows[0]); //creates an indexed array of headers as strings 
$rows = array_slice($rows,1); //removes headers from $rows 
$array = Array(); 
foreach($rows as $row) { 
    $array[] = array_combine($headers, explode("\t",$row)); //creates associative arrays for each row 
} 

但是,我无法访问索引内部的关联数组

例如,这不起作用:

echo $array[0]['Name']; 

即使echo implode(', ', array_keys($array[0]));给出:

Name, Height, Weight 

我试图访问索引数组内的关联数组的许多不同的方式,但没有运气。我究竟做错了什么?

编辑:

所以,

$string = "Name Height Weight 
John 177 142 
Jill 156 123 
Jacob‌​ 183 157"; 

不工作,但

$string = "Name\tHeight\tWeight\nJohn\t177\t142\nJill\t156\t123\nJacob‌​‌​\t183\t157"; 

呢......

,所以我想的问题是:什么是区别?而我将如何解释前者的字符串为后者?

+0

'print_r($ array)''?你看到了什么? –

+0

Ermm nope,你的代码生成的数组没有这种格式 –

+0

@u_mulder是的,我看到上面提到的数组结构(不包括单引号) – jeffbeene

回答

1

您的代码不会产生阵列结构,但它可以固定这样的:

$string = 'Name Height Weight 
John 177  142 
Jill 156  123 
Jacob 183  157'; 

$rows = explode("\n",$string); //creates an indexed array of rows as strings 

$headers = preg_split("#\s+#",trim($rows[0], "\n\r\t ")); //creates an indexed array of headers as strings, by splitting by any white space 
var_dump($headers); 

$rows = array_slice($rows,1); //removes headers from $rows 
$array = Array(); 
foreach($rows as $row) { 
    $array[] = array_combine($headers, preg_split("#\s+#",trim($row, "\n\r\t "))); //creates associative arrays for each row, by splitting by any white space 
} 

var_dump($array); 

这将产生输出:

array(3) { 
    [0]=> 
    string(4) "Name" 
    [1]=> 
    string(6) "Height" 
    [2]=> 
    string(6) "Weight" 
} 
array(3) { 
    [0]=> 
    array(3) { 
    ["Name"]=> 
    string(4) "John" 
    ["Height"]=> 
    string(3) "177" 
    ["Weight"]=> 
    string(3) "142" 
    } 
    [1]=> 
    array(3) { 
    ["Name"]=> 
    string(4) "Jill" 
    ["Height"]=> 
    string(3) "156" 
    ["Weight"]=> 
    string(3) "123" 
    } 
    [2]=> 
    array(3) { 
    ["Name"]=> 
    string(5) "Jacob" 
    ["Height"]=> 
    string(3) "183" 
    ["Weight"]=> 
    string(3) "157" 
    } 
} 

主要思想是,你必须通过修剪埃维行字符串任何额外的空格并按最长的空白序列分割。

+0

谢谢。给我一分钟,把我的头围绕在这。与此同时,任何为什么print_r(),var_dump()和implode(',',array_keys($ array [0]))都是暗示数组结构是正确的? – jeffbeene

+0

好吧,为什么preg_split()是explode()的更好替代方法?不幸的是,我使用的真正的键有空格,所以这不适用于我:/ – jeffbeene

+0

我选择了正则表达式拆分,因为输入字符串可能会有所不同。如果它不变,并且始终具有完全相同的结构(即只有一个TAB字符),那么在您的情况下不是更好的选择。 –

0
//Use following code it will work as your answer 
$string='Name Height Weight 
John 177  142 
Jill 156  123 
Jacob 183  157'; 

$rows = explode("\n",$string); //creates an indexed array of rows as strings 

$headers = explode("\t",$rows[0]); //creates an indexed array of headers as strings 

$headers=array_filter(explode(" ",$headers[0])); ///t convert to space and filter remove empty element 

$rows = array_slice($rows,1); //removes headers from $rows 

$array = Array(); 
foreach($rows as $row) { 
    $row=explode("\t",$row); 
    $row=array_filter(explode(" ",$row[0])); 

    $array[] = array_combine($headers,$row); //creates associative arrays for each row 
} 

//print_r($array); 
echo $array[0]['Name']; 
+0

谢谢。现在,如果我想要数组键中的空格呢?例如:“Person's Name”而不是“Name” – jeffbeene

+0

如果您想使用该类型的标题[“Person's Name”],那么逻辑从一开始就是错误的。我们需要为该爆炸使用不同的逻辑\ n将为您提供单行空间单独的字符串。 –

+0

好的,谢谢。看来我的问题是在字符串中没有'\ t',但我不确定如何用'\ t'替换标签,如果这样做有意义的话。请参阅编辑我的问题。 – jeffbeene