-2

我有一个格式为:

 array (
      [0] => array (
        [0] => array (
          [id] =>  19
          [Type] =>  sng
          [TouristPrice] =>  2500
          [ExtraBeds] =>  1500
          [ChildPrice] => 1000
        )
      )
    )

我想将其转换为:

array (
    [0] => array (
      [id] =>  19
      [Type] =>  sng
      [TouristPrice] =>  2500
      [ExtraBeds] =>  1500
      [ChildPrice] => 1000
    )  
)

我该怎么做呢?

4

1 回答 1

1

您可以做的一件事是,遍历数组并以这种方式放置第一个选项,以防所有数组项都属于同一类型,只有一层深度。就像将文件夹内的文件夹移动到父文件夹一样:

foreach ($array as $subarray)
    $subarray = $subarray[0];

试过这段代码:

<?php
    $array = array(
        array(
            array(
                "Hello",
                "World"
            )
        ),
        array(
            array(
                "Hello One",
                "World One"
            )
        ),
    );
    var_dump($array);
    foreach ($array as $subarray)
        $subarray = $subarray[0];
    var_dump($array);
?>

得到这个输出:

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(5) "Hello"
      [1]=>
      string(5) "World"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(9) "Hello One"
      [1]=>
      string(9) "World One"
    }
  }
}
array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(5) "Hello"
      [1]=>
      string(5) "World"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(9) "Hello One"
      [1]=>
      string(9) "World One"
    }
  }
}
于 2013-01-31T08:07:48.567 回答