如何在php中设置Unset后的值

时间:2014-10-20 07:12:13

标签: php arrays

我需要什么

  • 我首先取消设置后需要保留数组值。

这是数组结构(未设置之前)

    [0] => Array
    (
        [currency] => INR
    )

   [1] => Array
    (
        [type] => General Public Tickets  Adult
        [amount] => 50
        [comment] => (Working Days)
    )
   [2] => Array
    (
        [type] => General Public Tickets  Adult
        [amount] => 80
        [comment] => (Saturday/ Sunday/ Holiday)
    )

这是数组结构(未设置后)

    [1] => Array
    (
        [type] => General Public Tickets  Adult
        [amount] => 50
        [comment] => (Working Days)
    )
   [2] => Array
    (
        [type] => General Public Tickets  Adult
        [amount] => 80
        [comment] => (Saturday/ Sunday/ Holiday)
    )

这是php代码

                   unset($data[0]);
                 /* code to set the value after unset */
                   $data= array_values($data[0]);
  • 我需要将货币放入另一个数组
  • 首先我从数组中取消设置数组值[currency]。
  • 然后我希望[货币]放在另一个数组中。
  • 如何在从数组中取消设置后设置值。

2 个答案:

答案 0 :(得分:3)

//save element to a new variable
$temp = $data[0];

//unset element
unset($data[0]);

/* code to set the value after unset */

//add element back to the array
$data[0] = $temp;
        OR
//To add elements to the beginning of an array, use array_unshift
array_unshift($data,$temp);

答案 1 :(得分:1)

在新数组中取消设置之前,您需要获取货币值。如果您取消设置,之后您尝试访问它,则会导致未定义

试试这样:

             $arrNew = $data[0];  //save it new array
              unset($data[0]); //unset it now