Php 按值对xml中的数组进行分组

Php 按值对xml中的数组进行分组,php,arrays,sorting,Php,Arrays,Sorting,您好,我需要使用标记curso作为键,在标记identificator之间对用户名进行排序,如上面的链接所示。我尝试过这个解决方案,但仍然有问题 $dataClip给我用户名(identificator),$dataClipCourse给我课程(curso) 编辑:processXML为我提供所有用户名(identificadores)和processXMLCourse所有课程(curso) a、 苏维埃拉 aj.桑帕约 赖斯 a、 皮埃达德 塔瓦雷斯 a、 梅塞罗 皮雷斯 ac.维埃拉 里贝罗

您好,我需要使用标记curso作为键,在标记identificator之间对用户名进行排序,如上面的链接所示。我尝试过这个解决方案,但仍然有问题

$dataClip给我用户名(identificator),$dataClipCourse给我课程(curso)

编辑:processXML为我提供所有用户名(identificadores)和processXMLCourse所有课程(curso)

a、 苏维埃拉

aj.桑帕约

赖斯

a、 皮埃达德

塔瓦雷斯

a、 梅塞罗

皮雷斯

ac.维埃拉

里贝罗

罗德里格斯学院

389

167

450

167

450

450

167

450

167

169

以下是输出的摘录:


数组([]=>Array([0]=>SimpleXMLElement对象([0]=>167)=>SimpleXMLElement对象([0]=>389)[2]=>SimpleXMLElement对象([0]=>167)[3]=>SimpleXMLElement对象([0]=>450)[4]=>SimpleXMLElement对象([0]=>167 5]=>SimpleXMLElement对象([0]=>450)[6]=>SimpleXMLElement对象([0]=>450)[7]=>SimpleXMLElement对象([0]=>167)[8]=>SimpleXMLElement对象([0]=>450)[9]=>SimpleXMLElement对象([0]=>167)[10]=>SimpleXMLElement对象([0]=>169)[11]=>SimpleXMLElement对象([0]=>167][12]=>SimpleXMLElement对象([0]=>167 13]=>SimpleXMLElement对象([0]=>167 14]=>SimpleXMLElement对象([0]=>167)[15]=>SimpleXMLElement对象([0]=>389)[16]=>SimpleXMLElement对象([0]=>167)[17]=>SimpleXMLElement对象([0]=>167)[18]=>SimpleXMLElement对象([0]=>450)[19]=>SimpleXMLElement对象([0]=>172)[20]=>SimpleXMLElement对象([0]=>167 21]=>SimpleXMLElement对象([0]=>167)[22]=>SimpleXMLElement对象([0]=>450)

首先,假设输入数据的格式如下:

$people = array( "Name A" => Array(450, 167), "Name B" => Array(629, 450) , ...);
因此,对于每个人,
$people
数组中都有一个条目,该条目以人的姓名为键,以她参加的课程数组为值。现在,将其转换为课程列表,在课程编号“倒数”旁边列出这些人数组,因此课程编号成为键,与会者列表成为值:

$courses = array( 450 => Array("Name A", "Name B"), 167 = Array("Name A"), 629 => Array("Name B") );
这可以通过以下算法实现:

$courses = array();

// Iterate all persons
foreach( $people as $name => $attendedCourses)
{
    // Iterate all attended courses of this person
    foreach( $attendedCourses as $courseNumber )
    {
        // Check if the current course was already remembered
        if( isset($courses[$courseNumber] ) )
        {
            // Add person to the list of attendees
            $courses[$courseNumber][] = $name;
        }
        else 
        {
            // Create list of attendees with the
            // current person as the single attendee
            $courses[$courseNumber] = array( $name );
        }
    }
}

// Sort by keys
ksort( $courses );

// Output
print_r( $courses );

但是您必须将SimpleXML节点列表转换为更简单的输入格式。

您使用value iteration变量在foreach中隐藏了
$dataClip
,因此
$dataClip['identificator']
没有为“原始”建立索引
$dataClip
来自外部作用域。另外,processXML和processXMLforCourse返回的确切内容是什么?我看到它们必须是SimpleXMLElement,但[0]的作用是什么=>NNN意思是?似乎根本没有“identificator”键。@Oliver您好,我已经编辑了我的帖子。你说的隐藏是什么意思?你有什么建议吗?隐藏是指通过在不同的作用域中声明一个新变量来隐藏对变量的访问。我不得不说我可能错了,因为PHP没有真正的变量作用域这意味着foreach循环将覆盖
$dataClip
的原始值。您是否也可以发布数据转储(
print\r
)$dataClip和$dataClipCourse数组的名称?我仍然不确定数据布局的外观。最终,您希望课程ID作为$new_arr中的键,人员的名称作为值?例如
数组(389=>“a.soveral”,167=>“aj.sampaio”,…)
?@Oliver最后,我想把属于167门课程的所有名字都归在它下面。与其他课程一样。类似于..167=>aj.sampaio a.piedade as.pires ac.ribeiro 389=>a.soveraldo。因此,要走的路是(伪代码):foreach(person as p){foreach(people.courses as pc){将p添加到课程[pc].peoples}对吗?执行正确的实现在很大程度上取决于实际的源数据格式。我想我可以为您编写一个半工作算法,您只需对源数据进行一点转换,以使算法的输入格式良好。谢谢,但我需要它保持此格式。我的脚本已经有了其他基于simplex的工作除非它是一个简单的转换,否则我必须彻底检查我所做的一切。我想从SimpleXML节点中获取所需的值应该很容易/编写一个包装器,根据给定的SimpleXML数据提供对这些值的访问
$courses = array();

// Iterate all persons
foreach( $people as $name => $attendedCourses)
{
    // Iterate all attended courses of this person
    foreach( $attendedCourses as $courseNumber )
    {
        // Check if the current course was already remembered
        if( isset($courses[$courseNumber] ) )
        {
            // Add person to the list of attendees
            $courses[$courseNumber][] = $name;
        }
        else 
        {
            // Create list of attendees with the
            // current person as the single attendee
            $courses[$courseNumber] = array( $name );
        }
    }
}

// Sort by keys
ksort( $courses );

// Output
print_r( $courses );