Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

PHP 陣列 配合explode,implode

我想在SQL裡做個權限表

例如

35|12|36|88|1

就是35,12,36,88,1

這些人可以有XX權限

管理員在發權限時

就在上面加個xx|就好

檢查權限時

$users= " 35|12|36|88|1 ";

$authority=explode( "|" , $users );

現在authority變成陣列了吧? (我只是第一次溝想,沒真正嘗試過)

if ( !in_arra y( $my_serial , $authority ) ){

die("權限不足");

}

但是

現在假如我要刪除12號的權限

要怎麼做呢

如何把$authority陣列裡的12去掉?

還有

如何把像這樣$authority陣列裡的每筆資料一一列出來

例如

[1]:35

[2]:12

[3:]36

...

Update:

是要用unset先把陣列裡的東西刪掉嗎

可是我要刪掉12的話

我要怎麼用程式判斷

他是陣列的第幾個?

Update 2:

@Chris

找不到array_serch這個函數耶

是不是現在的版本移除了?

Update 3:

@Chris

是的Appserv

Update 4:

@Chris

你可以用回答的嗎

因為等問題解決了我想給你點數:)

Update 5:

了解了解了

多謝多謝!!

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    可使用array_search

    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

    $key = array_search('green', $array); // $key = 2;

    $key = array_search('red', $array); // $key = 1;

    2010-04-05 18:58:01 補充:

    unset($authority[array_search('12', $authority)]); 大概是如此吧 沒試過

    2010-04-05 19:00:44 補充:

    [1]:35

    [2]:12

    [3:]36

    foreach ($authority as $k=>$v){

    echo '['.$k.']:'.$v.'<br>';

    }

    試試這個...沒有debug過...

    2010-04-05 19:04:35 補充:

    <>改半形..

    foreach ($authority as $k=>$v){

    echo '['.($k+1).']:'.$v.'<br>';

    }

    2010-04-05 19:39:51 補充:

    http://tw.php.net/manual/en/function.array-search.... 有呀..

    你是用appserv嗎??

    2010-04-05 22:22:11 補充:

    可使用array_search

    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

    $key = array_search('green', $array); // $key = 2;

    $key = array_search('red', $array); // $key = 1;

    做法

    unset($authority[array_search('12', $authority)]);

    [1]:35

    [2]:12

    [3:]36

    <>改半形..

    foreach ($authority as $k=>$v){

    echo '['.($k+1).']:'.$v.'<br>';

    }

    參考

    http://tw.php.net/manual/en/function.array-search....

    2010-04-05 22:22:39 補充:

    用貼的回答了 謝謝唷..

Still have questions? Get your answers by asking now.