可能重复:将
数组打印到文件
如何在文件中编写输出(例如当我使用数组时)?我试着这样做:
...
print_r($this->show_status_redis_server());
$status_redis = ob_get_contents();
fwrite($file, $status_redis);
...
Run Code Online (Sandbox Code Playgroud) 可能的重复:
如何从 PHP 中的多维数组中删除重复值
抱歉写得太快了,我正在工作……抱歉。现在我认为解释得很好。我想删除带有子元素 [title] common、equals 的子数组,我只想要一个元素,而不是重复,我该怎么做?我做到了,但我认为必须有一种更优雅的方式才能做到这一点。我尝试使用以下代码:
static function remove_duplicates_titles($deals){
$result = array();
$deal_repeated=false;
foreach ($deals as $index=>$deal) {
foreach ($deals as $deal_2) {
//discover if the subarray has the element title repeated is repeated or not
if ($deal['title'] == $deal_2['title']){
$deal_repeated=true;
unset($deal_2);
}
else{
$deal_repeated=false;
}
}
//if the array has no the element (title) repeated with another....
if(!$deal_repeated){
$result[]=$deal;
}
}
return $result;
Run Code Online (Sandbox Code Playgroud)
}
Array
(
[0] => Array
(
[id] => abc
[title] => bbb
) …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何使用对象方法作为回调函数
通常我使用带有过程代码的array_map,但在这种情况下我在OOP中工作,回调应该是"this-> id2areas",但它不起作用.反正用OOP进行回调吗?
ERROR MESSAGE: array_map() expects parameter 1 to be a valid callback, function 'this->id2area' not found or invalid function name
Run Code Online (Sandbox Code Playgroud)
我的代码================================================ =================================
$this->context->assign('user_areas', implode(', ', array_map('id2area', explode(',', $this->user['areas']))));
explode(',', $this->user['areas']))));
function id2area($id) {//callback
if ($id == 0) {
return 'National';
}
$query = "SELECT area FROM area WHERE id = $id";
return DB::fetch_instance()->slave->fetchColumn($query);
}
Run Code Online (Sandbox Code Playgroud)