从XML转换 - > PHP数组 - > Json编码

时间:2014-02-04 19:21:36

标签: php jquery xml json

我正在使用jquery tokeninput插件和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>
        etc..
    </majors>

我将它加载到PHP中:

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

然后我想做以下事情:

  • 将每个<major>元素重新格式化为字符串,包括program属性

    • (例如:<major program="GCIS">Computing &amp; Info Sci (PHD)</major>变成字符串GCIS - Computing &amp; Info Sci (PHD)
  • 通过过滤器函数运行转换后的字符串。过滤器函数检查strpos($convertedString, $userQuery),如果用户的查询存在,则返回true / false
  • 包含$userQuery的元素全部用json_encode($arr)
  • 编码
  • 返回JSON数据。

这是我目前的代码......我似乎无法使格式化/过滤正常工作。

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;
}

最终结果JSON输出应如下所示:

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

3 个答案:

答案 0 :(得分:1)

在您的迭代行中,您没有附加新条目 -

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

这样输出就是 -

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

答案 1 :(得分:0)

试试这个:

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);

答案 2 :(得分:0)

感谢您收到的输入,但我能够找到解决方案。

以下是感兴趣的人的最终PHP脚本。

<?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);

}

?>