PHP关联数组,正则表达式,数组 [英] php associative arrays, regex, array

查看:118
本文介绍了PHP关联数组,正则表达式,数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下的code:

  $内容=
&LT;名称&gt;制造商和LT; /名称&gt;&LT; VALUE&GT;约翰Deere</value><name>Year</name><value>2001</value><name>Location</name><value>NSW</value><name>Hours</name><value>6320</value>\";

我需要找到创建的方法和数组名称=&GT;值。例如制造商=&GT;约翰迪尔

谁能帮我用一个简单的code剪断我尝试了一些正则表达式,但甚至不工作来提取名称或值,例如:

  $模式=/<name>Manufacturer<\\/name><value>(.*)<\\/value>/;
preg_match_all($模式,$内容$匹配);
$ st_selval = $比赛[1] [0];


解决方案

我用你的 $含量变量:

  $preG1 = preg_match_all('#&LT;名称&gt;([^&LT;] +)#,$内容,$ name_arr);
$preG2 = preg_match_all('#&LT; VALUE&GT;([^&LT;] +)#,$内容,$ val_arr);
$阵列= array_combine($ name_arr [1],$ val_arr [1]);

I currently have the following code :

$content = "
<name>Manufacturer</name><value>John Deere</value><name>Year</name><value>2001</value><name>Location</name><value>NSW</value><name>Hours</name><value>6320</value>";

I need to find a method to create and array as name=>value. E.g Manufacturer => John Deere.

Can anyone help me with a simple code snipped I tried some regex but doesn't even work to extract the names or values, e.g.:

$pattern = "/<name>Manufacturer<\/name><value>(.*)<\/value>/";
preg_match_all($pattern, $content, $matches);
$st_selval = $matches[1][0];

解决方案

I'm using your $content variable:

$preg1 = preg_match_all('#<name>([^<]+)#', $content, $name_arr);
$preg2 = preg_match_all('#<value>([^<]+)#', $content, $val_arr);
$array = array_combine($name_arr[1], $val_arr[1]);

这篇关于PHP关联数组,正则表达式,数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆