Push entry in sub element php

Asked by At

I have an array $final_arr...which gives below output, then i merge fees and create a new array.

Array
(
    [0] => Array
        (

            [originator] =>Wayne
            [niche] => Construction
            [fee]=>30
        )

    [1] => Array
        (

            [originator] =>Josh
            [niche] => Construction
            [fee]=>30
        )

   [2] => Array
        (

            [originator] => Wayne
            [niche] => Construction
            [fee]=>50
        )

   [3] => Array
        (

            [originator] => Josh
            [niche] => Construction
            [fee]=>70
        )
)

I merged them using this code:

$final_arr = array();

foreach($formEntries as $value){

    if(isset($final_arr[$value['originator']])){
        $final_arr[$value['originator']]['fee_potential'] += $value['fee_potential'];
    } else{
        $final_arr[$value['originator']] = $value;
        $data_orgin[] = $value['originator'];
    }
}

which gives me below output when i print_r($final_arr), so now fees are total and array is smaller then before.

Array
(
    [Wayne] => Array
        (

            [originator] =>Wayne
            [entries=> Construction
            [fee]=>80
        )

    [Josh] => Array
        (

            [originator] =>Josh
            [niche] => Construction
            [fee]=>100
        )


)

Now i want to push entires like this in sub element but it does not work fine.:

  Array
(
    [wayne] => Array
        (

            [originator] =>Wayne
            [niche] => Construction
            [fee]=>80
                    [sub]=>array(
                            Array
                            (
                                [1] => Array
                                    (

                                    [originator] =>Wayne
                                    [niche] => Construction
                                    [fee]=>30
                                  )



                                [2] => Array
                                    (

                                    [originator] => Wayne
                                    [niche] => Construction
                                    [fee]=>50
                                  )


                           )


                    )


        )

    [josh] => Array
        (

            [originator] =>Josh
            [niche] => Construction
            [fee]=>100 


             [sub]=>array(
                            Array
                            (
                                [1] => Array
                                    (

                                    [originator] =>josh
                                    [niche] => Construction
                                    [fee]=>30
                                  )



                                [2] => Array
                                    (

                                    [originator] => josh
                                    [niche] => Construction
                                    [fee]=>70
                                  )


                           )


                    )
        )


)

2 Answers

2
Rahul Meshram On Best Solutions

You can try like this to get all the values in the sub below in foreach loop

foreach ($formEntries as $value) {

    if (isset($final_arr[$value['originator']])) {
        $final_arr[$value['originator']]['fee_potential'] = (!empty($final_arr[$value['originator']]['fee_potential']) ? $final_arr[$value['originator']]['fee_potential'] : 0) + (!empty($value['fee_potential']) ? $value['fee_potential'] : 0);
    } else {
        $final_arr[$value['originator']] = $value;
        $data_orgin[]                    = $value['originator'];
    }
    $final_arr[$value['originator']]['sub'][] = $value; // new line for sub index
}

Demo.

1
RiggsFolly On

Just add one more line placing the $value array into a sub array

$final_arr = array();

foreach($formEntries as $value){

    if(isset($final_arr[$value['originator']])){
        $final_arr[$value['originator']]['fee_potential'] += $value['fee_potential'];
    } else{
        $final_arr[$value['originator']] = $value;
        $data_orgin[] = $value['originator'];
    }
    // new line
    $final_arr[$value['originator']]['sub'][] = $value;
}