获取具有特定键值的所有值

时间:2011-09-13 05:47:40

标签: php arrays loops multidimensional-array foreach

所以我一直试图弄清楚这一段时间,似乎被卡住了。我有一个multidimensionalarray,我试图用一个键输出所有值。这是代码:

//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循环。

对此的任何帮助都会很棒甚至是其他建议。

3 个答案:

答案 0 :(得分:9)

使用php&gt; 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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

我没有按照您的所有问题,但您询问是否可以按服务类型对数组进行排序 - 这样您就可以构建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');