Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Arrays 如何在foreach循环中使用array\u replace?_Arrays_Loops_Symfony_Search_Replace - Fatal编程技术网

Arrays 如何在foreach循环中使用array\u replace?

Arrays 如何在foreach循环中使用array\u replace?,arrays,loops,symfony,search,replace,Arrays,Loops,Symfony,Search,Replace,哥伦比亚: array:4 [▼ 0 => "id" 1 => "name" 2 => "productgroup" 3 => "category" ] 字段: {#9767 ▼ +"id": array:9 [▶] +"name": array:8 [▼ "fieldName" => "name" ] +"productgroup": array:19 [▼ "fieldName" => "produ

哥伦比亚:

array:4 [▼
  0 => "id"
  1 => "name"
  2 => "productgroup"
  3 => "category"
]
字段:

    {#9767 ▼
  +"id": array:9 [▶]
  +"name": array:8 [▼
    "fieldName" => "name"
  ]
  +"productgroup": array:19 [▼
    "fieldName" => "productgroup"
    "mappedBy" => null
  ]
  +"category": array:19 [▼
    "fieldName" => "category"
    "mappedBy" => null
  ]
}
我想做的是,每当元素的
字段中存在
mappedBy时,我想将
name
添加到值中。因此,
列\u arr
应如下所示:

array:4 [▼
  0 => "id"
  1 => "name"
  2 => "productgroup.name"
  3 => "category.name"
]
这是我的方法:

  foreach ($fields as $field) {
      $MappedBy = isset($field['mappedBy']);
        if($MappedBy != true){
          $class_field = $field['fieldName'];
          $key = array_search($field['fieldName'],$input);
          $replace=array($key=>$class_field.".name");
          $columns_arr = (array_replace($input,$replace));
      }
    }
问题是,我现在的结果是:

array:4 [▼
  0 => "id"
  1 => "name"
  2 => "productgroup"
  3 => "category.name"
]
为什么
name
未添加到
productgroup

foreach ($fields as $key => $field) {
    $MappedBy = isset($field['mappedBy']);
      if($MappedBy != true){
        $columns_arr[$key] = $field['fieldName'].".name";
    }
  }

您需要像这样覆盖数组的正确键。

我无法真正回答您的问题,有点不清楚您在做什么以及值来自何处等。因此,我尝试重建您的数组,这是我在不使用数组搜索的情况下得到的结果:

<?php

$columns = array(0 => "id", 1 => "name", 2 => "productgroup", 3 => "category");

$fields = array("id" => 9, "name" => array("fieldName" => "name"), "productgroup" => array("fieldName" => "productgroup", "mappedBy" => null), "category" => array("fieldname" => "category", "mappedBy" => null));
foreach ($fields as $key => $field) {
    if($key !== "id" && $key !== "name"){
        if(!isset($field['mappedBy'])){
            foreach($columns as $ckey => $column){
                if($column === $key){
                    $columns[$ckey] = $column.".name";
                }
            }
        }
    }
}

var_dump($columns);

$columns\u arr[]=(数组替换($input,$replace))
@Fabian您的建议给了我输出
数组:6[▼   0=>“id”1=>“名称”2=>“产品组”3=>“类别”4=>数组:4[▼     0=>“id”1=>“名称”2=>“产品组.名称”3=>“类别”]5=>数组:4[▼     0=>“id”1=>“name”2=>“productgroup”3=>“category.name”]
看看我的答案我测试过了。输出为:
array:4[▼   0=>“id”1=>“名称”2=>数组:4[▼     0=>“id”1=>“名称”2=>“产品组.名称”3=>“类别”]3=>数组:4[▼     0=>“id”1=>“name”2=>“productgroup”3=>“category.name”]
现在输出为:
数组:6[▼   0=>“id”1=>“名称”2=>“产品组”3=>“类别”“产品组”=>“产品组.名称”“类别”=>“类别.名称”]
什么是
$input
?$input=array\u键($columns);这太难了,我只是试着做这个测试
foreach($fields as$field){$columns\u arr[array\u search($field,$columns\u arr)]='test';}
,我希望所有的值都变成
test
。但只有
id
被替换。。。
array(4) { [0]=> string(2) "id" [1]=> string(4) "name" [2]=> string(17) "productgroup.name" [3]=> string(13) "category.name" }