Question

TL;DR

I have this data: var_export and print_r.

And I need to narrow it down to: http://pastebin.com/EqwgpgAP ($data['Stock Information:'][0][0]);

How would one achieve it? (dynamically)


I'm working with vTiger 5.4.0 CRM and am looking to implement a function that would return a particular field information based on search criteria.

Well, vTiger is pretty weakly written system, looks and feels old, everything comes out from hundreds of tables with multiple joins (that's actually not that bad) etc., but job is job.

The need arose from getting usageunit picklist from Products module, Stock Information block.

Since there is no such function as getField();, I am looking forward to filter it out from Blocks, that is actually gathering the information about fields also.

getBlocks(); then calls something close to getFields();, that again something close to getValues(); and so on.

So...

$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)

As you can see, the array under the key [Stock Information:], that actually comes out from translations (yada, yada...), under [0][0] contains information for usageunit.

Now, I was trying to array_filter(); the data out from there, but only thing I've managed to get is $productsBlocks stripped down to only contain [Stock Information:] with all the data:

$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)

And, the result I'm looking forward to is http://pastebin.com/EqwgpgAP, that I've manually got by print_r($productsUsageUnit['Stock Information:'][0][0]);.


How do I achieve this? (dynamically...)

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top