0

I have a problem When I want To rebuild my array . this is I am create on phpfiddle.org

http://phpfiddle.org/main/code/tzp-t1u

anyone can give me suggestion ?

6
  • 1
    You may need to provide a little more information; - what are the rules around merging the arrays (is there a common key, is there a value that match?) - are there always just 3 indexes to start with, etc. Commented Feb 21, 2014 at 4:33
  • you would have to iterate throw the array. Array merge doesn't merge values of existing keys. it just mergers two array together and leaves duplicate keys untouched Commented Feb 21, 2014 at 4:34
  • Post your array generating code with few sample array, I really can't get what is your output & what you want it to be. Commented Feb 21, 2014 at 4:35
  • I am updated my question Commented Feb 21, 2014 at 4:50
  • What is your matching criteria for the arrays, how do you group the child key? Commented Feb 21, 2014 at 5:00

1 Answer 1

1

This code will help you

<?php

$result = array (
        array(
                'lev1'=> null,
                'lev2'=> 34,
                'lev3'=> 21,
                'lev4'=> 22,
                'child' => 'cyaiooo'
        ),
        array(
                'lev1'=> null,
                'lev2'=> 34,
                'lev3'=> 10,
                'lev4'=> 8,
                'child' => 'test1'
        ),
        array(
                'lev1'=> null,
                'lev2'=> 34,
                'lev3'=> 21,
                'lev4'=> 22,
                'child' => 'hodem'
        )
);

$data = array();

foreach($result as $value)
{
    if(count($data)==0)
    {
        $data[] = $value;

    }else{

        foreach($data as &$container)
        {
            if($value['lev1']==$container['lev1']
                    &&
                $value['lev2']==$container['lev2']
                    &&
                $value['lev3']==$container['lev3']
                    &&
                $value['lev4']==$container['lev4'])
            {               

                if(is_string($container['child']))
                {
                    //get the string value
                    $string = $container['child'];

                    //unset the child string
                    unset($container['child']);

                    //declatre the array
                    $container['child'] = array();

                    $container['child'][] =  $string;
                }

                $container['child'][] =  $value['child'];

                break;

            }else{
                $data[] = $value;
                break;
            }
        }

    }
}

print_r($data);
Sign up to request clarification or add additional context in comments.

2 Comments

that for your answer really I am appreciate, that are really almost like i want, but the value of children result as string, in my goal the value is array .
@viyancs you can try little to achieve the result. Anyway I modified please check the updated answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.