2017-03-10 12 views
0

PHP DOM XPath을 사용하여 을 읽습니다. XML 내가 붙어있어 다음과 같은 구조를 가지고있다PHP XPath에서 동일한 노드 이름을 구별합니다

<details> 
    <name>name1</name> 
    <address>address1</address> 
</details> 
<details> 
    <name>name2</name> 
    <mobile>mobileNum</mobile> 
    <address>address2</address> 
</details> 

내가 가진 내가이 배열을 사용하고 다른 배열에 값 XML 및 저장소에서하지만 첫 번째 레코드 모바일에서 값을 읽을

array(
     'name', 
     'mobile', 
     'address' 
    ); 

처럼 배열 번호이 없으므로 두 번째 레코드에서 모바일 번호를 읽고 첫 번째 레코드에 삽입합니다.

예상 출력은

array 
(

    [0] => array(

       'person_name' = name1, 
       'address' = address1 
       ) 
    [1] => array(

       'person_name' = name1, 
       'mobile_no' = mobileNum 
       'address' = address1 
       ) 
) 

이다하지만 내가 같은 이름을 가진 두 노드 사이의 값을 차별화 할 수있는 방법

array 
(

    [0] => array(

       'person_name' = name1, 
       'mobile_no' = mobileNum 
       'address' = address1 
       ) 
    [1] => array(

       'person_name' = name1, 
       'address' = address1 
       ) 
) 

으로 출력을 얻을. XML을 읽을 수

코드는 여기에

$nodes = array 
(
    'person_name' => 'name', 
    'mobile_no' => 'mobile', 
    'address'  => 'address' 
) 

$final_data = array(); 

$node_values = ''; 

foreach($nodes as $key => $data) 
{ 
    $node_values = $xml->xpath('//details'.$data); 

    $node_values = json_decode(json_encode((array)$node_values), TRUE); 

    if(!empty($node_values))  
    { 
     $i = 0; 

     foreach($node_values as $d) 
     { 
      $final_data[$i][$key] = trim($d[0]); 

      $i++; 
     } 
    } 
} 
+1

__code__를 알려주십시오. –

+1

이러한 접근법은 잘못된 것입니다.'details'를 반복하십시오. –

+0

그러면 다른 방법으로 데이터를 얻을 수 있습니다. – hrishi

답변

1

입니다 의견이 어떻게 작동하는지 설명과 함께 동작하는 예제입니다 :

<?php 

// First of all, I've added <root> element to your XML document, 
// because otherwise it's invalid. 
// But it's not important for the rest of the code. 
// 
// Also I've added additional <somethingelse> tag to show that filtering is working 
$xmlString = '<root><details> 
    <name>name1</name> 
    <address>address1</address> 
</details> 
<details> 
    <name>name2</name> 
    <mobile>mobileNum</mobile> 
    <address>address2</address> 
    <somethingelse>This will be filtered</somethingelse> 
</details></root>'; 


$xml = new SimpleXMLElement($xmlString); 
//array_flip to get node names as keys for later foreach loop 
$nodes = array_flip(array 
(
    'person_name' => 'name', 
    'mobile_no' => 'mobile', 
    'address'  => 'address', 
)); 

$final_data = array(); 

//Here are all <details> sections' data in array. 
$node_values = $xml->xpath('//details'); 
$node_values = json_decode(json_encode((array)$node_values), TRUE); 

//this loop filters XML data from keys not existing in $nodes, 
// which are the only that you want to keep 
foreach($node_values as $node) { 
    $final_data[] = array_intersect_key($node, $nodes); 
} 

var_dump($final_data); 

https://3v4l.org/ICO5i 난 당신이 XML 데이터를 필터링 할 것인지, 가정이 $nodes 배열에 나열되어 있지 않습니다. 그렇지 않으면 필요 이상으로 많은 데이터가 필요 없으면 foreach 루프를 건너 뛰고 $node_values을 최종 데이터로 사용할 수 있습니다.

+0

Jakub Matczak : 고마워요. 그것은 잘 작동하지만 접두사가 있으면 빈 출력을 제공합니다. – hrishi