tdc
tdc

Reputation: 5464

converting from XML -> PHP Array -> Json Encode

I am setting up an input autocomplete using jquery tokeninput plugin and XML data.

I have an XML file structured as shown:

<?xml version="1.0" encoding="UTF-8"?>
    <majors>
        <major program="GCIS">Computing &amp; Info Sci (PHD)</major>
        <major program="UINT">Business Administration (AAS)</major>
        etc..
    </majors>

I load it into PHP:

$majors = simplexml_load_file('../ajax/majors.xml');

I then want to do the following things:

This is the code I currently have... I can't seem to get the formatting / filtering to work correctly.

if(isset($_POST['query']) ) {
    $majors = simplexml_load_file('../ajax/majors.xml');

    # iterate through.
    foreach ($majors as $key => $value) {
        $arr = array('major' => $value['program'] . " - " . $value);
    }

    # filter the response using our query
    $arr = array_filter($arr, 'filterArrayWithQuery');

    # JSON-encode the response
    $json_response = json_encode($arr);

    # Return the response
    return $json_response;
}

# ensures that the search query is present
function filterArrayWithQuery( $string ) {
    return !strpos( $string, $query ) === false;
}

The end-result JSON output should look like this:

{"major":"GCIS - Computing &amp; Info Sci (PHD)","major":"UINT - Business Administration (AAS)"}

Upvotes: 0

Views: 526

Answers (3)

tdc
tdc

Reputation: 5464

Thank you to the input I received, however I was able to figure out the solution.

Here is the final PHP script for those interested.

<?php


# vars
$reqMethod    = $_SERVER['REQUEST_METHOD'];
$ref          = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : "http://www.google.com";                            
$allowedHosts = array('redacted', 'localhost'); 
$majors       = null;
$arr          = array();
$query        = isset($_POST['q']) ?  $_POST['q'] : "";

# confirm request is being sent from where we allow.
if ( $reqMethod != 'POST' || !in_array(parse_url($ref, PHP_URL_HOST), $allowedHosts) ) {

    header('Location: ' . $ref);

} else {

    # load in XML
    $majors = simplexml_load_file('../ajax/majors.xml');

    # iterate through XML. if element contains query, add it to an array.
    foreach ($majors as $key => $value) {
        if( !stristr( $value, $query ) === false ){
            $arr[] = array('major' => $value['program'] . " - " . $value);
        }
    }

    # Return the response - JSON encoded
    header('Content-type: application/json');
    echo json_encode($arr);

}

?>

Upvotes: 0

hindmost
hindmost

Reputation: 7195

Try this:

function convert($xml) {
    # ensures that the search query is present
    function filterArrayWithQuery($string) {
        static $query = null;
        if (is_null($query) && isset($_POST['query'])) $query = $_POST['query'];
        return !$query || strpos($string, $_POST['query']) !== false;
    }

    $majors = simplexml_load_string($xml);
    $arr = array();

    # iterate through
    foreach ($majors as $key => $value) {
        $arr[] = array('major' => $value['program'] . " - " . htmlentities($value));
    }

    # filter the response using our query
    $arr = array_filter($arr, 'filterArrayWithQuery');

    # Return the response (JSON-encoded)
    return json_encode(array('majors' => $arr));
}

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
    <majors>
        <major program="GCIS">Computing &amp; Info Sci (PHD)</major>
        <major program="UINT">Business Administration (AAS)</major>
    </majors>
XML;

$_POST['query'] = 'Info';
echo convert($xml);

Upvotes: 0

Kamehameha
Kamehameha

Reputation: 5473

In your iterate through line, you are not appending new entries -

# iterate through.
foreach ($majors as $key => $value) {
   $arr[] = array('major' => $value['program'] . " - " . $value);
//     ^^
}

With this the output would be-

[{"major":"GCIS - Computing & Info Sci (PHD)"},{"major":"UINT - Business Administration (AAS)"}]

Upvotes: 1

Related Questions