PHP关联数组&取消设置

时间:2015-04-11 21:37:09

标签: php arrays

我创建了一个irc bot,我有一个频道数组($ BotChannels = ['频道' => [],'命令' => [ ],'输入' => []];) 当添加新频道并删除旧频道时以及当删除频道时,我会使用for来捕捉频道位置,但是当我使用unset函数从数组中删除频道时,我总是得到一个下面的数组代码上的空白区域。

if(count($BotChannels['Channel']) > 0) {
            for($i = 0; $i < count($BotChannels['Channel']); $i++) {
                if(!in_array($BotChannels['Channel'][$i], $TempChan)) {
                    IrcMsg($BotChannels['Channel'][$i], 'Channel removed from database, leaving.');
                    PartChannel($BotChannels['Channel'][$i]);
                    unset($BotChannels['Channel'][$i], $BotChannels['Commands'][$i] ,$BotChannels['Type'][$i]);
                }
            }
            unset($TempChan);
        }

1 个答案:

答案 0 :(得分:0)

首先,您的数组结构为&#34; $ BotChannels&#34;看起来它不支持持有SQL结果的人。

因此我建议结构如下所示。

$BotChannels = [
  'key' => [  # "key" is your variable $i
     'Channel' => [],
     'Commands' => [],
     'Type' => []
  ],
];

至于你的算法

if(count($BotChannels) > 0) {
   foreach (array_keys($BotChannels) as $key) { 
      if(!in_array($key, $TempChan)) {
      IrcMsg($key, 'Channel removed from database, leaving.');
      PartChannel($key);
      unset($BotChannels[$key]);
   }
   unset($TempChan);
}

如果您坚持不更改阵列&#34; $ BotChannels&#34;,请确保正确填充SQL结果,如何填充。

echo '<pre>'.print_r($BotChannels, 1).'</pre>'; # TIP