Hi, how do i remove an array from config? The problem is that, the name can't be found in the config. Thanks PHP: <?php/* * Copyright (C) 2017 RTG * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */namespace RTG\WorldBorder;use pocketmine\Server;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;class Loader extends PluginBase implements Listener { public $config; public $save = array(); public function onEnable() { parent::onEnable(); $this->config = new \pocketmine\utils\Config($this->getDataFolder() . "config.yml", \pocketmine\utils\Config::YAML, array( "worlds" => array() )); } public function onAdd(\pocketmine\Player $p, $name, $blocks) { $this->config->set($name, array('blocks' => $blocks)); $this->config->save(); } public function onRM($name) { if ($this->config->get($name)) { echo 'Found!'; } } public function onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $command, $label, array $args) { switch (strtolower($command->getName())) { case "add": $this->onAdd($sender, "world", "6"); $sender->sendMessage("added!"); return true; break; case "rm": $this->onRM("world"); $sender->sendMessage("Removed!"); return true; break; } } public function onDisable() { parent::onDisable(); } }
You probably mean this: PHP: $file = $this->config; $data = $file->get("Your Data", []); $id = array_search($YourObject, $data); unset($data[$id]); $file->set("Your Data", $data); $file->save(); $file->reload();
I've updated my code, i want to know how to set arrays to worlds and remove: Config shd be like this: Code: worlds: level: blocks: 7 newlevel: blocks: 5
Maybe this: PHP: $array = $this->config->get("worlds");$new = $YourNewStuff;array_push($array, $new);$this->config->set("worlds", $array);$thie->config->save();// This should work when adding something to worlds array// Haven't tested it but it should work
code and expected result mismatch please make sure both the code and the config is produced in the same version... expecting world: - 6
@Az928 Hey, how can i get the specific world name? My code for the add thing: PHP: public function onAdd($name, $blocks) { $array = $this->config->get("worlds"); $new = array($name => array("blocks" => $blocks)); $data = $new; array_push($array, $data); $this->config->set("worlds", $array); $this->config->save(); } Config: Code: --- worldborder: true message: This is end of world worlds: - City: blocks: "2000" - City: blocks: "2000" ...
your over thinking it just do Code: // GET THE INFO OF CERTAIN CONFIG LOCATION $config->get("worlds")["City"]["blocks"]; // ADD A 'array' to config $config->setNested("worlds.City.blocks", $array);
I get this error: Code: Notice: Undefined index: city in C:\Users\RTG\Desktop\TesterUnit\plugins\plugin-beta-worldborder\src\WorldBorder\WorldBorder.php on line 118 Code: PHP: public function onRM($name) { $name = strtolower($name); if ($this->config->get("worlds")[$name]) { // Line 118 echo "Found"; } }
PHP: public function onRM(string $name) { //$name = strtolower($name); #config is CaSe sensitive var_dump($this->config->getAll()); // what's actually in the config? if (($worlds = $this->config->get("worlds", null)) != null and array_key_exists($name, $worlds)) { // Line 118 var_dump($worlds[$name]); echo "\n\nFound!\n"; } }
Hi this is my current state: PHP: <?php/* * Copyright (C) 2017 RTG * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */namespace RTG\WorldBorder;use pocketmine\plugin\PluginBase;use pocketmine\utils\Config;class Main extends PluginBase { public $config; public function onEnable() { $this->config = new Config($this->getDataFolder() . "config.yml", Config::YAML, array('worlds' => array())); $this->onAdd("world", '1'); print_r($this->config); $this->onRM("world"); } public function onAdd($name, $blocks) { $array = $this->config->get("worlds"); $name = strtolower($name); $new = array($name => array("blocks" => $blocks)); $data = $new; array_push($array, $data); $this->config->set("worlds", $array); $this->config->save(); } public function onRM($name) { $array = $this->config->get("worlds"); unset($array[array_search($name, $array)]); $this->config->set("worlds", $array); $this->config->save(); }} Last config state: Code: --- worlds: 1: world: blocks: "1" 2: world: blocks: "1" 3: world: blocks: "1" ... Print_r() Error: Code: pocketmine\utils\Config Object ( [config:pocketmine\utils\Config:private] => Array ( [worlds] => Array ( [1] => Array ( [world] => Array ( [blocks] => 1 ) ) [2] => Array ( [world] => Array ( [blocks] => 1 ) ) [3] => Array ( [world] => Array ( [blocks] => 1 ) ) [4] => Array ( [world] => Array ( [blocks] => 1 ) ) ) ) [nestedCache:pocketmine\utils\Config:private] => Array ( ) [file:pocketmine\utils\Config:private] => C:\Users\RTG\Desktop\TesterUnit\plugins\Test/config.yml [correct:pocketmine\utils\Config:private] => 1 [type:pocketmine\utils\Config:private] => 2 [jsonOptions:pocketmine\utils\Config:private] => 130 )
Can you post the entire source code on github? It'll be much easier for people to help than trying to figure the code you are running from the extracts above.