Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

PHP xml get distinct attribute

So if i have an xml structure like the one below in the code.  I am looking to get the unique 'STATION' elements.  So from this xml there is RECEPTION, ACCOUNTANT AND LEGAL.  Its fine if i just put them in an array.  Eventually i am going to put it into a json format but i can do that.  However i need to sort them by the 'STATION_NUMBER', so in this case using this xml i want to get an array like this

['RECEPTION', 'LEGAL', 'ACCOUNTANT']

Those are the distinct 'STATION' values sorted by the STATION_NUMBER Ascending

I am unsure how i would approach this in php, i have been getting the resource using curl like below
$url = 'personal.xml'; // this is the xml file listed below
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GET);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
$content = curl_exec($ch);
curl_close($ch);

but now that i have the $content i am unsure how to get the

['RECEPTION', 'LEGAL', 'ACCOUNTANT']

from these values
<?xml version='1.0'  encoding='Cp1252' ?>
<RESULTS>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Jones, Bob]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[2]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[RECEPTION]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Johnson, Steve]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[2]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[RECEPTION]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Smith, John]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[4]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[ACCOUNTANT]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Agard, Joe]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[3]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[LEGAL]]></COLUMN>
	</ROW>
</RESULTS>

Open in new window

0
Brant Snow
Asked:
Brant Snow
2 Solutions
 
Beverley PortlockCommented:
I believe I have answered this in your related question. In that question I load the values into PHP arrays were they are then accessible. Since you only ever have one attribute we can be lazy about this.

See my other comment on the other question about the CDATA delimiters

<?php
ini_set('display_errors',1); error_reporting(E_ALL);


$data = '<?xml version=\'1.0\'  encoding=\'Cp1252\' ?>
<RESULTS>
    <ROW>
        <COLUMN NAME="BUSINESS_NAME">Jones, Bob</COLUMN>
        <COLUMN NAME="STATION_NUMBER">2</COLUMN>
        <COLUMN NAME="STATION">RECEPTION</COLUMN>
    </ROW>
    <ROW>
        <COLUMN NAME="BUSINESS_NAME">Johnson, Steve</COLUMN>
        <COLUMN NAME="STATION_NUMBER">2</COLUMN>
        <COLUMN NAME="STATION">RECEPTION</COLUMN>
    </ROW>
    <ROW>
        <COLUMN NAME="BUSINESS_NAME">Smith, John</COLUMN>
        <COLUMN NAME="STATION_NUMBER">4</COLUMN>
        <COLUMN NAME="STATION">ACCOUNTANT</COLUMN>
    </ROW>
    <ROW>
        <COLUMN NAME="BUSINESS_NAME">Agard, Joe</COLUMN>
        <COLUMN NAME="STATION_NUMBER">3</COLUMN>
        <COLUMN NAME="STATION">LEGAL</COLUMN>
    </ROW>
</RESULTS>
';


$xml = simplexml_load_string( $data );


$arr = array();
foreach ( $xml->ROW as $aRow ) {

     $column = array();
     foreach( $aRow->COLUMN as $index => $aColumn ) {

          $attribName = (string) $aColumn->attributes();
          $column [ $attribName ] = (string) $aColumn;
     }

     $arr [] =  $column;
}


print_r( json_encode( $arr ) );

Open in new window

0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_thawts.php

Outputs:
RECEPTION
RECEPTION
ACCOUNTANT
LEGAL
string(34) "["RECEPTION","ACCOUNTANT","LEGAL"]"
<?php // RAY_temp_thawts.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xml = <<<XML
<?xml version='1.0'  encoding='Cp1252' ?>
<RESULTS>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Jones, Bob]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[2]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[RECEPTION]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Johnson, Steve]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[2]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[RECEPTION]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Smith, John]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[4]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[ACCOUNTANT]]></COLUMN>
	</ROW>
	<ROW>
		<COLUMN NAME="BUSINESS_NAME"><![CDATA[Agard, Joe]]></COLUMN>
		<COLUMN NAME="STATION_NUMBER"><![CDATA[3]]></COLUMN>
		<COLUMN NAME="STATION"><![CDATA[LEGAL]]></COLUMN>
	</ROW>
</RESULTS>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO FIND THE NAMES
$array = array();
foreach ($obj->ROW as $row)
{
    $name = (string)$row->COLUMN[2];
    echo PHP_EOL . $name;
    $array[] = $name;
}

$array = array_values(array_unique($array));
$str = json_encode($array);
echo PHP_EOL;
var_dump($str);

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now