skip to Main Content

Hi i have many children arrays inside array all data exactly comes from api output result is i tried array recursive php but not worked

Array
(
    [0] => Array
        (
            
            [price] => 100
            [weight] => 42
            [sub_option] => Array
                (
                    [0] => Array
                        (
                            [price] => 25.99
                            [points] => 32
                            [sub_option] => Array
                                (
                                    [0] => Array
                                        (
                                            [price] => 27.99
                                            [weight] => 83
                                            [sub_option] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [price] => 14.99
                                                            [weight] => 50
                                                            [sub_option] => NULL
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [price] => 24.99
                            [weight] => 25
                            [sub_option] => NULL

                        )

                )

        )

    [1] => Array
        (
            [price] => 100
            [weight] => 4233
            [sub_option] => NULL
        )

)

like this but i want output like this ……

Array
(
    [0] => Array
        (
            [price] => 100
            [weight] => 42
        )
    [1] => Array
        (
            [price] => 25.99
            [points] => 32
        )
    [2] => Array
        (
            [price] => 27.99
            [weight] => 83
        )
    [3] => Array
        (
            [price] => 14.99
            [weight] => 50
            [sub_option] => NULL
        )

    [4] => Array
        (
            [price] => 24.99
            [weight] => 25
            [sub_option] => NULL

        )
    [5] => Array
        (
            [price] => 100
            [weight] => 4233
            [sub_option] => NULL
        )
)

I am using PHP version 7 and i also tried to search many online solutions from stackoverflow but i cant find please help me thanks

I tried this code but not working

$output = [];

$arr1 = [];
$arr2 = [];
foreach($arr as $key=>$value){
    $arr1 = [
        'price' => $value['price'],
        'weight' => $value['weight'],

    ];

    if(isset($value['sub_option']) && is_array($value['sub_option'])){
        foreach($value['sub_option'] as $val2){
            $arr2 = [
                'price' => $val2['price'],
                'weight' => $val2['weight'],
            ];

        }
    }

$output[] = $arr1+$arr2;

}

print_r($output);

2

Answers


  1. Here is one method of doing this

    function format_array($arr) {
        $tmp = [];
        foreach($arr as $a) {
            //pass each of the root values to the helper function
            //also pass a reference to $tmp
            format_array_helper($a, $tmp);
        }
        return $tmp;
    }
    
    function format_array_helper($loop_value, &$tmp) {
        
        //set the sub options to it's own variable,
        //unset the sub options from the main variable
        $sub_options = $loop_value['sub_option'];
        unset($loop_value['sub_option']);
        
        //add the main variable to $tmp
        $tmp[] = $loop_value;
        
        //if there are sub options
        if(!empty($sub_options)) {
            foreach($sub_options as $sub) {
                
                //pass each root of sub_options to the helper function
                //also pass the reference to tmp
                format_array_helper($sub, $tmp);
                
            }
        }
    }
    

    Let’s assume your array is called $arr. Here is the output of print_r(format_array($arr));

    Array
    (
        [0] => Array
            (
                [price] => 100
                [weight] => 42
            )
    
        [1] => Array
            (
                [price] => 25.99
                [weight] => 32
            )
    
        [2] => Array
            (
                [price] => 27.99
                [weight] => 83
            )
    
        [3] => Array
            (
                [price] => 14.99
                [weight] => 50
            )
    
        [4] => Array
            (
                [price] => 24.99
                [weight] => 25
            )
    
        [5] => Array
            (
                [price] => 100
                [weight] => 4233
            )
    
    )
    
    Login or Signup to reply.
  2. Here is another method to doing this (with anonymous function)

    #example date
    $data = [
        [
            'price' => 101,
            'weight' => 42,
            'sub_option' =>
                [
                    'price' => 101,
                    'weight' => 43,
                    'sub_option' => [
                                     [
                                      'price' => 101,
                                      'weight' => 44,
                                      'sub_option' => null
                                     ]
                                    ]
                ]
        ],
        [
            'price' => 102,
            'weight' => 42,
            'sub_option' => null
        ]
    ];
    #global collector
    $result = [];
    #function variable, here with null value so we can use it as reference
    $func = null;
    #create the real function & use references
    $func = function ($current) use (&$result, &$func) {
        #look for assoc array key
        if (isset($current['price'])) {
            #copy and unset not needed keys
            $tmp = $current;
            unset($tmp['sub_option']);
            #collect 
            $result[] = $tmp;
            #check for subarray and go deeper
            if (is_array($current['sub_option'])) {
                $func($current['sub_option']);
            }
        } elseif (is_array($current)) {
            #iterate index arrays
            foreach ($current as $sub) {
                $func($sub);
            }
        }
    };
    $func($data);
    var_export($result);
    

    I will not explain this in detail, because first answer from GrumpyCrouton should be taken/accepted. But when you take some time, may you learn new stuff and how php works from this little code snipped.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search