简体   繁体   中英

How to format arrays into one with new index in php

I tried to format my code as shown below. But failed to do so.

My criteria and my code is mentioned below.

Here is my array structure:

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [tagged_field] => description
                    [created_date] => 2015-02-06 14:47:44
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [tagged_field] => description_html
                    [created_date] => 2015-02-06 14:47:44
                )

            [1] => Array
                (
                    [tagged_field] => description_html
                    [created_date] => 2015-02-06 14:47:44
                )

        )

)

Desired array structure:

Array
(
    [0] => Array
        (
            [tagged_field] => description
            [created_date] => 2015-02-06 14:47:44
        )

    [1] => Array
        (
            [tagged_field] => description_html
            [created_date] => 2015-02-06 14:47:44
        )
[2] => Array
        (
            [tagged_field] => description_html
            [created_date] => 2015-02-06 14:47:44
        )
)

My code :

foreach($tagArr as $key => $value){
            $a_tag[$inc] = $value[0];
            $inc++;
        }

But was not able to format array like that.....

How can i sort my array as shown in desired format if my input array is mentioned as above.

Try with -

foreach($tagArr as $key => $value){
    foreach($value as $val) {
            $a_tag[] = $val;
    }
}
foreach($tagArr as $key => $value){
    $a_tag[] = array_values($value[0]);     
}

Use this

$a_tag = [];
foreach ($tagArr as $value) {
     $a_tag += $value;
}

http://php.net/manual/en/function.array-merge.php

You can do this by using the following code:

foreach($tagArr as $key => $value){
    foreach($value as $val) {
          $a_tag[] = $val;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM