将数组内容拆分为另一个数组 php
split array contents into another array php
你好,我是 php 的新手...正在做作业,我们的任务是分离一个包含内容的数组。但技巧是分离内容并将其放入一个包含内容的新数组。
但是,我的新数组是错误的。一个索引应将所有名称包含在 1 个字符串中
另一个包含所有 phone 数字的索引 ...等等
我的显示和图片一样
有什么建议吗?附上代码图片
<pre>
<?php
$fileName = "c:/wamp/www/datebook";
$line = file($fileName);
print_r($line);
foreach($line as $value)
{
$newLine[] = explode(":",$value);
}
print_r($newLine);
?>
</pre>
这些是小块,总共 26 个..来自记事本
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
您需要将它们添加到自己的数组中。
$line = explode("\n", $s);
$newLine = array('name' => '','phone' => ''); // add the rest of the columns.....address,etc
foreach($line as $value)
{
list($name,$phone,$address,$date,$postcode) = explode(":",$value);
$newLine['name'] .= (empty($newLine['name'])? $name : " ". $name);
$newLine['phone'] .= (empty($newLine['phone'])? $phone : " ". $phone);
// etc
}
这将适当地添加它们。
Example(只需按ctrl
+ enter
到运行即可)
它 returns 是一个如下所示的关联数组:
Array
(
[0] => Array
(
[name] => Jon DeLoach
[phone] => 408-253-3122
[address] => 123 Park St., San Jose, CA 04086
)
[1] => Array
(
[name] => Sir Lancelot
[phone] => 837-835-8257
[address] => 474 Camelot Boulevard, Bath, WY 28356
)
[2] => Array
(
[name] => Jesse Neal
[phone] => 408-233-8971
[address] => 45 Rose Terrace, San Francisco, CA 92303
)
)
你可以试试这个 -
// The indexes to be set to new array [Currently I am assuming, You can change accordingly]
$indexes= array(
'Name' , 'Phone', 'Address', 'Date', 'Value'
);
$new = array();
// Loop through the indexes array
foreach($indexes as $key => $index) {
// extract column data & implode them with [,]
$new[$index] = implode(', ', array_column($newline, $key));
}
支持 array_column PHP >= 5.5
<?php
$fileName = "c:/wamp/www/datebook";
$line = file($fileName);
$newLine= array();
foreach($line as $va)
{
$new = explode(":",$va);
$newLine['name'][] = $new[0];
$newLine['phone'][] = $new[1];
$newLine['etc'][] = $new[2];
}
echo "<pre>";
print_r($newLine);
?>
这将输出
Array
(
[name] => Array
(
[0] => Jon DeLoach
[1] => Joo Del
)
[phone] => Array
(
[0] => 408-253-3122
[1] => 408-253-3122
)
[etc] => Array
(
[0] => 7/25/53
[1] => 7/25/53
)
)
你好,我是 php 的新手...正在做作业,我们的任务是分离一个包含内容的数组。但技巧是分离内容并将其放入一个包含内容的新数组。
但是,我的新数组是错误的。一个索引应将所有名称包含在 1 个字符串中 另一个包含所有 phone 数字的索引 ...等等
我的显示和图片一样
有什么建议吗?附上代码图片
<pre>
<?php
$fileName = "c:/wamp/www/datebook";
$line = file($fileName);
print_r($line);
foreach($line as $value)
{
$newLine[] = explode(":",$value);
}
print_r($newLine);
?>
</pre>
这些是小块,总共 26 个..来自记事本
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
您需要将它们添加到自己的数组中。
$line = explode("\n", $s);
$newLine = array('name' => '','phone' => ''); // add the rest of the columns.....address,etc
foreach($line as $value)
{
list($name,$phone,$address,$date,$postcode) = explode(":",$value);
$newLine['name'] .= (empty($newLine['name'])? $name : " ". $name);
$newLine['phone'] .= (empty($newLine['phone'])? $phone : " ". $phone);
// etc
}
这将适当地添加它们。
Example(只需按ctrl
+ enter
到运行即可)
它 returns 是一个如下所示的关联数组:
Array
(
[0] => Array
(
[name] => Jon DeLoach
[phone] => 408-253-3122
[address] => 123 Park St., San Jose, CA 04086
)
[1] => Array
(
[name] => Sir Lancelot
[phone] => 837-835-8257
[address] => 474 Camelot Boulevard, Bath, WY 28356
)
[2] => Array
(
[name] => Jesse Neal
[phone] => 408-233-8971
[address] => 45 Rose Terrace, San Francisco, CA 92303
)
)
你可以试试这个 -
// The indexes to be set to new array [Currently I am assuming, You can change accordingly]
$indexes= array(
'Name' , 'Phone', 'Address', 'Date', 'Value'
);
$new = array();
// Loop through the indexes array
foreach($indexes as $key => $index) {
// extract column data & implode them with [,]
$new[$index] = implode(', ', array_column($newline, $key));
}
支持 array_column PHP >= 5.5
<?php
$fileName = "c:/wamp/www/datebook";
$line = file($fileName);
$newLine= array();
foreach($line as $va)
{
$new = explode(":",$va);
$newLine['name'][] = $new[0];
$newLine['phone'][] = $new[1];
$newLine['etc'][] = $new[2];
}
echo "<pre>";
print_r($newLine);
?>
这将输出
Array
(
[name] => Array
(
[0] => Jon DeLoach
[1] => Joo Del
)
[phone] => Array
(
[0] => 408-253-3122
[1] => 408-253-3122
)
[etc] => Array
(
[0] => 7/25/53
[1] => 7/25/53
)
)