不区分大小写的唯一多维数组循环 [英] Case insensitive unique multidimensional array loop
本文介绍了不区分大小写的唯一多维数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遍历多维数组并获取唯一值(不区分大小写),有人可以帮助我吗?
I'm trying to loop through a multidimensional array and retreive unique values(case insensitive), can anyone help me?
$shop = array(
array(
'Title' => "General enquiries",
'Phone' => 02085237289,
),
array(
'Title' => "general enquiries",
'email' => 'something@gmail.com',
),
array(
'Title' => "not enquiries",
'Phone' => 02039303039,
),
array(
'Title' => "Not enquiries",
'email' => 'blah@gmail.com',
)
);
这就是我要创建的内容:
This what i'm trying to create:
General Enquiries
02085237289
something@gmail.com
Not enquiries
blah@gmail.com
02039303039
到目前为止我已经尝试过:
What I've tried so far:
$res = array();
foreach ($shop as $each) {
array_push($res,strtolower($each['Title']));
array_push($res,$each['email']);
array_push($res,$each['Phone']);
}
$test = array_unique($res);
foreach($test as $t){
//echo $t;
}
推荐答案
我在最后要感谢拉斯克拉特和刘浩天。我以为我应该把它摆出来,以防人们好奇。
谢谢大家!
I figured things out in the end thanks to Rasclatt and Haotian Liu. I thought I should put it up just in case people were curious. Thanks guys!
我稍微改变了数组,看起来是这样的:
I changed the array a bit, this is how it looked:
Array
(
[0] => Array
(
[contact_description] => Employment support
[contact_type] => Phone
[contact] => 0300 456 8110
)
[1] => Array
(
[contact_description] => General enquiries
[contact_type] => Phone
[contact] => 0300 456 8052
)
[2] => Array
(
[contact_description] => employment support
[contact_type] => Email
[contact] => employmentservices.osc@remploy.co.u
)
[3] => Array
(
[contact_description] => general enquiries
[contact_type] => Email
[contact] => info@remploy.co.uk
)
)
$res = array();
foreach ($shop as $each) {
$lcValue = strtolower($each['Title']);
if (isset($res[$lcValue]))
array_push($res[$lcValue], $each['contact']);
else
$res[$lcValue] = array($each['contact']);
}
foreach ($res as $name => $contact) {
echo '<h5 class="mb-0">' . ucwords($name) . '</h5>';
foreach ($contact as $contact) {
if (1 === preg_match('~[0-9]~', $contact)) {
// Phone Number
echo '<li class="work_number"><a href="tel:' . $contact . '">' . $contact . '</a></li>';
} elseif (strpos($contact, '@') !== false) {
//Email
echo '<li class="email"><a href="mailto:' . $contact . '" target="_blank">' . $contact . '</a></li>';
} else {
echo '<li><a>' . $contact . '</a></li>';
}
}
}
这篇关于不区分大小写的唯一多维数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文