不区分大小写的唯一多维数组循环

时间:2017-07-03 03:05:52

标签: php arrays multidimensional-array unique

我正在尝试遍历多维数组并检索唯一值(不区分大小写),任何人都可以帮助我吗?

$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',
            )
        );

这是我想要创造的:

General Enquiries 
02085237289
something@gmail.com

Not enquiries
blah@gmail.com
02039303039 

到目前为止我尝试过:

$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;
}

2 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是使用两个数组,一个用于存储原始值,另一个用于存储小写比较:

# 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);

给你:

Array
(
    [0] => General enquiries
    [1] => 02085237289
    [2] => something@gmail.com
    [3] => not enquiries
    [4] => 02039303039
    [5] => blah@gmail.com
)

有一点需要注意的是,您将如何知道要存储的版本,按照数组的顺序,将不会获得Not enquiries的大写版本,因为小写版本首先在循环中运行。你的例子有大写,但你说它可以不区分大小写,所以我想它很好......

答案 1 :(得分:0)

由于Rasclatt和Haotian Liu,我最终想出了一些事情。我想我应该把它放在一起以防人们好奇。 谢谢你们!

我稍微更改了数组,这就是它的样子:

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>';
                        }
                    }

                }