简体   繁体   中英

how to get result in single array?

HI,

this is my array coming in a variable

Array
(
    [msg] => Array
        (
            [0] => Array
                (
                    [alertId] => 2416
                    [alerttitle] => Raven Lexy
                    [alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
                    [alertDescription] => (1) New Message(s)
                    [alertType] => New Message
                    [Date] => 1304679217
                    [count] => 1
                )

        )

    [rehp] => Array
        (
            [0] => Array
                (
                    [alertId] => 48
                    [alerttitle] => Artin
                    [alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
                    [alertDescription] => Reply From Artin
                    [alertType] => Reply To Hotpress
                    [count] => 1
                    [id] => 48
                )

            [1] => Array
                (
                    [alertId] => 48
                    [alerttitle] => Artin
                    [alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
                    [alertDescription] => Reply From Artin
                    [alertType] => Reply To Hotpress
                    [count] => 1
                    [id] => 48
                )

i want to convert into

Array
(

            [0] => Array
                (
                    [alertId] => 2416
                    [alerttitle] => Raven Lexy
                    [alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
                    [alertDescription] => (1) New Message(s)
                    [alertType] => New Message
                    [Date] => 1304679217
                    [count] => 1
                )

            [1] => Array
                (
                    [alertId] => 48
                    [alerttitle] => Artin
                    [alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
                    [alertDescription] => Reply From Artin
                    [alertType] => Reply To Hotpress
                    [count] => 1
                    [id] => 48
                )

            [2] => Array
                (
                    [alertId] => 48
                    [alerttitle] => Artin
                    [alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
                    [alertDescription] => Reply From Artin
                    [alertType] => Reply To Hotpress
                    [count] => 1
                    [id] => 48
                )
)

how can i use foreach loop/for loop to get the the result.

thanks

关于什么

$new_array = array_merge($orig["msg"],$orig["rehp"])

Simple foreach loop and concatenating the arrays:

$result = array();

foreach($array as $a) {
    $result = array_merge($result, $a);
}

This works and has been tested:

$a = Array(
    "msg" => Array
        (
            0 => Array
                (
                    "alertId" => 2416,
                    "alerttitle" => "Raven Lexy",
                    "alertImageUrl" => "photos/81951b37ad01c4aa65662956f178214eth.jpeg",
                    "alertDescription" => "(1) New Message(s)",
                    "alertType" => "New Message",
                    "Date" => 1304679217,
                    "count" => 1
                )

        ),

    "rehp" => Array
        (
            0 => Array
                (
                    "alertId" => 48,
                    "alerttitle" => "Artin",
                    "alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
                    "alertDescription" => "Reply From Artin",
                    "alertType" => "Reply To Hotpress",
                    "count" => 1,
                    "id" => 48
                ),

            1 => Array
                (
                    "alertId" => 48,
                    "alerttitle" => "Artin",
                    "alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
                    "alertDescription" => "Reply From Artin",
                    "alertType" => "Reply To Hotpress",
                    "count" => 1,
                    "id" => 48,
                )
    )
);

$b = array();
foreach ($a as $v)
{
    foreach ($v as $i)
        $b[] = $i;
}

print_r($b);

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