2016-10-27 1 views
0

다음 json이 주어지면 shipmentWay 속성에 각 부모 요소에 대해 동일한 값이 있는지 확인하려면 어떻게해야합니까?속성에 JSON과 동일한 값이 있는지 확인하십시오.

{ 
    "3":[ 
     { 
     "idOrdenCompraProducto":3, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ], 
    "4":[ 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":3000 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"A\u00e9reo", 
     "qty":1500 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":1000 
     } 
    ], 
    "5":[ 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     }, 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ] 
} 

저는 정말 고맙습니다. 미리 감사드립니다.

+0

return false;을 대체 숫자가 idOrdenCompraProducto의 값과 일치한다고 가정, 최초의 불일치 있던 배열 반환하기를 원한다면

하지만 실제 텍스트 –

+0

이 스크린 샷을 다시 타이핑 한 이후로 나는 [json_decode] (http://php.net/manual/en/function.json-decode.php)를 보면 object –

+0

@ Memor-X 네 말이 맞아. 이것은 나의 첫 번째 질문입니다. 내 질문에 json 문자열로 업데이트했습니다. – user3720752

답변

0
<?php 
$json = '{ 
    "3":[ 
     { 
     "idOrdenCompraProducto":3, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ], 
    "4":[ 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":3000 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"A\u00e9reo", 
     "qty":1500 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":1000 
     } 
    ], 
    "5":[ 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     }, 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ] 
}'; 
$array = json_decode($json, true); 
$shipmentWay = array(); 
foreach($array as $key => $parent) 
{ 
    foreach($parent as $index => $child) 
    $shipmentWay[$key][$child['shipmentWay']] = true; 
} 
// var_dump($shipmentWay); 
    $result = array(); 
    foreach($shipmentWay as $key => $value) 
    { 
     $result[$key] = (count($value) > 1)?false:true; 
    } 
    var_dump($result); 

데모 출력

어레이 (3) {[3] => 불리언 (TRUE) [4] => BOOL (FALSE) 인 https://eval.in/667231

참조 [5] =>
BOOL} (참)을

+0

@ Memor-X 더 빨리 수정하겠습니다. –

+0

알림 : 정의되지 않은 색인 : shipmentWay – user3720752

+0

@ user3720752 나는 그것을 편집합니다. –

0

확인 제가

this example 만들어
function jsonAllSame($jsonStr) 
{ 
    $object = json_decode($jsonStr); 

    foreach($object as $objArr) 
    { 
     $index = 0; 
     $compare = ""; 
     foreach($objArr as $obj) 
     { 
      if($compare == "") 
      { 
       $compare = $obj->shipmentWay; 
      } 
      else 
      { 
       if($obj->shipmentWay != $compare) 
       { 
        return false; 
       } 
      } 
     } 
    } 

    return true; 
} 

$json = '{ 
    "3":[ 
     { 
     "idOrdenCompraProducto":3, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ], 
    "4":[ 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":3000 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"A\u00e9reo", 
     "qty":1500 
     }, 
     { 
     "idOrdenCompraProducto":4, 
     "shipmentWay":"Confirmation Pending", 
     "qty":1000 
     } 
    ], 
    "5":[ 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     }, 
     { 
     "idOrdenCompraProducto":5, 
     "shipmentWay":"Confirmation Pending", 
     "qty":25000 
     } 
    ] 
}'; 
$compare = "Confirmation Pending"; 

var_dump(jSonAllSame($json)); 

당신이보고 싶은 주요 기능은 jSonAllSame입니다. 그것이하는 일은 json 문자열을 인수로 취하는 것입니다. 함수 내부에서 디코드 한 다음 2 개의 foreach 루프를 사용하여 객체의 각 배열을 반복합니다.

처음으로 객체 배열 (두 번째 foreach 루프)에 대해 비교할 항목이 없으므로 $compare을 우리가 보는 첫 번째 값으로 설정합니다.

$compare이 비어 있지 않으므로 두 번째 반복에서 비교할 수 있습니다. 우리가 일치하지 않는 값을 하나 찾으면 그곳에서 함수를 멈추고 false를 반환합니다. 객체의 각 배열을 살펴보고 불일치를 찾지 못하면 true를 반환 할 수 있습니다. 우리가 대신 우리는 당신이 JSON의 스크린 샷을 게시하지 않은 경우 쉬울 것 return $obj->idOrdenCompraProducto;

+0

감사합니다 @ memor-x 아래 해결 방법으로 끝났습니다. – user3720752

관련 문제