我的问题可能没有意义,但在尝试各种array_chunk和爆炸后,我似乎找不到一个简单的方法来解决我的问题。
最终,我有一个textarea,我希望能够输入这样的数据:
<textarea>
Song 1 by Artist 1
Song 2 by Artist 2
Song 3 by Artist 1
Song 4 by Artist 3
Song 5 by Artist 3
</textarea>
我想最终创建一个我可以过滤和循环的数组,并获取每个歌曲标题和艺术家标题,并拥有一个嵌套数组。
到目前为止,我可以使用explode( "\n", $source)
创建一个简单的数组:
array (size=5)
0 => string 'Song 1 by Artist 1' (length=19)
1 => string 'Song 2 by Artist 2' (length=19)
2 => string 'Song 3 by Artist 1' (length=19)
3 => string 'Song 4 by Artist 3' (length=19)
4 => string 'Song 5 by Artist 3' (length=18)
但是我想现在为每个歌曲标题和艺术家标题进一步创建一个数组,所以它看起来像:
array (
0 => array(
'title' => 'Song 1',
'artist' => 'Artist 1
),
1 => array(
'title' => 'Song 2',
'artist' => 'Artist 2
)
etc.
如何扩展初始爆炸功能,以便能够将最终数组值作为列表循环出来?
答案 0 :(得分:2)
这里我使用PHP中的函数来提取数据
$arrays = [
"Song 1 by Artist 1",
"Song 2 by Artist 2",
"Song 3 by Artist 1",
"Song 4 by Artist 3",
"Song 5 by Artist 3",
];
$result = [];
array_walk($arrays, function ($data) use (&$result) {
$fields = explode(' by ', $data);
$result[] = [
"title" => $fields[0],
"artist" => $fields[1],
];
});
print_r($result);
答案 1 :(得分:1)
$songList = explode( "\n", $source);
foreach($songList as &$value) {
$interim = explode(" by ", $value);
$value = ['title' => $interim[0], 'artist' => $interim[1]];
}
答案 2 :(得分:1)
然后您需要按$row = explode(' by ', $array)
拆分代码
$mainArray = explode("\n", $text);
$list = [];
foreach ($mainArray as $oneRow) {
$row = explode(' by ', $oneRow);
$list[] = [
'artist' => $row[1],
'title' => $row[0]
];
}
答案 3 :(得分:1)
你去吧
<ImageView
android:id="@+id/img"
android:src="@drawable/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>