问题
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;
}
回答1:
One method to accomplish this is with two arrays, one to store the original value and one to store a lowercase comparison:
# Create comparison array
$compare = array();
# Create a final store array
$store = array();
# Loop main rows
foreach($shop as $row) {
# Loop rows (don't hardcode, it may change later)
foreach($row as $key => $value) {
# Since case is fine, you can turn all to lower for comparison
$lcValue = strtolower($value);
# Check if not in comparison array already
if(!in_array($lcValue,$compare)) {
# If not, add lowercase version to $compare and add original to $store
$store[] = $value;
$compare[] = $lcValue;
}
}
}
print_r($store);
Gives you:
Array
(
[0] => General enquiries
[1] => 02085237289
[2] => something@gmail.com
[3] => not enquiries
[4] => 02039303039
[5] => blah@gmail.com
)
One caveat is how you are going to know which version to store, in the order you have the array, will not get the uppercase version of Not enquiries
because the lower case version runs first in the loop. Your example has uppercase but you say it can be case insensitive, so I imagine it's fine...
回答2:
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>';
}
}
}
来源:https://stackoverflow.com/questions/44877229/case-insensitive-unique-multidimensional-array-loop