检查值是否存在并返回多维数组中的对象

时间:2017-07-25 11:46:10

标签: php arrays multidimensional-array

我需要检查数组中是否存在值,一旦存在,我需要获取对象。

Array
(
    [0] => Array
        (
            [_id] => Array
                (
                    [purok] => test
                    [year] => 2017
                    [options] => below-1
                )

            [data] => Array
                (
                    [58cf4935572d6e32900057ab] => Array
                        (
                            [age-sex-distribution] => Array
                                (
                                    [age-range] => Array
                                        (
                                            [options] => below-1
                                        )

                                    [gender] => Array
                                        (
                                            [male-distribution-count] => 12
                                            [female-distribution-count] => 12
                                        )

                                )

                        )

                )

            [date] => 2017-07-08
        )

    [1] => Array
        (
            [_id] => Array
                (
                    [purok] => test
                    [year] => 2017
                    [options] => toddlers (1-2)
                )

            [data] => Array
                (
                    [58cf4935572d6e32900057ab12] => Array
                        (
                            [age-sex-distribution] => Array
                                (
                                    [age-range] => Array
                                        (
                                            [options] => toddlers (1-2)
                                        )

                                    [gender] => Array
                                        (
                                            [male-distribution-count] => 12
                                            [female-distribution-count] => 12
                                        )

                                )

                        )

                )

            [date] => 2017-07-08
        )

)

我需要检查此[options] => below-1是否存在。一个存在,我需要在数组中获得data

到目前为止,我已尝试过这个。

$keySearch = "data.options";
$dataOption = array_search("below-1", array_column($rec, $keySearch));
print_r($dataOption);

但没有结果。

感谢您提前帮助我。

3 个答案:

答案 0 :(得分:2)

generate_pgm() {
  PDF="$1"
  find . -name \*.pdf | parallel 'mkdir -p {.} && gs -dQUIET -dINTERPOLATE -dSAFER -dBATCH -dNOPAUSE -dPDFSETTINGS=/ebook -dNumRenderingThreads=4 -sDEVICE=pgmraw -r300 -dTextAlphaBits=4 -sProcessColorModel=DeviceGray -sColorConversionStrategy=Gray -dOverrideICC -o {.}/{.}-%03d.pgm {}' ::: *.pdf
}
export -f generate_pgm
ocr() {
  PGM="$1"
  find . -name \*.pgm | parallel 'tesseract {} {.} -l deu_frak && rm {.}.pgm'
  rm "$PGM"
}
export -f ocr

time parallel -j 4 --progress --eta 'generate_pgm {}; parallel --argsep ,,, ocr ,,, pgm/*.pgm'  ::: *pdf

你可以试试这个

答案 1 :(得分:1)

试试这个:

function search_array($needle, $haystack) {
     if(in_array($needle, $haystack)) {
          return true;
     }
     foreach($haystack as $element) {
          if(is_array($element) && search_array($needle, $element))
               return true;
     }
   return false;
}

if(!search_array($value, $array)) {
     // do something if the given value does not exist in the array
}else{
     // do something if the given value exists in the array
}

答案 2 :(得分:1)

你应该试试这个:

for($i=0; $i < count($rec); $i++) {
     if ($rec[$i]['_id']['options'] === "below-1") {
          $dataOption = $rec[$i]['data'];
          break;
     }
}
print_r($dataOption);

它应该做你期望的事情; - )