PHP:使用XML字符串

时间:2015-07-11 21:15:09

标签: php arrays xml

我想根据以下XML的工资顺序排列一系列玩家。请注意,我已经按工资对篮球队进行排序。

<?php
$string = <<<EOS
<Sports_Team>
<Basketball>
<Players>Tom, Jack, Sue</Players>
<salary>4</salary>
</Basketball>
<Basketball>
<Players>Josh, Lee, Carter, Bennett</Players>
<salary>6</salary>
</Basketball>
<Basketball>
<Players>Mary, Jimmy, Nancy</Players>
<salary>44</salary>
</Basketball>
</Sports_Team>
EOS;

$xml = simplexml_load_string($string);

$trees = $xml->xpath('/Sports_Team/Basketball');

function sort_trees($t1, $t2) {
    return strcmp($t1['salary'], $t2['salary']);
}

usort($trees, 'sort_trees');
var_dump($trees);
?>

我想从$trees制作一系列玩家。如何创建一个数组对象:

[0]-> Mary, Jimmy, Nancy
[1]-> Josh, Lee, Carter, Bennett
[2]-> Tom, Jack, Sue

此外,一旦我存储了我的阵列,我该如何在视觉上打印出来?

2 个答案:

答案 0 :(得分:2)

基本上你完全正确地做了一切,除了我将在下面提到的几个小部分:)

  • 在用户定义的比较函数&#39; sort_trees&#39;中,最好直接比较整数而不是字符串,因此无需使用(strcmp)比较字符串。
  • 您也可以使用uasort()方法代替usort()来维护索引关联

所以你的代码改变很小,可能就像下面那样,最后我用print_r()方法按照你的要求打印数组

<?php

function sort_trees_by_salary($t1, $t2)
{
    return (int)$t1['salary'] > (int)$t2['salary'];
}


function sort_trees_by_number_of_players($t1, $t2)
{
    return substr_count($t1->Players, ',') > substr_count($t2->Players, ',');
}


$string = <<<EOS
<Sports_Team>
<Basketball>
<Players>Tom, Jack, Sue</Players>
<salary>4</salary>
</Basketball>
<Basketball>
<Players>Josh, Lee, Carter, Bennett</Players>
<salary>6</salary>
</Basketball>
<Basketball>
<Players>Mary, Jimmy, Nancy</Players>
<salary>44</salary>
</Basketball>
</Sports_Team>
EOS;

$xml = simplexml_load_string($string);

$trees = $xml->xpath('/Sports_Team/Basketball');

// Lets say you want to sort by salary
uasort($trees, 'sort_trees_by_salary');
$results = [];
foreach ($trees as $tree) {
    $results[] = (string)$tree->Players;
}

echo 'Sorted by Salary:';
print_r($results);


// Lets say you want to sort by number of players
uasort($trees, 'sort_trees_by_number_of_players');
$results = [];
foreach ($trees as $tree) {
    $results[] = (string)$tree->Players;
}

echo 'Sorted by number of players:';
print_r($results);

输出:

Sorted by Salary:Array
(
    [0] => Mary, Jimmy, Nancy
    [1] => Josh, Lee, Carter, Bennett
    [2] => Tom, Jack, Sue
)
Sorted by number of players:Array
(
    [0] => Mary, Jimmy, Nancy
    [1] => Tom, Jack, Sue
    [2] => Josh, Lee, Carter, Bennett
)

请注意:考虑到user-defined comparison function 与参考一起工作,上面的示例将对您的数据集应用两种排序方法,首先根据工资排序列表,然后基于工资关于号码球员

答案 1 :(得分:0)

假设你有PHP 5.3+,试试这个:

$playersArray = array_map(
    create_function('$inputArray', 'return (string) $inputArray->Players;'),
    $trees
);

例如:

php > var_dump($playersArray);
array(3) {
  [0]=>
  string(18) "Mary, Jimmy, Nancy"
  [1]=>
  string(26) "Josh, Lee, Carter, Bennett"
  [2]=>
  string(14) "Tom, Jack, Sue"
}

如果您使用较旧的PHP,则需要对array_map()使用真实(非匿名)函数,或使用create_function()

$playersArray = array_map(
    create_function('$inputArray', 'return (array) $inputArray->Players;'),
    $users
);

要回答最后一部分,你如何在视觉上打印出来,这取决于!如果您只是想将其转储以查看它以进行调试,请使用var_dump()print_r()。两者都将数组变量作为唯一需要的参数。 var_dump()有点冗长。

这是var_dump()manual):

php > var_dump($playersArray);
array(3) {
  [0]=>
  string(18) "Mary, Jimmy, Nancy"
  [1]=>
  string(26) "Josh, Lee, Carter, Bennett"
  [2]=>
  string(14) "Tom, Jack, Sue"
}

这是print_r()manual):

php > print_r($playersArray);
Array
(
    [0] => Mary, Jimmy, Nancy
    [1] => Josh, Lee, Carter, Bennett
    [2] => Tom, Jack, Sue
)

否则,要根据数组处理和显示最终用户的输出,您可能希望循环并处理它,或者使用类似于上面的array_map()来生成输出。另外,如果您想在PHP Console中尝试使用与我在示例中所做的相似的操作,则可以运行php -a并在提示符下键入代码。

修改

要回答评论中的问题,请尝试以下方法:

/* Sort by the number of ',' characters in the string. */
function sort_players($a, $b) {
    $ca = substr_count($a, ',');
    $cb = substr_count($b, ',');
    if ($ca == $cb) return 0;
    return ($ca > $cb) ? -1 : 1;
}

usort($playersArray, 'sort_players');