Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何拆分这些阵列?_Php_Arrays_String_Loops - Fatal编程技术网

Php 如何拆分这些阵列?

Php 如何拆分这些阵列?,php,arrays,string,loops,Php,Arrays,String,Loops,我想分两部分,然后得到最终结果 $data = "google,98%,bing,92%,searchengine,56%,seo,85%,search,94%"; 如果希望输出为包含新行的单个字符串,可以使用: 输出: $result = preg_replace('/([^,]*),([^,]*),?/', "$1 = $2\n", $data); 谷歌=98% bing=92% 搜索引擎=56% 搜索引擎优化=85% 搜索=94% 在线查看它的工作状态。尝试以下操作: google =

我想分两部分,然后得到最终结果

$data = "google,98%,bing,92%,searchengine,56%,seo,85%,search,94%";

如果希望输出为包含新行的单个字符串,可以使用:

输出:

$result = preg_replace('/([^,]*),([^,]*),?/', "$1 = $2\n", $data);
谷歌=98% bing=92% 搜索引擎=56% 搜索引擎优化=85% 搜索=94% 在线查看它的工作状态。

尝试以下操作:

google = 98% bing = 92% searchengine = 56% seo = 85% search = 94%
$data=“谷歌,98%,必应,92%,搜索引擎,56%,搜索引擎优化,85%,搜索,94%”;
preg_match_all(“/(\w+),(\d+)%/”,$data,$data_数组,preg_SET_顺序);
foreach($data\u数组作为$item){
打印$item[1]。“=”$item[2]。“%
”; }
解析都发生在一行中;唯一的循环是在输出中。您可以执行
print\r($data\u array)
查看数组的结构,以防您想对数据执行不同的操作


此外,如果希望在数据中包含百分号,可以将其移动到第二个括号对的内部。但是,如果不使用它(只在输出时显示),则在需要时对数据执行计算会更容易,这将为您提供关联数组:

$data = "google,98%,bing,92%,searchengine,56%,seo,85%,search,94%";

preg_match_all("/(\w+),(\d+)%/", $data, $data_array, PREG_SET_ORDER);

foreach($data_array as $item) {
    print $item[1]." = ".$item[2]."%<br />";
}
$out=array();
$parts=分解(“,”,$data);
对于($i=0;$i

$out = array();
$parts = explode(',', $data);
for($i=0;$i<count($parts);$i++) {
   $out[$parts[$i]] = $parts[++$i];
}

你想要什么格式的最终结果?一个关联数组?一个字符串数组,每行一个?一个包含新行的字符串?这是一个页面上的最终结果,我想显示某某得到某某百分比…实际上对于seo pupose
str_replace(',',','=',str_replace('%,“%\r\n”,$str))学会正确地提问。你需要这些问题的目的并不重要。但解释了很多:3如果他们只想改变文本布局,那么很好,如果他们真的需要数据,那么问题就更大了。好的一个短的,但我需要不同变量中的两个值。好的看起来不错,但如何得到最终结果??第一个$out[$parts[$i]]不返回valueTry
var_dump
foreach($out as$name=>$percent)
echo$out['google']
。是的,我现在知道了…之前有一些错误..我想他可能会编辑相同的内容。@aron-n:很好,可能有点复杂,但这是一项有用的技术。
$out = array();
$parts = explode(',', $data);
for($i=0;$i<count($parts);$i++) {
   $out[$parts[$i]] = $parts[++$i];
}
$data = "google,98%,bing,92%,searchengine,56%,seo,85%,search,94%";
$dataSet = array_combine(array_map(create_function('$entry', 'return $entry[0];'),
                                   array_chunk(explode(",", $data), 2)),
                         array_map(create_function('$entry', 'return $entry[1];'),
                                   array_chunk(explode(",", $data), 2)));

foreach ($dataSet as $cType => $cPercentage) {
    echo $cType . " = " . $cPercentage;
}