简体   繁体   English

在PHP中排序并获取数组中值的频率?

[英]Sorting and getting the frequency of values in an array in php?

I have an xml file with a list of field titles. 我有一个带有字段标题列表的xml文件。 I want to store them in an array an sort them alphabetically and then by which "title" appears the most. 我想将它们存储在数组中,然后按字母顺序对它们进行排序,然后按“标题”显示最多。 My actual XML file is huge to copy here (PS: I'm importing from a bunch of XML files). 我的实际XML文件很大,可以在此处复制(PS:我从一堆XML文件中导入)。

In a nutshell, this is an example of how the XML files look. 简而言之,这是XML文件外观的一个示例。

<add overwrite="true">
<docs>

    <field name="id">9637a08df6aa0765</field>
    <field name="url">http://somewebsite.ca </field>
    <field name="blogurl">http://www.someblog.com</field>
    <field name="published">2015-05-21</field>
    <field name="language">English</field>
    <field name="title">Stephen Harper</field>
    <field name="title">Mike Duffy Trial</field>
    <field name="title">POTUS on Twitter</field>
    <field name="title">Tim Hortons Closed</field>
    <field name="title">Stephen Harper</field>
    <field name="title">The New iPhone</field>
    <field name="title">Stephen Harper</field>
</docs>
</add>

So let's say I get the title attribute from the XML file and store them in an array. 假设我从XML文件中获取了title属性,并将它们存储在一个数组中。 I then want to sort that array in alphabetical order and then by frequency of the "titles". 然后,我想按字母顺序对该数组进行排序,然后按“标题”的频率进行排序。

This is what I have so far. 到目前为止,这就是我所拥有的。

<?php

    $titles_array = array();
    $counter = 0; 
    $xml = simplexml_load_file("fields.xml") or die ("Error: Cannot Create Object");

    foreach($xml->docs->field as $fields){
        array_push ($titles_array, $fields);
        echo $fields . "<br>";
        $counter++;  
    }

    echo '<p>' . "Sorted Array" . '</p>';

    sort($titles_array);

    for ($a=0; $a<$counter; $a++){  
        echo $titles_array[$a] . "<br>";
    }

?>

The output is actually not in alphabetical order? 输出实际上不是按字母顺序排列的吗? Also, how do I get to show the most frequent "title"? 另外,如何显示最频繁的“标题”?

Your $fields is actually going to be the whole SimpleXMLElement inside of your foreach loop. 实际上, $fields将成为foreach循环中的整个SimpleXMLElement Your arrays will sort as expected if you use this instead: 如果使用以下数组,则数组将按预期排序:

array_push($titles_array, (string)$fields);

To count the occurrences, create another array: 要计算出现次数,请创建另一个数组:

$titles_count = array();

Then in your loop, do something like this: 然后在循环中执行以下操作:

if (isset($titles_count[(string)$fields])) {
  $titles_count[(string)$fields] = $titles_count[(string)$fields] + 1;
} else {
  $titles_count[(string)$fields] = 1;
}

Finally, get your key with the highest count like this: 最后,按以下方式获取具有最高计数的密钥:

echo array_search(max($titles_count), $titles_count);

Put all that code together like this: 像这样将所有代码放在一起:

<?php

    $titles_array = array();
    $titles_count = array();
    $xml = simplexml_load_file("fields.xml") or die ("Error: Cannot Create Object");

    foreach($xml->docs->field as $fields){
      array_push ($titles_array, (string)$fields);
      echo $fields . "<br>";

      // First we check if the key exists.
      // Example $titles_count['Tim Hortons Closed']
      if (isset($titles_count[(string)$fields])) {
        // If it does, augment the count by 1;
        $titles_count[(string)$fields] = $titles_count[(string)$fields] + 1;
      } else {
        // If it doesn't yet, set the count to 1;
        $titles_count[(string)$fields] = 1;
      }
    }

    echo "<p>Sorted Array</p>";

    sort($titles_array);

    for ($a=0; $a<$counter; $a++){  
      // Let's put the count in for each one:
      echo $titles_array[$a] . "(" . $titles_count[$titles_array[$a]] . ")" . "<br>";
    }

    echo "<p>Highest key count:</p>";

    // Here we get the value with the highest count use max(...)
    // Then we get it's key (example 'Tim Hortons Closed')
    echo array_search(max($titles_count), $titles_count);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM