2017-05-09 2 views
0

이 배열을 [ 'field'] 값으로 검색하고 배열의 해당 필드와 관련된 [ 'value'] 값을 반환하려고합니다.PHP 배열 배열 배열

예를 들어, "TheThirdField와 관련된 가치를 말해주세요"라고 말하고 싶습니다.

다음과 같은 많은 순열을 시도했습니다. $ myVariable = $ Array [ 'result'] [ 'totalrows'] [0] [ 'rownum'] [0] [ 'field'] ;

더 명확히하기 위해, 내 검색 필드가있는 순서/하위 배열을 알 수 없으며 [field ']와 관련된 문자열 값만 알고 있습니다.

어떻게하면됩니까?

Array 
(
    [result] => Array 
     (
      [totalrows] => 1 
      [rows] => Array 
       (
        [0] => Array 
         (
          [rownum] => 1 
          [values] => Array 
           (
            [0] => Array 
             (
              [field] => testMeOnce 
              [value] => 436586498 
             ) 

            [1] => Array 
             (
              [field] => testMeTwice 
              [value] => 327698034 
             ) 

            [2] => Array 
             (
              [field] => theThirdField 
              [value] => 108760374 
             ) 

            [3] => Array 
             (
              [field] => theFourthField 
              [value] => 2458505 
             ) 

            [4] => Array 
             (
              [field] => fifthField 
              [value] => -0.0201 
             ) 

           ) 

         ) 

       ) 

     ) 

) 
+0

시도 할 때 오류가 있습니까? –

답변

1

그와 비슷한 것을 기대 했습니까?

$needle = 'theThirdField'; // searched field name 
$valuesArray = $Array['result']['rows'][0]['values']; //now you have clearer array 

array_walk($valuesArray, function($element, $key) use ($needle) { 
    if ($element['field'] == $needle) { 
     echo $element['value']; 
    } 
}); 
1

난 당신 만 $ 인 myVariable의 차원에서 검색하려는 가정, 이런 짓을 할 것이다 :

난 당신의 코드를 실행
$myVariable = $Array['result']['rows'][0]['values']; 

foreach ($myVariable as $key => $value) { 
    if($value['field'] === 'theThirdField') { 
     echo $value['value']; 
     break; 
    } 
} 
0

, 나는 몇 가지 구문 오류를 얻었다. 나는이 (변경 구문 만)에 배열을 변경 한 :

$a = Array 
    (
     'result' => 
      [ 
       'totalrows' => 1, 
       'rows' => 
        [ 
         0 => 
          [ 
           'rownum' => 1, 
           'values' => 
            [ 
             0 => 
              [ 
               'field' => 'testMeOnce', 
               'value' => 436586498 
              ], 

             1 => 
              [ 
               'field' => 'testMeTwice', 
               'value' => 327698034 
              ], 

             2 => 
              [ 
               'field' => 'theThirdField', 
               'value' => 108760374 
              ], 

             3 => 
              [ 
               'field' => 'theFourthField', 
               'value' => 2458505 
              ], 

             4 => 
              [ 
               'field' => 'fifthField', 
               'value' => -0.0201 
              ] 

            ] 

          ] 

        ] 

      ] 

    ); 

지금이 같은 값을 얻을 수 있습니다 :

print($a['result']['rows'][0]['values'][2]['value']); // --> 108760374 

가 나는 이것이 당신이 찾고있는 희망!

0

감사합니다. 난 당신의 제안을 혼합하여 사용하고 다음 일을 결국 : 내가 원하는 모든 필드의 이름을 알고 있기 때문에

$valuesArray = $Array['result']['rows'][0]['values']; 

foreach ($valuesArray as $value) { 
    $fieldName = $value['field']; 
    $$fieldName = $value['value']; 
} 

,하지만 순서가 아니라,이 날 각 필드의 모든 값을 캡처 할 수 필드 이름이 변수 이름이됩니다.