简体   繁体   中英

How to get one value from duplicates values in Multidimensional array using PHP?

I have an multidimensional array some values are duplicate and some unique.

duplicate values are parent and unique values are child of duplicate values.

Multidimensional array :

Array
(
    [0] => Array
        (
            [L1_ID] => 21
            [L1_ATTR_DESC] => Project Overview
            [L2_ATTR_DESC] => Project Status
            [L3_ATTR_DESC] => Ready
        )
    [1] => Array
        (
            [L1_ID] => 21
            [L1_ATTR_DESC] => Project Overview
            [L2_ATTR_DESC] => Project Status
            [L3_ATTR_DESC] => Pending
        )
    [2] => Array
        (
            [L1_ID] => 21
            [L1_ATTR_DESC] => Project Overview
            [L2_ATTR_DESC] => Project Status
            [L3_ATTR_DESC] => Completed
        )
     [3] => Array
        (
            [L1_ID] => 21
            [L1_ATTR_DESC] => Project Overview
            [L2_ATTR_DESC] => Project Quality
            [L3_ATTR_DESC] => Independ
        )
       [4] => Array
        (
            [L1_ID] => 21
            [L1_ATTR_DESC] => Project Overview
            [L2_ATTR_DESC] => Project Quality
            [L3_ATTR_DESC] => G+1
        )
)

I want to show duplicate value print one time and unique value should be child of duplicate values.

Expected Output - Like this :

-Project Status      -Project Build
    --Ready             --Independ
    --Pending           --G+1
    --Completed

I think what you want is commonly called "index array". You can check array_column , which I think will fill your need.

$array = array(/* multidimensional array */);
$index = array_column($array, L3_ATTR_DESC);

$index will contain an array of values associated with L3_ATTR_DESC in $array.

Just for check see whats happen: Online Link

$ps = array();
foreach($arr as $value){
    $index = str_replace(" ", "", strtolower($value['L2_ATTR_DESC']));
    $ps[$index][] = $value['L3_ATTR_DESC'];
}

echo '<pre>';
print_r($ps);

From now you can design it in your way.

Working demo . You can use foreach Statement

foreach($a as $arg) {
   $tmp[$arg['L1_ATTR_DESC']][$arg['L2_ATTR_DESC']][] = $arg['L3_ATTR_DESC'];
}

$output = array();
foreach($tmp as $type => $labels) {
$output[] = array(
    'L1_ATTR_DESC' => $type,
    'L2_ATTR_DESC' => $labels
  );
}

I think you expecting output like these

 Array
(

     [L1_ATTR_DESC] => Project Overview
     [L2_ATTR_DESC] => Array
         (
            [Project Status] => Array
                (
                     [0] => Ready
                     [1] => Pending
                     [2] => Completed
                )

            [Project Quality] => Array
                (
                     [0] => Independ
                     [1] => G+1
                )
       )
)

OR

foreach($a as $arg)
{
    $tmp[$arg['L2_ATTR_DESC']][] = $arg['L3_ATTR_DESC'];
}
print_r($tmp);

Output

    Array
    (
        [Project Status] => Array
            (
                 [0] => Ready
                 [1] => Pending
                 [2] => Completed
            )

        [Project Quality] => Array
            (
                 [0] => Independ
                 [1] => G+1
            )
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM