我有一个包含多个值的字符串。新行被识别为|符号。 |之间的值永远在一起。
现在,我正在尝试例如IKK和Homo sapiens是否存在于该行中,以返回该行中的其他值(96,NC_000001,IKK,核因子抑制剂,IKK = IKKI)。
我使用explode函数来基于|分离字符串符号。但是,如果我现在再次使用爆炸功能,符号。我再次得到一个阵列。但如果我现在想要寻找智人和智人IKK并希望从同一行返回其他值,我卡住了。
我有以下字符串:
$var = 'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor,
IKK = IKKI |GAL4 ,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81
|TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';
我的代码:
function terms($result){
$result = (explode("|",$result));
$final = array_unique($result);
return $final;
}
$test = terms($var);
foreach ($test as $key => $value){
echo "key: $key -- value: $value"."<br \>\n";
$res = explode(",",$value);
foreach ($res as $v){
echo "tester: >> $v"."<br \>\n";
}
}
我可能应该最终得到这样的东西。但我找不到办法到达那里。是否有可能获得这种结构?
if ($d[1] == "Homo sapiens" && $d[0] == "IKK"){
$k = $d[2] # 96
$s = $d[3] # NC_000001
}
作为最终结果,我想将值96,NC_000001,IKK,核因子抑制剂,IKK = IKKI分别保存在一个单独的变量中。 $ number = 96 $ acc = NC_000001
答案 0 :(得分:1)
对于这部分:
$filters = array("IKK", "Homo sapiens", .... "etc.");
$res = explode(",", $value);
$raw_filtered_res = array_filter($res, function ($v, $k) use($filters) {
return !in_array($v, $filters);
}, ARRAY_FILTER_USE_BOTH);
if (count($res) !== count($raw_filtered_res)) {
$filtered_res = array_values(array_diff($res, $raw_filtered_res));
foreach ($filtered_res as $v) {
echo "tester: >> $v"."<br \>\n";
}
} else {
break;
}
重写为
function hasAccess() {
var dataObj = {
"id" : $scope.groupId
};
$http.post('http://vlinux:9099/GetItems', dataObj).then(
function(response) {
var result = response.data.result;
if(result.includes($scope.screenId)) {
return "ok";
} else {
return "nok";
}
}).catch(function (error) {
// do something
});
}
答案 1 :(得分:1)
我还没有运行它,所以这里可能存在语法错误......但是这样的事情可能适合你的需求吗?一个通用函数,用于将后续子字符串需要的术语数组传递给(后管道爆炸)
function filterList($str, $needed = []){
//str is the pre-exploded comma delimited string
//needed are the terms required for the string to pass
//needed terms are filtered out of the returned array
$found = true;
if(! is_array($needed)) $needed = [$needed];
foreach($needed as $needs_str){
if(stripos($str, $needs_str) === false){
$found = false;
}
}
if($found){
return array_filter(explode($str, ','), function($item) use ($needed){
return ! in_array($item, $needed);
});
}
return [];
}
然后使用
foreach ($test as $key => $value){
$res = filterList($value, ["Homo sapiens","IKK"]);
if(! empty($res)){
//do something with res
}
}
答案 2 :(得分:1)
最初的问题之一是您的源数据在整个地方都包含空格,因此第一部分是向下拆分数据并array_walk()
所有数据。 $filters
用于一次处理每一行。
然后是根据您想要的字段过滤结果的情况。这是检查array_slice()
数组是否与术语中的第一个字段相同(使用array_slice()
来提取正确数量的字段)的情况,只需比较数组而不是单个字段,但是这假设字段的顺序相同。
然后使用输出,它会通过从前面移除过滤器来处理任何结果(再次使用$var = 'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor,
IKK = IKKI |GAL4 ,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81
|TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';
$terms = explode("|", $var);
array_walk($terms, function (&$line) {
$line = explode(",",$line);
$line = array_map('trim', $line);
});
$filters = ["IKK", "Homo sapiens"];
$output = array_filter($terms, function ($line) use ($filters) {
return $filters == array_slice($line, 0,count($filters));
});
array_walk($output, function (&$line) use ($filters) {
$line = array_slice($line, count($filters));
});
print_r($output);
)。
results.push([[warrantyType],[warrantyStart],[warrantyEnd],[model]]);
答案 3 :(得分:1)
由于涉及白色空间,我认为您无法比较值。 请试试这个:
<?php
$var = 'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor,
IKK = IKKI |GAL4 ,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81
|TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';
function terms($result){
$result = (explode("|",$result));
$final = array_unique($result);
return $final;
}
function compare_terms($res){
$d = array_map('trim',$res);
if ($d[1] == "Homo sapiens" && $d[0] == "IKK") {
$k = $d[2] ;
echo $k ."<br \>\n"; // 96
$s = $d[3] ;
echo $s ."<br \>\n"; // NC_000001
}
}
$test = terms($var);
foreach ($test as $key => $value){
echo "key: $key -- value: $value"."<br \>\n";
$res = explode(",",$value);
compare_terms($res);
}
在compare_terms函数中,我们使用以下方法对数组的每个值应用trim函数: $ d = array_map(&#39; trim&#39;,$ res);
现在我们可以相应地比较值
答案 4 :(得分:1)
PHP has a concept of nested arrays. Each element of an array can be an array, too. So explode and iterate the lines, then explode the fields. You can use array_map()
to clean up the field values:
$data =
'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor, IKK = IKKI |'.
'GAL4,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81|'.
'TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';
$values = [];
foreach (explode('|', $data) as $line) {
$values[] = array_map(
function($field) {
return trim($field);
},
explode(',', $line)
);
}
var_dump($values);
Output:
array(4) {
[0]=>
array(7) {
[0]=>
string(3) "IKK"
[1]=>
string(12) "Homo sapiens"
[2]=>
string(2) "96"
[3]=>
string(9) "NC_000001"
[4]=>
string(3) "IKK"
[5]=>
string(27) "inhibitor of nuclear factor"
[6]=>
string(10) "IKK = IKKI"
}
[1]=> ...
So now that you have that list of arrays you can use array_filter()
.
$filteredValues = array_filter(
$values,
function($fields) {
return ($fields[0] === 'IKK' && $fields[1] === 'Homo sapiens');
}
);
var_dump($filteredValues);
Or you use a foreach()
to find the first line that matches your condition.
$searchedLine;
foreach ($values as $fields) {
if ($fields[0] === 'IKK' && $fields[1] === 'Homo sapiens') {
$searchedLine = $fields;
break;
}
}
var_dump($searchedLine);