douying0108 2014-02-04 19:21
浏览 48
已采纳

从XML转换 - > PHP数组 - > 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:

  • reformat each <major>element into string, including program attribute

    • (ex: <major program="GCIS">Computing &amp; Info Sci (PHD)</major> turns into string GCIS - Computing &amp; Info Sci (PHD)
  • run the converted string through a filter function. The filter function checks for strpos($convertedString, $userQuery) and returns true/false if the user's query is present
  • elements which DO contain the $userQuery are all then encoded with json_encode($arr)
  • return JSON data.

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)"}
  • 写回答

3条回答 默认 最新

  • douzuo0002 2014-02-04 20:54
    关注

    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);
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。