BASE . LOG, 'log_file_id' => $LOG_FILE_ID, 'log_per_date' => true, ]); // $_array = new CoreLibs\Combined\ArrayHandler(); // $array_class = 'CoreLibs\Combination\ArrayHandler'; $PAGE_NAME = 'TEST CLASS: ARRAY HANDLER'; print ""; print "" . $PAGE_NAME . ""; print ""; print '
Class Test Master
'; print '

' . $PAGE_NAME . '

'; // recursive array search $test_array = [ 'foo' => 'bar', 'input' => [ 'element_a' => [ 'type' => 'text' ], 'element_b' => [ 'type' => 'email' ], 'element_c' => [ 'type' => 'email' ], 'element_d' => [ 'type' => 'butter' ], ], ]; echo "SOURCE ARRAY: " . DgS::printAr($test_array) . "
"; // frist return echo "ARRAYSEARCHRECURSIVE(email, [array], type): " . DgS::printAr(ArrayHandler::arraySearchRecursive('email', $test_array, 'type')) . "
"; echo "ARRAYSEARCHRECURSIVE(email, [array]['input'], type): " . DgS::printAr(ArrayHandler::arraySearchRecursive('email', $test_array['input'], 'type')) . "
"; echo "ARRAYSEARCHRECURSIVE(email, [array]['input'], wrong): " . DgS::printAr(ArrayHandler::arraySearchRecursive('email', $test_array['input'], 'wrong')) . "
"; // all return echo "ARRAYSEARCHRECURSIVEALL(email, [array], type): " . Dgs::printAr((array)ArrayHandler::arraySearchRecursiveAll('email', $test_array, 'type')) . "
"; echo "ARRAYSEARCHRECURSIVEALL(email, [array], type, false): " . Dgs::printAr((array)ArrayHandler::arraySearchRecursiveAll('email', $test_array, 'type', false)) . "
"; // simple search echo "ARRAYSEARCHSIMPLE([array], type, email): " . Dgs::prBl(ArrayHandler::arraySearchSimple($test_array, 'type', 'email')) . "
"; echo "ARRAYSEARCHSIMPLE([array], type, not): " . Dgs::prBl(ArrayHandler::arraySearchSimple($test_array, 'type', 'not')) . "
"; echo "ARRAYSEARCHSIMPLE([array], type, [email,butter]): " . Dgs::prBl(ArrayHandler::arraySearchSimple($test_array, 'type', ['email', 'butter'])) . "
"; echo "ARRAYSEARCHSIMPLE([array], type, [email,not]): " . Dgs::prBl(ArrayHandler::arraySearchSimple($test_array, 'type', ['email', 'not'])) . "
"; echo "ARRAYSEARCHSIMPLE([array], type, [never,not]): " . Dgs::prBl(ArrayHandler::arraySearchSimple($test_array, 'type', ['never', 'not'])) . "
"; $array_1 = [ 'foo' => 'bar' ]; $array_2 = [ 1, 2, 3 ]; $array_3 = [ 'alpha' => [ 'beta' => 4 ] ]; // recusrice merge print "ARRAYMERGERECURSIVE: " . DgS::printAr(SetVarType::setArray( ArrayHandler::arrayMergeRecursive($array_1, $array_2, $array_3) )) . "
"; // array difference $array_left = [ 'same' => 'data', 'left' => 'Has L' ]; $array_right = [ 'same' => 'data', 'right' => 'has R' ]; print "ARRAYDIFF: " . DgS::printAr(ArrayHandler::arrayDiff($array_left, $array_right)) . "
"; // in array check print "INARRAYANY([1,3], [array]): " . DgS::printAr(SetVarType::setArray( ArrayHandler::inArrayAny([1, 3], $array_2) )) . "
"; // flatten array print "FLATTENARRAY: " . DgS::printAr(ArrayHandler::flattenArray($test_array)) . "
"; print "FLATTENARRAYKEY: " . DgS::printAr(ArrayHandler::flattenArrayKey($test_array)) . "
"; // flatten for key set print "ARRAYFLATFORKEY: " . DgS::printAr(ArrayHandler::arrayFlatForKey($test_array, 'type')) . "
"; /** * attach key/value to an array so it becomes nested * * @param string $pre Attach to new (empty for new root node) * @param string $cur New node * @param array $node Previous created array * @return array Updated array */ function rec(string $pre, string $cur, array $node = []) { print "
#### PRE: " . $pre . ", CUR: " . $cur . ", N-c: " . count($node) . " [" . join('|', array_keys($node)) . "]
"; if (!$pre) { print "** NEW
"; $node[$cur] = []; } else { if (array_key_exists($pre, $node)) { print "+ KEY FOUND: " . $pre . ", add: " . $cur . "
"; $node[$pre][$cur] = []; } else { print "- NOT FOUND: loop
"; foreach ($node as $_pre => $_cur) { print "> TRY: " . $_pre . " => " . count($_cur) . " [" . join('|', array_keys($_cur)) . "]
"; if (count($_cur) > 0) { $node[$_pre] = rec($pre, $cur, $_cur); } } } } return $node; } $data = [ 'image' => 'foo', 'element' => 'w-1', 'rotate' => 360, 'html' => [ 'image' => 'bar', 'result_image' => 'baz', 'rule' => 'wrong' ], [ 'image' => 'large' ], [ 'nothing' => 'wrong' ], 'nest' => [ 'nust' => [ 'nist' => [ 'foo' => 'bar', 'image' => 'long, long' ] ] ], 's' => [ 'image' => 'path?' ], ]; $search = ['image', 'result_image', 'nothing', 'EMPTY']; $result = ArrayHandler::arraySearchKey($data, $search); print "ARRAYSEARCHKEY: Search: " . DgS::printAr($search) . ", Found: " . DgS::printAr($result) . "
"; $result = ArrayHandler::arraySearchKey($data, $search, true); print "ARRAYSEARCHKEY: FLAT: Search: " . DgS::printAr($search) . ", Found: " . DgS::printAr($result) . "
"; $result = ArrayHandler::arraySearchKey($data, $search, true, true); print "ARRAYSEARCHKEY: FLAT:PREFIX: Search: " . DgS::printAr($search) . ", Found: " . DgS::printAr($result) . "
"; $result = ArrayHandler::arraySearchKey($data, ["EMPTY"], true); print "ARRAYSEARCHKEY: FLAT:PREFIX: Search: " . DgS::printAr(["EMPTY"]) . ", Found: " . DgS::printAr($result) . "
"; // $data = [ // [ // [name] => qrc_apcd, // [value] => 5834367225, // ], // [ // [name] => qrc_other, // [value] => test, // ], // [ // [name] => qrc_car_type, // [value] => T33P17, // ], // [ // [name] => qrc_deaer_store, // [value] => 9990:001, // ] // ] // $test = [ // 'A' => [ // 'B' => [], // 'C' => [ // 'D' => [], // 'E' => [ // 'F' => [] // ] // ] // ], // '1' => [], // '2' => [], // '3' => [ // 'G' => [] // ] // ]; // build a tested array for flatten $test = []; // core $test = rec('', 'A', $test); $test = rec('', '1', $test); $test = rec('', '2', $test); $test = rec('', '3', $test); $test = rec('3', 'G', $test); $test = rec('A', 'B', $test); $test = rec('A', 'C', $test); $test = rec('C', 'D', $test); $test = rec('C', 'E', $test); $test = rec('E', 'F', $test); // new $test = rec('C', 'U', $test); $test = rec('F', 'U', $test); $test = rec('', 'Al', $test); $test = rec('B', 'B1', $test); print "ORIGINAL: " . \CoreLibs\Debug\Support::printAr($test) . "
"; print "FLATTEN-c: " . \CoreLibs\Debug\Support::printAr(ArrayHandler::flattenArrayKey($test)) . "
"; $test = [ 'a' => ['a1' => 'a1foo', 'a2' => 'a1bar'], 1 => 'bar', 'c' => [2, 3, 4], 'd' => [ 'e' => [ 'de1' => 'subfoo', 'de2' => 'subbar', 'a2' => 'a1bar' ] ] ]; print "ORIGINAL: " . \CoreLibs\Debug\Support::printAr($test) . "
"; print "FLATTEN: " . \CoreLibs\Debug\Support::printAr(ArrayHandler::flattenArrayKey($test)) . "
"; // genAssocArray $db_array = [ 0 => ['a' => 'a1', 'b' => 2], 1 => ['a' => 'a2', 'b' => 3], 2 => ['a' => '', 'b' => ''], ]; // $key = false; $key = 'a'; // $value = false; $value = 'b'; $flag = false; $output = \CoreLibs\Combined\ArrayHandler::genAssocArray($db_array, $key, $value, $flag); print "OUTPUT: " . \CoreLibs\Debug\Support::printAr($output) . "
"; print "
"; $array = [ 'a' => 'First', 'b' => 'Second', 'c' => 'Third', ]; foreach (array_keys($array) as $search) { print "Result[" . $search . "]: " . "next: " . DgS::printAr(ArrayHandler::arrayGetNextKey($array, $search)) . ", " . "prev: " . DgS::printAr(ArrayHandler::arrayGetPrevKey($array, $search)) . "
"; } print "Key not exists: " . DgS::printAr(ArrayHandler::arrayGetNextKey($array, 'z')) . "
"; print "
"; $keys = ['b', 'c', 'f']; print "Return only: " . DgS::printAr($keys) . ": " . DgS::printAr(ArrayHandler::arrayReturnMatchingKeyOnly($array, $keys)) . "
"; $out = array_filter($array, fn($key) => in_array($key, $keys), ARRAY_FILTER_USE_KEY); print "array filter: " . DgS::printAr($keys) . ": " . DgS::printAr($out) . "
"; $out = array_intersect_key( $array, array_flip($keys) ); print "array intersect key: " . DgS::printAr($keys) . ": " . DgS::printAr($out) . "
"; print "array + suffix: " . DgS::printAr(ArrayHandler::arrayModifyKey($array, key_mod_suffix:'_attached')) . "
"; print "
"; $unsorted = [9, 5, 'A', 4, 'B', 6, 'c', 'C', 'a']; $unsorted_keys = [ 'A' => 9, 'B' => 5, 'C' => 'A', 'D' => 4, 'E' => 'B', 'F' => 6, 'G' => 'c', 'H1' => 'D', 'B1' => 'd', 'H' => 'C', 'I' => 'a' ]; print "Unsorted: " . DgS::printAr($unsorted) . "
"; print "(sort): " . DgS::printAr(ArrayHandler::sortArray($unsorted)) . "
"; print "(sort, lower): " . DgS::printAr(ArrayHandler::sortArray($unsorted, case_insensitive:true)) . "
"; print "(sort, reverse): " . DgS::printAr(ArrayHandler::sortArray($unsorted, reverse:true)) . "
"; print "(sort, lower, reverse): " . DgS::printAr(ArrayHandler::sortArray($unsorted, case_insensitive:true, reverse:true)) . "
"; print "(sort, keys): " . DgS::printAr(ArrayHandler::sortArray($unsorted_keys, maintain_keys:true)) . "
"; print "(sort, keys, lower): " . DgS::printAr(ArrayHandler::sortArray($unsorted_keys, maintain_keys:true, case_insensitive:true)) . "
"; print "
"; $unsorted = [9 => 'A', 5 => 'B', 'A' => 'C', 4 => 'D', 'B' => 'E', 6 => 'F', 'c' => 'G', 'C' => 'H', 'a' => 'I']; print "Unsorted Keys: " . DgS::printAr($unsorted) . "
"; print "(sort): " . DgS::printAr(ArrayHandler::sortArray($unsorted)) . "
"; print "(sort, keys): " . DgS::printAr(ArrayHandler::sortArray($unsorted, maintain_keys:true)) . "
"; print "(kosrt): " . DgS::printAr(ArrayHandler::ksortArray($unsorted)) . "
"; print "(kosrt, reverse): " . DgS::printAr(ArrayHandler::ksortArray($unsorted, reverse:true)) . "
"; print "(kosrt, lower case, reverse): " . DgS::printAr(ArrayHandler::ksortArray($unsorted, case_insensitive:true, reverse:true)) . "
"; print "
"; $nested = [ 'B' => 'foo', 'a', '0', 9, /** @phpstan-ignore-line This is a test for wrong index */ '1' => ['z', 'b', 'a'], 'd' => ['zaip', 'bar', 'baz'] ]; print "Nested: " . DgS::printAr($nested) . "
"; print "(sort): " . DgS::printAr(ArrayHandler::sortArray($nested)) . "
"; print "(ksort): " . DgS::printAr(ArrayHandler::ksortArray($nested)) . "
"; print "
"; $search_array = [ 'table_lookup' => [ 'match' => [ ['param' => 'access_d_cd', 'data' => 'a_cd', 'time_validation' => 'on_load',], ['param' => 'other_block', 'data' => 'b_cd'], ['pflaume' => 'other_block', 'data' => 'c_cd'], ['param' => 'third_block', 'data' => 'd_cd', 'time_validation' => 'cool'], ['special' => 'other_block', 'data' => 'e_cd', 'time_validation' => 'other'], ] ] ]; print "Search: " . DgS::printAr($search_array) . "
"; print "Result (all): " . Dgs::printAr(ArrayHandler::findArraysMissingKey( $search_array, 'other_block', 'time_validation' )) . "
"; print "Result (key): " . Dgs::printAr(ArrayHandler::findArraysMissingKey( $search_array, 'other_block', 'time_validation', 'pflaume' )) . "
"; print "Result (key): " . Dgs::printAr(ArrayHandler::findArraysMissingKey( $search_array, 'other_block', ['data', 'time_validation'], 'pflaume' )) . "
"; print "
"; $search_array = [ 'a' => [ 'lookup' => 1, 'value' => 'Foo', 'other' => 'Bar', ], 'b' => [ 'lookup' => 1, 'value' => 'AAA', 'other' => 'Other', ], 'c' => [ 'lookup' => 0, 'value' => 'CCC', 'other' => 'OTHER', ], 'd' => [ 'd-1' => [ 'lookup' => 1, 'value' => 'D SUB 1', 'other' => 'Other B', ], 'd-2' => [ 'lookup' => 0, 'value' => 'D SUB 2', 'other' => 'Other C', ], 'more' => [ 'lookup' => 1, 'd-more-1' => [ 'lookup' => 1, 'value' => 'D MORE SUB 1', 'other' => 'Other C', ], 'd-more-2' => [ 'lookup' => 0, 'value' => 'D MORE SUB 0', 'other' => 'Other C', ], ] ] ]; print "Search: " . DgS::printAr($search_array) . "
"; print "Result: " . DgS::printAr(ArrayHandler::selectArrayFromOption( $search_array, 'lookup', 1, )) . "
"; print "Result: " . DgS::printAr(ArrayHandler::selectArrayFromOption( $search_array, 'lookup', 1, recursive:true )) . "
"; print "Result: " . DgS::printAr(ArrayHandler::selectArrayFromOption( $search_array, 'lookup', 1, recursive:true, flat_separator:'-=-' )) . "
"; print "Result: " . DgS::printAr(ArrayHandler::selectArrayFromOption( $search_array, 'lookup', 1, recursive:true, flat_result:false )) . "
"; print "Result: " . DgS::printAr(ArrayHandler::selectArrayFromOption( $search_array, 'other', 'Other', case_insensitive:false, )) . "
"; $nestedTestData = [ 'level1_a' => [ 'name' => 'Level1A', 'type' => 'parent', 'children' => [ 'child1' => [ 'name' => 'Child1', 'type' => 'child', 'active' => true ], 'child2' => [ 'name' => 'Child2', 'type' => 'child', 'active' => false ] ] ], 'level1_b' => [ 'name' => 'Level1B', 'type' => 'parent', 'children' => [ 'child3' => [ 'name' => 'Child3', 'type' => 'child', 'active' => true, 'nested' => [ 'deep1' => [ 'name' => 'Deep1', 'type' => 'deep', 'active' => true ] ] ] ] ], 'item5' => [ 'name' => 'Direct', 'type' => 'child', 'active' => false ] ]; $result = ArrayHandler::selectArrayFromOption( $nestedTestData, 'type', 'child', false, false, true, true, ':*' ); print "*1*Result: " . DgS::printAr($result) . "
"; $data = [ 'parent1' => [ 'name' => 'Parent1', 'status' => 'ACTIVE', 'children' => [ 'child1' => [ 'name' => 'Child1', 'status' => 'active' ] ] ] ]; $result = ArrayHandler::selectArrayFromOption( $data, 'status', 'active', false, // not strict true, // case insensitive true, // recursive true, // flat result '|' // custom separator ); print "*2*Result: " . DgS::printAr($result) . "
"; print ""; // __END__