0

I have an array which have 3 level

Array
 (
   [BIOCHEMISTRY] => Array
    (
        [BloodRoutine] => Array
            (
                [DifferentialCount] => Array
                    (
                        [itemId] => 552
                        [catId] => 2
                        [testId] => 1
                        [parent] => 0
                        [title] => FBS
                        [unit] => mg%
                        [amount] => 20
                        [catName] => BIOCHEMISTRY
                    )
               [ESR]=>2
               [PPR]=>4

            )
         [PCV]=>3

     )
  )

i need to populate an array if testId has value

 Array
 (
   [BIOCHEMISTRY] => Array
    (
        [BloodRoutine] => Array
            (
                [DifferentialCount] => Array
                    (
                        [itemId] => 552
                        [catId] => 2
                        [testId] => 1
                        [parent] => 0
                        [title] => FBS
                        [unit] => mg%
                        [amount] => 20
                        [catName] => BIOCHEMISTRY
                    )

            )

     )
  )

i need to populate an array if ESR has value, no value for any of the child for DifferentialCount

Array
 (
   [BIOCHEMISTRY] => Array
    (
        [BloodRoutine] => Array
            (

               [ESR]=>2

            )

     )
  )

i need to populate an array if PCV has value, no value for any of the child for BloodRoutine

Array
 (
   [BIOCHEMISTRY] => Array
    (

         [PCV]=>3

     )
  )

Please give me a dynamic function to do this.

Thank you.

0

2 Answers 2

1

Try

foreach($input as $key1=>$level1) {
    if(is_array($level1)) {
        foreach($level1 as $key2=>$level2) {
            if(is_array($level2)) {
                foreach($level2 as $key3=>$level3) {
                    if(is_array($level3)) {
                        foreach($level3 as $key4=>$level4) {
                            if(isset($level4)) {
                                $output[$key1][$key2][$key3][$key4]=$level4;
                            }
                        }
                    }
                    else {
                        if(isset($level3)) {
                            $output[$key1][$key2][$key3]=$level3;
                        }
                    }
                }
            }
            else {
                if(isset($level2)) {
                    $output[$key1][$key2]=$level2;
                }
            }
        }
    }
    else {
        if(isset($level1)) {
            $output[$key1]=$level1;
        }
    }
}
echo '<pre>';print_r($output);echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

0

use foreach function and loop through the array and using the $key in the foreach check if($key=='testid') {//populate} if($key=='ESR') and like wise. You need to do it in seperate foreach for the coding comfort.

1 Comment

can you write a function for the same

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.