我的网络API正在返回
"timestamp","full_message"
"2017-09-06T21:54:00.853Z","Device 192.168.1.1 is unstable"
"2017-09-06T21:54:01.069Z","Device 192.168.22.20 is unstable"
"2017-09-06T21:54:01.069Z","Device 192.168.22.18 is unstable"
我想把它传递给看起来像这样的数组
["Device 192.168.1.1 is unstable","Device 192.168.22.20 is unstable","Device 192.168.22.18 is unstable"]
想要除掉设备信息以外的大部分内容。 API来自graylog,因此无法控制输出。
答案 0 :(得分:1)
正如其他人已经指出的那样,您可以使用fopen()
和fgetcsv()
来实施更清晰,更可靠的解决方案。
无论如何......使用file_get_contents()
的可能实现可能是:
// This is your data
$data = file_get_contents(...)
// Split $data by "\n"
$lines = explode("\n", $data);
// Get rid of first element of array, which will always (?) be "timestamp","full_message"
array_shift($lines);
// Prepare variable $output
$output = Array();
// Fill $output array with the information you are looking for
foreach ($lines as $line) // for each $line in array $lines
{
$tokens = explode(",", $line); // split $line by "," and store array result in $tokens
$output[] = $tokens[1]; // push the second element of $tokens in $output
}
// Done, now $output is your array
假设您的输入数据始终与您发布的示例相似。否则,请考虑对输入进行更多控制。