我有以下代码:
// delete an existing app using the app path
public function deleteApp($app_path)
{
$content = file_get_contents('data.json');
$content = json_decode($content);
unset($content->apps->app_path);
dd($content);
}
输出结果为:
这意味着unset没有移除对象。
我的问题是,如何从$content->apps
删除条目?
用于测试的代码,如要求的那样
// --------------------------------------------- testCanDeleteApp()
public function testCanDeleteAnApp()
{
$local_data = new LocalData;
$app_name = $this->getRandomName();
$app_path = 'can/you/delete/me';
// first let's add the app and make sure it exists
$local_data->addNewApp($app_name, $app_path);
$this->assertTrue($local_data->isAppExists($app_path));
// then i just delete it.
$local_data->deleteApp($app_path);
$this->assertFalse($local_data->isAppExists($app_path));
}
答案 0 :(得分:5)
改为使用
// delete an existing app using the app path
public function deleteApp($app_path)
{
$content = file_get_contents('data.json');
$content = json_decode($content);
unset($content->apps->$app_path);
dd($content);
}
请注意,app_path $content->apps->$app_path
前面有一个$,而这将转换为$content->apps->the_path_you_passed_in_as_a_variable