0

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
Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65
  • 2
    okay, and what did you try for now? did it not work as intended? – rsz May 18 '16 at 09:21
  • Actually, I am confused how to do this. – Manish Tiwari May 18 '16 at 09:22
  • use loop and condition to store like you want – Murad Hasan May 18 '16 at 09:24
  • 1
    To begin, your multidimensional array isn't descriptive enough (or understandable) for the array to be built into the structure you wanted. I suppose if you mean "Project Build" to be "Project Quality", then you simply just need to loop through the array, check "L2_ATTR_DESC" and sort it into another array. – mauris May 18 '16 at 09:24
  • @ManishTiwari which part confuses you? – rsz May 18 '16 at 09:24
  • check this answer http://stackoverflow.com/questions/10408482/how-to-get-unique-value-in-multidimensional-array – Ranjeet Singh May 18 '16 at 09:26
  • see this logic: this is not complete but almost, just see the code [https://3v4l.org/JuPIa](https://3v4l.org/JuPIa) – Murad Hasan May 18 '16 at 09:30

3 Answers3

0

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.

JesusTheHun
  • 1,217
  • 1
  • 10
  • 19
0

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.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

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
            )
    )
KARTHI SRV
  • 499
  • 4
  • 20