Jump to content

Converting a Multi-dimensional Array Structure


bannock

Recommended Posts

Hi,

 

I have a database query returning the following array structure;

Array
(
    [0] => Array
        (
            [Category] => Array
                (
                    [id] => 9
                    [name] => ABC
                )

            [Ds] => Array
                (
                    [ds_id] => 39
                )

        )

    [1] => Array
        (
            [Category] => Array
                (
                    [id] => 10
                    [name] => DEF
                )

           [Ds] => Array
                (
                    [ds_id] => 40
                )

        )

)

I need to change the structure of the array(to feed a json object) to this;

Array
(
    [0] => Array
        (
           [id] => 9
           [name] => ABC
           [ds_id] => 39
         )

    [1] => Array
        (
           [id] => 10
           [name] => DEF
           [ds_id] => 40
         )

)

I am embarassed to say i have spent the best part of two days mucking around with a multitude of recursive functions to no avail. At best i completely flatten the array at worst i end up with a complete mishmash.

 

I tried to set up a php debugger to watch the process but after 3 days of trying i came to the conclusion it was not compatible with my working environment.

I am still using php 4.4.9.

Any assistance would be greatly appreciated

Try this

I have got this from http://us3.php.net/manual/en/function.array-values.php

<?php
    function array_flatten($array, $preserve = FALSE, $r = array()){
        foreach($array as $key => $value){
            if (is_array($value)){
                foreach($value as $k => $v){
                    if (is_array($v)) { $tmp = $v; unset($value[$k]); }
                }
                if ($preserve) $r[$key] = $value;
                else $r[] = $value;
            }
        }
        $r = isset($tmp) ? array_flatten($tmp, $preserve, $r) : $r;
        return $r;
    }

    print_r($tmp);
/* ---
Array
(
    [home] => Array
        (
            [id] => 1
            [pid] => 0
            [link] => home
            [subcat] =>
        )

    [works] => Array
        (
            [id] => 2
            [pid] => 0
            [link] => works
            [subcat] => Array
                (
                    [viz] => Array
                        (
                            [id] => 4
                            [pid] => 2
                            [link] => viz
                            [subcat] =>
                        )

                    [script] => Array
                        (
                            [id] => 5
                            [pid] => 2
                            [link] => script
                            [subcat] => Array
                                (
                                    [arch] => Array
                                        (
                                            [id] => 6
                                            [pid] => 5
                                            [link] => arch
                                            [subcat] =>
                                        )

                                )

                        )

                )

        )

    [blog] => Array
        (
            [id] => 3
            [pid] => 0
            [link] => blog
            [subcat] =>
        )

)

--- */

    print_r(array_flatten($tmp, 1));

/* ---
Array
(
    [home] => Array
        (
            [id] => 1
            [pid] => 0
            [link] => home
            [subcat] =>
        )

    [works] => Array
        (
            [id] => 2
            [pid] => 0
            [link] => works
        )

    [blog] => Array
        (
            [id] => 3
            [pid] => 0
            [link] => blog
            [subcat] =>
        )

    [viz] => Array
        (
            [id] => 4
            [pid] => 2
            [link] => viz
            [subcat] =>
        )

    [script] => Array
        (
            [id] => 5
            [pid] => 2
            [link] => script
        )

    [arch] => Array
        (
            [id] => 6
            [pid] => 5
            [link] => arch
            [subcat] =>
        )

)
--- */
?>

thanks for the quick replies;

 

I failed to mention, the data is dynamic, thus identifying the keys in the array for the filtering process may not be practical.

 

The code from the php manual flattens the array completely.

I will post some code, but i have made so many variations to it that i usually delete and start afresh.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.