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
7
  • 2
    okay, and what did you try for now? did it not work as intended? Commented May 18, 2016 at 9:21
  • Actually, I am confused how to do this. Commented May 18, 2016 at 9:22
  • use loop and condition to store like you want Commented May 18, 2016 at 9: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. Commented May 18, 2016 at 9:24
  • @ManishTiwari which part confuses you? Commented May 18, 2016 at 9:24

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

why you think? why not confident?
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
            )
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.