簡體   English   中英

如何在php中取消設置后設置值

[英]How To Set The value after Unset in php

我需要的

  • 我需要先取消設置后保留數組值。

這是數組結構(未設置之前)

    [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]。
  • 那我想[貨幣]放在另一個數組中。
  • 從數組取消設置后如何設置值。
//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);

您需要先獲取貨幣值,然后才能在新數組中將其取消設置。 如果您取消設置,然后嘗試訪問它,則會導致未定義

嘗試這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM