在一组深度嵌套的php数组中找到键并返回true

在一组深度嵌套的php数组中找到键并返回true,php,arrays,Php,Arrays,所以有很多这样的问题,但我还没有找到一个适合我期望结果的答案 考虑以下数组: array:13 [ "Peripheral Neuropathy" => array:3 [ "name" => "peripheral_neuropathy" "value" => array:1 [ 0 => "yes" ] "dependencies" => array:5 [ "neuropathy_staging_fa

所以有很多这样的问题,但我还没有找到一个适合我期望结果的答案

考虑以下数组:

array:13 [
  "Peripheral Neuropathy" => array:3 [
    "name" => "peripheral_neuropathy"
    "value" => array:1 [
      0 => "yes"
    ]
    "dependencies" => array:5 [
      "neuropathy_staging_fap" => array:1 [
        0 => "2"
      ]
      "neuropathy_staging_pnd" => array:1 [
        0 => "II"
      ]
      "specify_type" => array:1 [
        0 => "Sensory"
      ]
      "autonomic" => array:1 [
        0 => "yes"
      ]
      "sensory_fiber_size" => array:1 [
        0 => "Small"
      ]
    ]
  ]
  "Neuropathic pain" => array:3 [
    "name" => "neuropathic_pain"
    "value" => array:1 [
      0 => "yes"
    ]
    "dependencies" => []
  ]
  "Functional Motor Assessment" => array:3 [
    "name" => "functional_motor_assessment"
    "value" => array:1 [
      0 => "Yes"
    ]
    "dependencies" => array:3 [
      "functional_motor_assessment_test_name" => "sample"
      "functional_motor_assessment_test_score" => "10"
      "functional_motor_assessment_date" => "2020-02-11"
    ]
  ]
  "Assessment name" => array:3 [
    "name" => "functional_motor_assessment_test_name"
    "value" => "sample"
    "dependencies" => []
  ]
  "Assessment Score" => array:3 [
    "name" => "functional_motor_assessment_test_score"
    "value" => "10"
    "dependencies" => []
  ]
  "Assessment Date" => array:3 [
    "name" => "functional_motor_assessment_date"
    "value" => "2020-02-11"
    "dependencies" => []
  ]
  "Carpal Tunnel Syndrome" => array:3 [
    "name" => "carpal_tunnel_syndrome"
    "value" => array:1 [
      0 => "yes"
    ]
    "dependencies" => []
  ]
  "EMG" => array:3 [
    "name" => "emg"
    "value" => array:1 [
      0 => "yes"
    ]
    "dependencies" => array:5 [
      "emg_type" => array:1 [
        0 => "Median"
      ]
      "median_amplitude" => "10"
      "median_CV" => "10"
      "median_tml" => "10"
      "median_size" => array:1 [
        0 => "7cm"
      ]
    ]
  ]
  "EMG Type" => array:3 [
    "name" => "emg_type"
    "value" => array:1 [
      0 => "Median"
    ]
    "dependencies" => array:4 [
      "median_amplitude" => "10"
      "median_CV" => "10"
      "median_tml" => "10"
      "median_size" => array:1 [
        0 => "7cm"
      ]
    ]
  ]
  "Amplitude" => array:3 [
    "name" => "median_amplitude"
    "value" => "10"
    "dependencies" => []
  ]
  "CV" => array:3 [
    "name" => "median_CV"
    "value" => "10"
    "dependencies" => []
  ]
  "TML" => array:3 [
    "name" => "median_tml"
    "value" => "10"
    "dependencies" => []
  ]
  "Size" => array:3 [
    "name" => "median_size"
    "value" => array:1 [
      0 => "7cm"
    ]
    "dependencies" => []
  ]
]
正如我们可以看到的,有重复项,例如看:依赖项下的功能性运动评估-这是正确的,但直接在功能性运动评估下的是评估名称,名称为Functional_Motor_Assessment_test_name

这是副本。此特定数组、评估名称不应存在,因为该名称已存在于功能性运动评估的依赖项数组中

所以我想,我将编写以下函数:

protected function alreadyExists(array $values, string $fieldName) {
    if (empty($values)) {
        return false;
    }

    foreach ($values as $key => $value) {
        foreach ($value as $k => $v) {
            if ($k === 'dependencies' && !empty($value[$k])) {
                return array_key_exists($fieldName, $value[$k]);
            }
        }
    }

    return false;
}
其中,$value是上面的数组,在这种情况下,$fieldName将是例如功能\电机\评估\测试\名称

这里的想法是,这应该遍历数组,查找匹配的任何键:functional\u motor\u assessment\u test\u name,如果找到,则返回true;如果上述数组为空,则返回false,否则返回false

有两条规则:

如果值数组为空,则返回false,因为它显然不存在。 如果从未找到,则返回false 跳过检查,如果依赖项数组为空,则有时可能为空。 这是它应该转移到下一个阵列的地方,所以如果在周围神经病变中没有发现,就转移到神经性疼痛等等。。。 我认为这个函数必须是递归的,但我不确定递归的方面应该放在哪里,也就是说:我在周围神经病变中没有发现它,让我们检查一下神经病理性疼痛等等。直到找到或没有

我尝试了array\u walk\u recursive,但正如我在阅读文档时所预期的那样,没有办法脱离这种函数-所以我想,我拥有的这个函数是正确的,我只需要使它递归


想法?

无需循环第二级数组,只需通过索引直接获取dependencies元素即可

public function alreadyFound(array $values, string $fieldName) {
    foreach ($values as $item) {
        if (!empty($item['dependencies']) && array_key_exists($fieldName, $item['dependencies'])) {
        return true;
    }
    return false;
}
如果依赖项中可以嵌套依赖项,则确实需要递归解决方案

protected function alreadyExists(array $values, string $fieldName) {
    if (array_key_exists($fieldName, $values)) {
        return true;
    }

    foreach ($values as $item) {
        if (!empty($item['dependencies']) && $this->alreadyExists($item['dependencies'], $fieldName) {
            return true;
        }
    }

    return false;
}

使用foreach循环。对当前元素进行递归调用,如果成功,可以立即返回,否则继续循环;一个用于获取所有依赖项,另一个用于将每个名称与您刚刚找到的依赖项相匹配。我假设依赖项可以嵌套得更深。但依赖项结构与顶级元素完全不同,不过: