PHP: create array from XML string

I want to make an array of players sorted by salary order from the following XML. Notice that I am already sorting basketball teams by salary.

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

      

I want to create an array of players from $trees

. How to create an array object in such a way that:

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

      

Also, once I have saved my array, how do I print it out visually?

+3


source to share


2 answers


Basically you did everything right, except for a few tiny bits, which I will cover below :)

  • In your custom comparison function "sort_trees" it is best to compare an integer directly rather than a string, so there is no need to compare a string using (strcmp).
  • Also you can use the method uasort()

    instead usort()

    to support the association of indexes

so your code, with a little change, could be something like this: and finally I use a method print_r()

to print the array as you request

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

      



Output:

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
)

      

Please note: given a custom compare function works with a link, the above example will be applied to both sorting methods on your dataset, ordering the list first based on salary and second based on the number of players

+2


source


Assuming you have PHP 5.3+, try this:

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

      

For example:

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

      

If you are using old PHP, you need to use a real (not anonymous) function for array_map()

or use create_function()

:

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

      

To answer the last part, how do you print it out visually, well, it depends! If you just want to dump it to view it for debugging purposes, use var_dump()

or print_r()

. Both take an array variable as the only required argument. var_dump()

is a little more verbose.

This is 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"
}

      

This is print_r()

( manual ):

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

      

Otherwise, to process and output the output for end users based on an array, you will most likely want to skip and process it, or use array_map()

similarly to the above to generate output. Also, if you want to experiment with this in the PHP Console similar to what I did in my examples, you can run php -a

and enter your code at the prompt.

Edit

To answer the question from the comments, try this:

/* 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');

      

0


source







All Articles