我是一名PHP初学者,遇到以下问题,我完全陷入困境。我有一个这种格式的数组
Array (
[date] => 0
[author] => 1
[categories] => 1
[tags] => 0
[comments] => 0
[readmore] => 0
)
现在我需要从该数组中删除[value] => 0
的所有项目并以此格式获取
Array (
[0] => author
[1] => categories
)
非常感谢您的任何意见。
答案 0 :(得分:1)
$array = array ( 'date' => 0, 'author' => 1, 'categories' => 1, 'tags' => 0, 'comments' => 0, 'readmore' => 0 );
$array = array_filter($array);
print_r(array_keys($array));
<强>输出强>
Array
(
[0] => author
[1] => categories
)
答案 1 :(得分:1)
那里有一个名为array_search()
的函数。假设唯一可能的值是0
和1
,您可以只搜索整个数组以获取此值:
$array = [
'date' => 0,
'author' => 1,
'categories' => 1,
'tags' => 0,
'comments' => 0,
'readmore' => 0
];
$new_array = array_flip(array_search(1, $array));
因此print_r($new_array)
会为您提供所需的结果。
答案 2 :(得分:0)
您可以尝试这样的事情:
foreach($array as $item){
if($item == "0"){
unset($item);
}
}
使用unset函数进行一些修改。
答案 3 :(得分:0)
这也是做的......
$dataSource = array('data' => 0,
'author' => 1,
'categories' => 1,
'tags' => 0,
'comments' => 0,
'readmore' => 0,
);
$updatedDataList = array();
foreach ($dataSource as $key => $value) {
if($value !== 0) {
$updatedDataList[] = $key;
}
}
print_r($updatedDataList);
答案 4 :(得分:0)
您可以尝试以下代码
$yourArray = Array ("date" => 0,"author" => 1, "categories" => 1,"tags" => 0, "comments" => 0,"readmore" => 0 );
$removeZeroFromArray = array(0);
$desiredResult = array_diff($yourArray, $removeZeroFromArray);
echo"<pre>";
print_r(array_keys($desiredResult));
echo"</pre>";
您的输出就像
Array
(
[0] => author
[1] => categories
)
替代方式应该是
$desiredResult = array_filter($yourArray, function($a) { return ($a !== 0); });
希望它有所帮助!
答案 5 :(得分:0)
@savethegold你可以像下面那样保存你的金币:D
{{ Form::model($books, ['route' => ['admin.book.edit', $books->id], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'PATCH']) }}
<!--form content-->
{{ Form::close() }}
答案 6 :(得分:0)
<?php
$array = array ( 'date' => 0, 'author' => 1, 'categories' => 1, 'tags' => 0, 'comments' => 0, 'readmore' => 0 );
$resutl = [];
foreach($array as $k => $v)
{
if($v != 0)
$result[] = $k;
}
print_r($result);