PHP多维数组foreach - 不要重复共同的值

时间:2016-04-22 00:16:55

标签: php multidimensional-array

这是我的阵列:

    $tabs = Array(
[0] => Array
    (
        [primaryTabID] => 001
        [subtabID] => 
        [parent_tab] => 
        [primaryName] => Tab1
        [subName] => 
        [BStatus] => 
    )

[1] => Array
    (
        [primaryTabID] => 002
        [subtabID] => 013
        [parent_tab] => 002
        [primaryName] => Tab2
        [subName] => SubTab2-1
        [BStatus] => A
    )

[2] => Array
    (
        [primaryTabID] => 003
        [subtabID] => 015
        [parent_tab] => 003
        [primaryName] => Tab3
        [subName] => SubTab3-1
        [BStatus] => A
    )

[3] => Array
    (
        [primaryTabID] => 003
        [subtabID] => 016
        [parent_tab] => 003
        [primaryName] => Tab3
        [subName] => Subtab3-2
        [BStatus] => A
    )
  )

我要查找的输出仅列出每个主要标签名称一次,然后如果它有子标签,则每个标签都列在主要标签之后,如下所示:

输出:

    001 -> Tab1
    002 -> Tab2
        013 -> SubTab2-1
    003 -> Tab3
        015 -> SubTab3-1
        016 -> SubTab3-2

foreach语句的嵌套和if-else让我完全糊涂了。

4 个答案:

答案 0 :(得分:1)

像这样创建你的数组......

$out = array();
foreach($tabs as $tab) {
    if(!empty($tab['primaryTabID']))
        $out[$tab['primaryTabID']]['main'] = $tab['primaryName'];           
    if($tab['primaryTabID'] == $tab['parent_tab'])
        $out[$tab['primaryTabID']][$tab['subtabID']] = $tab['subName'];
}
echo "<pre>";
var_dump($out);

然后,处理它(输出与你在上面显示的相同..

    foreach($out as $key => $tab) {
        if(count($tab) == 1) {
            echo $key . " -> " .$tab['main']."\n";
        } else {
            foreach($tab as $subKey => $sub) {
                if($subKey == "main")
                    echo $key . " -> " .$tab['main']."\n";
                else
                    echo "\t".$subKey . " -> " .$sub."\n";
            }
        }
    }

<强>输出

001 -> Tab1
002 -> Tab2
    013 -> SubTab2-1
003 -> Tab3
    015 -> SubTab3-1
    016 -> Subtab3-2

答案 1 :(得分:1)

您可以使用以下内容:

数据:

$tabs = array(
    0 => array(
        'primaryTabID' => '001',
        'subtabID' => '',
        'parent_tab' => '',
        'primaryName' => 'Tab1',
        'subName' => '',
        'BStatus' => '',
    ),
    1 => array(
        'primaryTabID' => '002',
        'subtabID' => '013',
        'parent_tab' => '002',
        'primaryName' => 'Tab2',
        'subName' => 'SubTab2-1',
        'BStatus' => 'A',
    ),
    2 => array(
        'primaryTabID' => '003',
        'subtabID' => '015',
        'parent_tab' => '003',
        'primaryName' => 'Tab3',
        'subName' => 'SubTab3-1',
        'BStatus' => 'A',
    ),
    3 => array(
        'primaryTabID' => '003',
        'subtabID' => '016',
        'parent_tab' => '003',
        'primaryName' => 'Tab3',
        'subName' => 'Subtab3-2',
        'BStatus' => 'A',
    ),
);

<强>代码:

$new_tabs = array();

foreach($tabs as $key => $value) {
    if (!array_key_exists ($value['primaryTabID'], $new_tabs)) {
        $new_tabs[$value['primaryTabID']] = array('primaryName' => $value['primaryName']);
    }
    if (!empty($value['subtabID'])) {
        $new_tabs[$value['primaryTabID']]['children'][$value['subtabID']] = $value['subName'];
    }
}

foreach($new_tabs as $key => $value) {
    echo '<p>'.$key.' -> '.$value['primaryName'].'</p>';
    if (!empty($value['children'])) {
        echo '<ul>';
        foreach($value['children'] as $s_key => $s_value) {
            echo '<li>'.$s_key.' -> '.$s_value.'</li>';
        }
        echo '</ul>';
    }
}

<强>输出:

001 -> Tab1

002 -> Tab2

    013 -> SubTab2-1

003 -> Tab3

    015 -> SubTab3-1
    016 -> Subtab3-2

答案 2 :(得分:0)

$newArray = array();

foreach ($tab as $elements)
{
  array_push($newArray, 
    array(
      $elements['primaryTabID'] => $elements['primaryName'], 
      $elements['subtabID'] => $elements['subName']
    )
  );
}

如果您正在寻找,请告诉我!如果它没有告诉我有什么不同,因为我可能误解了你希望Sub标签与主要标签相关的位置

答案 3 :(得分:0)

尝试这样的事情

$newArray = array();
foreach ($tabs as $tab)
{
    $key = array_search($tab['primaryName'], array_column($newArray, 'primaryTabID'));
    if($key)
    {
        $newArray[$key]['subtabID'][] = $tab['subName'];
    }
    else
    {
      array_push($newArray,  array( 'primaryTabID' => $tab['primaryName'], $tab['subtabID'] => array($tab['subName'])) );
    }
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";