Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_non_zero( $str_keys, $data ) {
- $keys = explode( ',', $str_keys );
- foreach ( $keys as $key ) {
- if ( ! empty( $data[ $key ] ) ) {
- return $data[ $key ];
- }
- }
- return null;
- }
- $data = ['key0' => 0, 'key1' => 1, 'key3'=> 3];
- echo( get_non_zero( 'key1,key2', $data ) ); // выведет 1
- $data = ["key0" => 0, "key1" => 1, "key3"=> 3];
- $search = array_flip(explode(",", "key1,key2"));
- $result = array_intersect_key($data, $search);
- echo count($result) > 1 ? null : array_pop($result);
- function getNonZero($keys, $data)
- {
- $search = array_flip(explode(",", $keys));
- $result = array_intersect_key($data, $search);
- if (count($result) > 1):
- return null;
- else:
- return array_pop($result);
- endif;
- }
- $data = ["key0" => 0, "key1" => 1, "key3"=> 3];
- $search = "key1,key2";
- echo getNonZero($search, $data);
- function searchInArray($value)
- {
- preg_match("([w+]{1,})", $value, $matches);
- return !empty($matches) ? $matches[0] : $value;
- }
- function getNonZero($search, $data)
- {
- $search = array_map("searchInArray", explode(",", $search));
- $result = array_intersect_key($data, array_flip($search));
- if (count($result) === 1):
- return is_array($result) ? array_shift($result[key($result)]) : $result;
- else:
- return null;
- endif;
- }
- $data = ['key0' => 0, 'fields' => ['name_1'=>1], 'key3'=> 3];
- $sting = 'name,entry.436917433,billing_first_name,fields[name_1]';
- echo getNonZero($sting, $data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement