我的数组
name | sexe
jean | H
marck | H
élodie | F
我使用此代码获取$name="jeaan";
$ss[0]['name']='jean';
$ss[0]['sexe']='H';
$ss[1]['name']='marck';
$ss[1]['sexe']='H';
$ss[2]['name']='élodie';
$ss[2]['sexe']='F';
print_r($ss);
$name='jeaan';
for($i=0;$i<count($ss);$i++){
$pos = strpos($name, $ss[$i]['name']);
if ($pos !== false)
{
echo "yes :". $ss[$i]['sexe'];
break;
}
}
exit('null');
我把我的字符串与数组隔开但我的代码返回null
我如何为jeaan = H做爱?
英语不是我的母语,对任何错误都很抱歉
答案 0 :(得分:2)
这应该适合你:
只需使用array_column()
从数组中获取列name
即可。然后使用array_search()
在数组中搜索$name
,并将其用作第一维的键,例如
$name = 'jean';
if(in_array($name, array_column($ss, "name"))) //To make sure the name exists
echo $ss[array_search($name, array_column($ss, "name"))]["sexe"];
输出:
H
答案 1 :(得分:1)
$ss = array(
array('name'=> 'Jean', 'sexe'=> 'H'),
array('name'=> 'Marck', 'sexe'=> 'H'),
array('name'=> 'Elodie', 'sexe'=> 'F'),
);
$jean_k = array_search('Jean', array_column($ss, 'name'));
echo 'Jean sexe is: ' . $ss[$jean_k]['sexe'];
答案 2 :(得分:0)
版本2通过similar_text函数找到壁橱名称
<?php
$ss=array(
array('name'=>'jean','sexe'=> 'H'),
array('name'=>'marck','sexe'=> 'H'),
array('name'=>'élodie','sexe'=> 'F'),
);
//print_r($ss);
function isSexe($ss, $who){
$match=array();
foreach($ss as $k){
similar_text($k['name'], $who, $percentage);
$match[$k['name'].'|'.$k['sexe']]=$percentage;
}
arsort($match);
//print_r($match);
return current(array_keys($match));
}
echo isSexe($ss, 'marck');
答案 3 :(得分:0)
$name='jean'; // search string
$matches = array_filter($ss, function($v) use ($name){
return in_array($name, $v);
}); // will give array of matches of $name
$sexe = $matches[0]['sexe']; // will give 'H'