Rumah > pembangunan bahagian belakang > tutorial php > 各位大侠,通过php 读取字符串 求帮忙

各位大侠,通过php 读取字符串 求帮忙

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-23 14:00:48
asal
847 orang telah melayarinya

$str="谢姣
毕丹丹
马朝莉
李欣
李萍";
有这么一段字符串 通过php处理字符串,得到 一个新的数组(名字,count值);
比如生成 $ars=array('0'=>'谢姣','0'=>'毕丹丹','0'=>'马朝莉','0'=>'李欣','1'=>'李萍');

求各大侠帮帮忙,感激不尽


回复讨论(解决方案)

$str="<v:note id='33' count='0'>谢姣</v:note>		<v:note id='34' count='0'>毕丹丹</v:note>		<v:note id='35' count='0'>马朝莉</v:note>		<v:note id='36' count='0'>李欣</v:note>		<v:note id='37' count='1'>李萍</v:note>";	preg_match_all("/<v:note id=\'\d+\' count=\'(\d)\'>(.*?)<\/v:note>/", $str, $matches);	$newArr = array_combine($matches[2],$matches[1]);	var_dump($newArr);?>
Salin selepas log masuk
Salin selepas log masuk

php数组中不可能存在相同键值的 所以只能用名字做键值保证唯一

不可能生成你要的这种。相同的key是不可以同时存在的。后者会覆盖前者。

只能array('0'=>'谢姣','1'=>'毕丹丹','2'=>'马朝莉','3'=>'李欣','4'=>'李萍'); 类似这样。

你的count不能做key的

$str="<v:note id='33' count='0'>谢姣</v:note>		<v:note id='34' count='0'>毕丹丹</v:note>		<v:note id='35' count='0'>马朝莉</v:note>		<v:note id='36' count='0'>李欣</v:note>		<v:note id='37' count='1'>李萍</v:note>";	preg_match_all("/<v:note id=\'\d+\' count=\'(\d)\'>(.*?)<\/v:note>/", $str, $matches);	$newArr = array_combine($matches[2],$matches[1]);	var_dump($newArr);?>
Salin selepas log masuk
Salin selepas log masuk

php数组中不可能存在相同键值的 所以只能用名字做键值保证唯一  结果多了个string  array(5) {
  ["谢姣"]=>
  string(1) "0"
  ["毕丹丹"]=>
  string(1) "0"
  ["马朝莉"]=>
  string(1) "0"
  ["李欣"]=>
  string(1) "0"
  ["李萍"]=>
  string(1) "1"
}
 这个string 能去掉吗

不可能生成你要的这种。相同的key是不可以同时存在的。后者会覆盖前者。

只能array('0'=>'谢姣','1'=>'毕丹丹','2'=>'马朝莉','3'=>'李欣','4'=>'李萍'); 类似这样。

你的count不能做key的   我只要能头按照count 里面的值进行排序


没有string哪里的string 是你其他地方输出的吧


没有string哪里的string 是你其他地方输出的吧 谢谢你 给分

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan