Last active
August 29, 2015 13:56
-
-
Save tadas-s/9012754 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$json = array( | |
'values' => array( | |
0 => array( | |
'field' => 'id1', | |
'value' => 895 | |
), | |
1 => array( | |
'field' => 'id2', | |
'value' => 6395 | |
), | |
2 => array( | |
'field' => 'memo', | |
'value' => 'abcde' | |
), | |
3 => array( | |
'field' => 'id2', | |
'value' => 66666 | |
) | |
) | |
); | |
function array_silly_search_single($array, $column, $column_value, $return_column) | |
{ | |
return array_reduce( | |
$array, | |
function($result, $item) use($column, $column_value, $return_column) { | |
return $item[$column] == $column_value ? $item[$return_column] : $result; | |
} | |
); | |
} | |
function array_silly_search_multi($array, $column, $column_value, $return_column) | |
{ | |
return array_filter(array_map( | |
function($item) use($column, $column_value, $return_column) { | |
return $item[$column] == $column_value ? $item[$return_column] : null; | |
}, | |
$array | |
)); | |
} | |
var_dump(array_reduce($json['values'], function($current, $item) { return $item['field'] == 'id2' ? $item['value'] : $current; })); | |
var_dump(array_filter(array_map(function($item) { return $item['field'] == 'id2' ? $item['value'] : null; }, $json['values']))); | |
var_dump(array_silly_search_single($json['values'], 'field', 'id2', 'value')); | |
var_dump(array_silly_search_multi($json['values'], 'field', 'id2', 'value')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int(66666) | |
array(2) { | |
[1]=> | |
int(6395) | |
[3]=> | |
int(66666) | |
} | |
int(66666) | |
array(2) { | |
[1]=> | |
int(6395) | |
[3]=> | |
int(66666) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment