0

我有一个初始数组:

array
( [no] => array 
          (
             [<30] => 3
             [>30] => 5
             [>50] => 2
          )
  [yes] => array
          (
             [<30] => 4
             [>30] => 2
             [>50] => 7
          ) 
  [maybe] => array
          (
             [<30] => 7
             [>30] => 9
             [>50] => 10
          ) 
)

上述数组是否可以通过yes、no 和maybe 拆分成多个数组变量。如 :

$yes = array(3,5,2);
$no = array(4,2,7);
$maybe = array(7,9,10);
4

4 回答 4

1
$yes   = $array['yes'];
$no    = $array['no'];
$maybe = $array['maybe'];

这里的所有都是它的。

于 2013-02-13T01:35:08.853 回答
1

你可以试试这个。

$result = array();

foreach($data as $response)
    $result[] = array_values($response);

list($no, $yes, $maybe) = $result;

$data您的问题中显示的数组在哪里。

然后,您可以从 、 和 中访问 3 个值$no$yes$maybe假设 的顺序与该顺序$data相对应。

于 2013-02-13T01:41:58.833 回答
1

假设内部数组以相同的顺序填充相同的键,看起来你想看看 array_values:http://php.net/manual/en/function.array-values.php

从那开始,它应该很简单:

$yes   = array_values( $initial_array['yes'] );
$no    = array_values( $initial_array['no'] );
$maybe = array_values( $initial_array['maybe'] );
于 2013-02-13T01:46:35.443 回答
0

All the other solution given for this question depends on the name of the key, if the key changes from yes to YES, none of the solution works, Here is the dynamic solution for it, simple too.

$array   = array("yes"=>array(1,2,3,4),"no"=>array(3,45,6,),"maybe"=>array(7,8,9));

foreach($array as $key=>$val){
   $key      = strtolower($key);
   $$key     = $val;
}

print_r($yes);
echo "<br>";
print_r($no);
echo "<br>";
print_r($maybe);
于 2013-02-13T05:23:08.577 回答