2011-05-06 66 views
0

HI,如何获得单个数组的结果?

,这是我的阵列中的一个变量

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 
       ) 

我想转换进入

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 
       ) 
) 

如何使用foreach循环/ for循环得到的结果。

感谢

回答

1

简单foreach环路和连接的阵列:

$result = array(); 

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

这将合并键尽管如此,所以最终只会在'$ result'中取两个项目,而不是预期的3. – 2011-05-06 12:06:49

+0

@ Olivier:是。我认为它只是附加它并重新索引数字键。更改为'array_merge' ...谢谢! – 2011-05-06 12:08:58

3

什么

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

这工作,并已通过测试:

$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); 
相关问题