There is an array:

[my:МоиПоля] => Array ( [attr] => Array ( [xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance [xmlns:pc] => http://schemas.microsoft.com/office/infopath/2007/PartnerControls [xmlns:ma] => http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes [xmlns:d] => http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields [xmlns:q] => http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields [xmlns:dfs] => http://schemas.microsoft.com/office/infopath/2003/dataFormSolution [xmlns:dms] => http://schemas.microsoft.com/office/2009/documentManagement/types [xmlns:my] => http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-04-22T08:03:54 [xmlns:xd] => http://schemas.microsoft.com/office/infopath/2003 [xml:lang] => uk-UA ) [my:ODM_Total] => Array ( [my:Date] => Array ( [value] => 2017-05-09 ) [my:TotalScore] => Array ( [value] => 384 ) [my:Description] => Array ( ) [my:Manager] => Array ( [value] => "I" ) [my:Store] => Array ( [value] => 3101 ) ) [my:Clean] => Array ( [my:Total_Clean] => Array ( [value] => 63 ) [my:CH_1] => Array ( [value] => true ) [my:L_1] => Array ( [value] => 5 ) [my:CH_2] => Array ( [value] => true ) [my:L_2] => Array ( [value] => 5 ) [my:CH_3] => Array ( [value] => true ) [my:CH_4] => Array ( [value] => true ) [my:L_4] => Array ( [value] => 5 ) [my:L_3] => Array ( [value] => 5 ) [my:CH_5] => Array ( [value] => false ) [my:L_5] => Array ( ) [my:CH_6] => Array ( [value] => true ) [my:L_6] => Array ( [value] => 5 ) 

Etc.

You need to make a function to search for the key in the form "/my:CH_\d+/" and the result is an array:

 array( [my:CH_1]=>array( [value]=>true ) ... 

At the moment there is only such a function:

 function searchExc($array, $key) { $results = array(); if (is_array($array)) { foreach ($array as $key2=>$subarray) { if (preg_match($key,$key2)) if(is_array($array[$key2]) && isset($array[$key2]['value'])) { $results[] = $array; } $results = array_merge($results, searchExc($subarray, $key, $value)); } } return $results; } 

but she does not deduce what is needed ... She deduces this:

 Array ( [0] => Array ( [my:Total_Clean] => Array ( [value] => 63 ) [my:CH_1] => Array ( [value] => true ) [my:L_1] => Array ( [value] => 5 ) [my:CH_2] => Array ( [value] => true ) [my:L_2] => Array ( [value] => 5 ) [my:CH_3] => Array ( [value] => true ) [my:CH_4] => Array ( [value] => true ) [my:L_4] => Array ( [value] => 5 ) [my:L_3] => Array ( [value] => 5 ) 

Something is wrong with the function, so please help.
I searched all resources and can't find ...
Thanks in advance for the replies.

Additionally:

The array is multidimensional and these keys can be scattered in different dimensions.
and you need to find all the keys with this convergence.

  • @Visman, Thanks for editing, not very strong in Russian ... - Mykola Kіkets
  • The result should be an array, where all the CH_ keys will be at the first level? - rjhdby
  • @rjhdby, that's right - Mykola Kіkets

1 answer 1

 $input=['k1'=>['my:CH_1'=>['a'=>'b'],'c'=>'b'],'k2'=>'v2','my:CH_2'=>'k2']; $output = []; function walk($input, &$output){ foreach($input as $key => $value){ if(substr($key, 0, 6) === 'my:CH_') $output[$key] = $value; if(is_array($value)) walk($value, $output); } } walk($input, $output); var_dump($output); 

sandbox

  • does not work, returned empty masiv ... - Mykola Kіkets
  • Sori ... my school ... - Mykola Kіkets
  • forgot to check the data on the entry - Mykola Kіkec