多维数组搜索以保留父级


Multi-dimensional array search to preserve parent

TL;DR

我有这些数据:var_export和print_r。

我需要将其缩小到:http://pastebin.com/EqwgpgAP($data['股票信息:'][0][0]);

如何实现它?(动态)

<小时 />

我正在使用vTiger 5.4.0 CRM,并希望实现一个功能,该函数将根据搜索条件返回特定的字段信息。

好吧,vTiger 是一个编写得很弱的系统,看起来和感觉都很旧,所有内容都来自数百个具有多个连接的表(这实际上还不错)等,但工作就是工作。

需要从产品模块,股票信息块获取usageunit选择列表。

由于没有getField();这样的功能,我期待着从块中过滤掉它,这实际上也在收集有关字段的信息。

然后getBlocks();称接近getFields();的东西,再次接近getValues();的东西,依此类推。

所以。。。

$focus = new $currentModule(); // Products
$displayView = getView($focus->mode);
$productsBlocks = getBlocks($currentModule, $displayView, $focus->mode, $focus->column_fields); // in theory, $focus->column_fields should/could be narrowed down to my specific field, but vTiger doesn't work that way
echo "<pre>"; print_r($productsBlocks); echo "</pre>"; // = http://pastebin.com/3iTDUUgw (huge dump)

如您所见,键下的数组 [Stock Information:] ,实际上是从翻译(yada,yada...)中出来的,在[0][0]下包含usageunit的信息。

现在,我试图从那里array_filter();出数据,但我唯一设法得到的是$productsBlocks剥离,只包含包含所有数据的[Stock Information:]

$getUsageUnit = function($value) use (&$getUsageUnit) {
    if(is_array($value)) return array_filter($value, $getUsageUnit);
    
    if($value == 'usageunit') return true;
};
            
$productsUsageUnit = array_filter($productsBlocks, $getUsageUnit);
echo "<pre>"; print_r($productsUsageUnit); echo "</pre>"; // = http://pastebin.com/LU6VRC4h (not that huge of a dump)

而且,我期待的结果是 http://pastebin.com/EqwgpgAP,我已经手动print_r($productsUsageUnit['Stock Information:'][0][0]);

.<小时 />

我如何实现这一点?(动态...

function helper($data, $query) {
  $result = array();
  $search = function ($data, &$stack) use(&$search, $query) {
    foreach ($data as $entry) {
      if (is_array($entry) && $search($entry, $stack) || $entry === $query) {
        $stack[] = $entry;
        return true;
      }
    }
    return false;
  };

  foreach ($data as $sub) {
    $parentStack = array();
    if ($search($sub, $parentStack)) {
      $result[] = $parentStack[sizeof($parentStack) - 2];
    }
  }
  return $result;
}
$node = helper($data, 'usageunit');
print_r($node);