i have a small problem with the explode function. I have a string like this:
我有一个爆炸功能的小问题。我有一个像这样的字符串:
$response:"online,ksksuems,3428939,670605083faeb7750e1afc1010f0f66f8ef0025a,File1.zip
offline,iwksksiw,,, offline,kdlsiwie,,, offline,jdmsmwus,,,
online,uekseks,4023702,37d97c816afdfb10857057d870e74e8774e2bf8a,File2.zip
online,jwksjwa,8860421,20b5e3154653f24963d005cd873917d3cc0a0fe2,File3.rar
online,jsusneus,4912753,9489a47bac4d2a4f7f6810cb37f60924ef48fc48,File4.rar
online,udjdjsis,1177526,5d1da2a1aebae206908ef6d88105f5272ab423e0,File5.zip"
Now I wanted to use the explode function:
现在我想使用explode函数:
list($fileStatus, $fileId, $fileSize, $fileSha1, $fileName) = explode(",", $response);
But I will only get 1 response, if I print the content of $fileStatus. My Question now, how can i get an array for each variable? So that i have "array(ksksuems => online, iwksksiw => offline);"
?
但是如果我打印$ fileStatus的内容,我只会得到1个响应。我的问题现在,我怎样才能得到每个变量的数组?所以我有“阵列(ksksuems =>在线,iwksksiw =>离线);” ?
2 个解决方案
#1
0
This should work -
这应该工作 -
$arr = Array();
$lines = explode("\r\n", $response);
//print the exploded lines here.
var_dump($lines);
/*
Expected output -
array
0 => string 'online,ksksuems,3428939,670605083faeb7750e1afc1010f0f66f8ef0025a,File1.zip' (length=74)
1 => string 'offline,iwksksiw,,, offline,kdlsiwie,,, offline,jdmsmwus,,,' (length=59)
2 => string 'online,uekseks,4023702,37d97c816afdfb10857057d870e74e8774e2bf8a,File2.zip' (length=73)
3 => string 'online,jwksjwa,8860421,20b5e3154653f24963d005cd873917d3cc0a0fe2,File3.rar' (length=73)
4 => string 'online,jsusneus,4912753,9489a47bac4d2a4f7f6810cb37f60924ef48fc48,File4.rar' (length=74)
5 => string 'online,udjdjsis,1177526,5d1da2a1aebae206908ef6d88105f5272ab423e0,File5.zip' (length=74)
*/
foreach($lines as $line){
list($fileStatus, $fileId, $fileSize, $fileSha1, $fileName) = explode(",", $line);
$arr[$fileId] = $fileStatus;
}
var_dump($arr);
/*
OUTPUT-
array
'ksksuems' => string 'online' (length=6)
'iwksksiw' => string 'offline' (length=7)
'uekseks' => string 'online' (length=6)
'jwksjwa' => string 'online' (length=6)
'jsusneus' => string 'online' (length=6)
'udjdjsis' => string 'online' (length=6)
*/
#2
1
You need to use explode()
on your response to put the individual responses into an array and then loop through it to get each other values.
您需要在响应中使用explode()将各个响应放入一个数组中,然后遍历它以获取其他值。
Assuming a the new line character of \n
as the separator:
假设\ n的新行字符为分隔符:
$responses = explode("\n", $response);
foreach ($responses as $resp) {
list($fileStatus, $fileId, $fileSize, $fileSha1, $fileName) = explode(",", $resp);
// do stuff
}
#1
0
This should work -
这应该工作 -
$arr = Array();
$lines = explode("\r\n", $response);
//print the exploded lines here.
var_dump($lines);
/*
Expected output -
array
0 => string 'online,ksksuems,3428939,670605083faeb7750e1afc1010f0f66f8ef0025a,File1.zip' (length=74)
1 => string 'offline,iwksksiw,,, offline,kdlsiwie,,, offline,jdmsmwus,,,' (length=59)
2 => string 'online,uekseks,4023702,37d97c816afdfb10857057d870e74e8774e2bf8a,File2.zip' (length=73)
3 => string 'online,jwksjwa,8860421,20b5e3154653f24963d005cd873917d3cc0a0fe2,File3.rar' (length=73)
4 => string 'online,jsusneus,4912753,9489a47bac4d2a4f7f6810cb37f60924ef48fc48,File4.rar' (length=74)
5 => string 'online,udjdjsis,1177526,5d1da2a1aebae206908ef6d88105f5272ab423e0,File5.zip' (length=74)
*/
foreach($lines as $line){
list($fileStatus, $fileId, $fileSize, $fileSha1, $fileName) = explode(",", $line);
$arr[$fileId] = $fileStatus;
}
var_dump($arr);
/*
OUTPUT-
array
'ksksuems' => string 'online' (length=6)
'iwksksiw' => string 'offline' (length=7)
'uekseks' => string 'online' (length=6)
'jwksjwa' => string 'online' (length=6)
'jsusneus' => string 'online' (length=6)
'udjdjsis' => string 'online' (length=6)
*/
#2
1
You need to use explode()
on your response to put the individual responses into an array and then loop through it to get each other values.
您需要在响应中使用explode()将各个响应放入一个数组中,然后遍历它以获取其他值。
Assuming a the new line character of \n
as the separator:
假设\ n的新行字符为分隔符:
$responses = explode("\n", $response);
foreach ($responses as $resp) {
list($fileStatus, $fileId, $fileSize, $fileSha1, $fileName) = explode(",", $resp);
// do stuff
}