Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an array like : `

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [productid] => 141
                    [fieldid] => 2
                    [value] => Laptop
                )

            [1] => Array
                (
                    [productid] => 191
                    [fieldid] => 2
                    [value] => Books
                )

            [2] => Array
                (
                    [productid] => 177
                    [fieldid] => 2
                    [value] => Printer
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [productid] => 141
                    [fieldid] => 1
                    [value] => 3
                )

            [1] => Array
                (
                    [productid] => 191
                    [fieldid] => 1
                    [value] => 4
                )

            [2] => Array
                (
                    [productid] => 177
                    [fieldid] => 1
                    [value] => 2
                )

        )

)`

I want this array to be modified and looks like the following :

Array
(
    [0] => Array
        (
            [productid] => 141
            [fieldid] => 2
            [value] => Laptop
        )

    [1] => Array
        (
            [productid] => 191
            [fieldid] => 2
            [value] => Books
        )

    [2] => Array
        (
            [productid] => 177
            [fieldid] => 2
            [value] => Printer
        )

    [3] => Array
        (
            [productid] => 141
            [fieldid] => 1
            [value] => 3
        )

    [4] => Array
        (
            [productid] => 191
            [fieldid] => 1
            [value] => 4
        )

    [5] => Array
        (
            [productid] => 177
            [fieldid] => 1
            [value] => 2
        )
)

Just remove the outer array and combine all chunks of array into one. Is this possible in php. Thanks in advance.

share|improve this question
up vote 1 down vote accepted

One approach could be this:

Lets say $items is your original array,

$new_items = array();
foreach($items as $item)
{
    $new_items = array_merge($new_items,array_values($item));
}
share|improve this answer
    
That works like a charm. Thanks. – samir chauhan Sep 21 '11 at 5:13

Yes you can use the following method to do that.Quite simple.

//let the older array be $array
var $newArray = array();  //new array(all itms will be taken to this array)      
foreach($array as $key->$value){
    foreach($value as $key->$innervalue){
            $newArray[] = $innervalue;
             }

    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.