Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在foreach中使用in_数组过滤关联数组_Php_Arrays_Associative Array - Fatal编程技术网

Php 在foreach中使用in_数组过滤关联数组

Php 在foreach中使用in_数组过滤关联数组,php,arrays,associative-array,Php,Arrays,Associative Array,我将JSON数组转换为PHP关联数组。我试图循环此关联数组,如果键与单独数组中的值匹配($filter),我想从关联数组中回显键和值 $filter = array("test1", "test"); foreach ($central as $key => $value) { $key = str_replace("_", " ", $key); if(in_array('$filter', $key)){ echo "<ul>".ucwords

我将JSON数组转换为PHP关联数组。我试图循环此关联数组,如果键与单独数组中的值匹配(
$filter
),我想从关联数组中回显
键和

$filter = array("test1", "test");

foreach ($central as $key => $value) {
   $key = str_replace("_", " ", $key); 
   if(in_array('$filter', $key)){
       echo "<ul>".ucwords($key).':' .' '. $value."</ul>";
    }
   else { 
       continue;
   }
}
$filter=array(“test1”、“test”);
foreach($key=>$value){
$key=str\u replace(“\u”,”,$key);
if(在_数组($filter',$key)中){
回声“
    ”.ucwords($key)。:“.”.$value.“
”; } 否则{ 继续; } }
综上所述,如果我的
$central
关联数组的
$key
等于我的
$filter
数组中的一个值,我想回显这些值。否则转到循环中的下一项。但是,此代码不起作用

语法错误,意外“=>”


我不确定我是否理解正确,但我认为你的“针”和“干草堆”是错误的。 另外,不需要else语句

foreach ($central as $key => $value) {
   $key = str_replace("_", " ", $key); 
   if(in_array($key, $filter)){
     echo "<ul>".ucwords($key).':' .' '. $value."</ul>";
   }
}
foreach($centralas$key=>$value){
$key=str\u replace(“\u”,”,$key);
if(在_数组中($key,$filter)){
回声“
    ”.ucwords($key)。:“.”.$value.“
”; } }
要在_array
函数中使用
,必须将要查找的键(指针)作为第一个参数插入,将数组作为第二个参数插入

有关更多信息,请参阅

下面是正确调用in_数组函数的代码

foreach ($central as $key => $value) {
    $key = str_replace("_", " ", $key); 
    if(in_array($key, $filter)){
        echo "<ul>".ucwords($key).':' .' '. $value."</ul>";
    }
}
foreach($centralas$key=>$value){
$key=str\u replace(“\u”,”,$key);
if(在_数组中($key,$filter)){
回声“
    ”.ucwords($key)。:“.”.$value.“
”; } }

请注意,
continue
语句是无用的,因为它位于
for
循环的末尾。

请阅读有关的手册并查看:(+错误不可复制:)查看
$central
数组的示例也很有用,因为用空格替换
\u
可能也不会生成与
$filter
数组内容匹配的内容