【问题标题】:Get all values with a certain key value获取具有某个键值的所有值
【发布时间】:2011-11-15 21:02:39
【问题描述】:

所以我一直在尝试解决这个问题一段时间,但似乎被卡住了。我有一个多维数组,我正在尝试使用某个键输出所有值。代码如下:

//My Array:
$services = array(
                array(  service_name => 'facebook', 
                    service_title => 'Facebook',
                    service_order => 0, 
                    service_type => 'Social Networking'
                    ),
                array(  service_name => 'twitter', 
                    service_title => 'Twitter',
                    service_order => 0,
                    service_type => 'Social Networking'
                    ),
                array(  service_name => 'tumblr', 
                    service_title => 'MySpace',
                    service_order => 0, 
                    service_type => 'Blogging'
                    )
);

问题是我想通过service_type 输出值,例如“社交网络”或“博客”,如果我使用这样的 foreach 语句,我可以做到这一点:

 foreach($services as $service) {
    if ($service['service_type'] == 'Social Networking') {
             echo($service['service_title']);
    }
    if ($service['service_type'] == 'Blogging') {
             echo($service['service_title']);
    }
}

在我尝试在诸如

之类的类型之间添加标题之前效果很好
foreach($services as $service) {
    if ($service['service_type'] == 'Social Networking') {
             echo($service['service_title']);
    }
    echo ('<h3> Blogging</h3>');
    if ($service['service_type'] == 'Blogging') {
             echo($service['service_title']);
    }
}

我已经学到了足够多的知识,知道这是因为foreach 循环的工作方式,所以我一直在尝试使用while($service['service_type'] == 'Social Networking')(创建一个有趣的无限循环)和其他各种方式来做到这一点。

理想情况下,我想按service_type 对数组进行排序,然后显示结果,而不必在标题之间运行foreach 循环。

对此的任何帮助都会很棒,甚至还有其他关于如何做到这一点的建议。

【问题讨论】:

    标签: php arrays loops multidimensional-array foreach


    【解决方案1】:

    我认为你正在尝试做这样的事情:

    // Create an array containing all of the distinct "service types"
    // from the $services array
    $service_types = array();
    foreach ($services as $service) {
        // If we have not yet encountered this entry's "service type",
        // then we need to add it to our new array
        if (!in_array($service['service_type'], $service_types)) {
            $service_types[] = $service['service_type'];
        }
    }
    if (count($service_types)) {
        // For each "service type", we want to echo a header and then
        // a list of services of that type
        foreach ($service_types as $current_type) {
            echo '<h3>'.$current_type.'</h3>';
            // Loop through the services and echo those that are
            // of the correct type
            foreach ($services as $service) {
                if ($service['service_type'] == $current_type) {
                    echo($service['service_title']);
                }
            }
        }
    } else {
        echo '<h3>No services</h3>'
    }
    

    【讨论】:

    • 感谢您让我走上正轨。尽管Warning: in_array() [function.in-array]: Wrong datatype for second argument 不知道这意味着什么,但我会收到此错误,但我会稍微尝试一下。如果您能稍微解释一下代码,我也会喜欢它。我了解大部分内容,但我了解的越多越好。
    • service_type 应该被引用。 $service[service_type] 应该是第 3 行和第 4 行的 $service['service_type']。另外,如果您想按服务类型排序,只需在第一个 foreach 之后添加 sort($service_types);
    • 我添加了一些 cmets。我不知道你为什么会收到这个警告,代码一看就知道是正确的。
    • 我先这样做了,但似乎在向我的朋友 Google 询问后,问题是循环首次运行时未定义 $service_types,因此它会发出警告。我在第一个 if 之前添加了 $service_types=array(); 并且效果很好。
    • 你错过了一个:$service_types[] = $service['service_type'] :)
    【解决方案2】:

    我没有关注你的所有问题,但你问是否可以按服务类型对数组进行排序 - 这样你就可以构建一个 user defined sorting function

    function sortByServiceType($a, $b) {  
      // This will sort them by service type alphabetically
      return strcmp($a['service_type'], $b['service_type']);
    }
    
    uasort($services, 'sortByServiceType');
    

    【讨论】:

      【解决方案3】:

      使用 php > 5.5 你可以做到

      $myData = array(
          array(
              'service_name'  => 'facebook', 
              'service_title' => 'Facebook',
              'service_order' => 0, 
              'service_type'  => 'Social Networking'
          ),
          array(  
              'service_name'  => 'twitter', 
              'service_title' => 'Twitter',
              'service_order' => 0,
              'service_type'  => 'Social Networking'
          ),
          array(
              'service_name'  => 'tumblr', 
              'service_title' => 'MySpace',
              'service_order' => 0, 
              'service_type'  => 'Blogging'
          )
      );
      
      $filtered = array_column($myData, 'service_column');
      

      见:http://www.php.net/manual/en/function.array-column.php

      【讨论】:

      • 这太棒了。太糟糕了,我 刚刚 切换到 5.4 并且需要一段时间才能使用它.. :(
      猜你喜欢
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多