Php 在第二个arraylist中没有值时插入值
我有一个问题,它占用了我很多时间。虽然它应该非常简单(因为它太简单了!)。 我的问题是: 我在两个ArrayList中有这些值:Php 在第二个arraylist中没有值时插入值,php,arrays,arraylist,Php,Arrays,Arraylist,我有一个问题,它占用了我很多时间。虽然它应该非常简单(因为它太简单了!)。 我的问题是: 我在两个ArrayList中有这些值: $row[0]->COUNTER1 20 10 15 $row[0]->GRADE_POINTS 0 3 5 我应该将这些ArrayList更改为以下示例: $row[0]->COUNTER1 20 0 0 10 0 15 $ro
$row[0]->COUNTER1 20 10 15
$row[0]->GRADE_POINTS 0 3 5
我应该将这些ArrayList更改为以下示例:
$row[0]->COUNTER1 20 0 0 10 0 15
$row[0]->GRADE_POINTS 0 1 2 3 4 5
$counter1 = array(20, 10, 15);
$grade_points = array(0, 3, 5);
$new_grade_points = range(min($grade_points), max($grade_points));
foreach($new_grade_points as $key => &$value) {
// check if its part of the missing index if not get the value,
// if its the missing index put 0
$value = (in_array($key, $grade_points)) ? array_shift($counter1) : 0;
}
$counter1 = array_values($new_grade_points); // now contains 20,0,0,10,0,15
$grade_points = array_keys($new_grade_points); // now contains 0,1,2,3,4,5
print_r($counter1);
因此,缺少的值应该有0作为计数器。
虽然这并不难做到,但我可能想得太多了
我用来创建第一组数字的代码是:
$result = new SimpleXMLElement($xmlresult);
$xml = $result->children("soapenv", true)->Body->children();
$xmlBody = $xml[0];
$countPerResultaat = array();
foreach($xmlBody->query[0] as $row)
{
$countPerResultaat[] = (int) $row[0]->COUNTER1;
$xaxis[] = (string) $row[0]->GRADE_POINTS;
}
我认为可行的代码是:
for($i; $i<=10; $i++){
//foreach($xmlBody->query[0] as $row)
//{
$row = $xmlBody->query[0];
if($i==$row[0]->GRADE_POINTS){
$countPerResultaat[] = (int) $row[0]->COUNTER1;
$xaxis[] = (string) $row[0]->GRADE_POINTS;
}else{
$xaxis[] = (string) $i;
$countPerResultaat[] = (int) 0;
}
}
($i;$iquery[0]作为$row)的
//{
$row=$xmlBody->query[0];
如果($i==$row[0]->分数){
$countPerResultaat[]=(int)$row[0]->COUNTER1;
$xaxis[]=(字符串)$row[0]->GRADE_点;
}否则{
$xaxis[]=(字符串)$i;
$countPerResultaat[]=(int)0;
}
}
但是行不能使用,我真的不知道如何修复它。我唯一的解决方案是使用另一个for循环,它可能会创建100个值
谢谢你的帮助 如果我理解正确,并且
$row[0]->COUNTER1
和$row[0]->GRADE_点
是数组。您只需循环它们并在数组()中使用
。考虑这个例子:
$row[0]->COUNTER1 20 0 0 10 0 15
$row[0]->GRADE_POINTS 0 1 2 3 4 5
$counter1 = array(20, 10, 15);
$grade_points = array(0, 3, 5);
$new_grade_points = range(min($grade_points), max($grade_points));
foreach($new_grade_points as $key => &$value) {
// check if its part of the missing index if not get the value,
// if its the missing index put 0
$value = (in_array($key, $grade_points)) ? array_shift($counter1) : 0;
}
$counter1 = array_values($new_grade_points); // now contains 20,0,0,10,0,15
$grade_points = array_keys($new_grade_points); // now contains 0,1,2,3,4,5
print_r($counter1);
样本输出:
Array
(
[0] => 20
[1] => 0
[2] => 0
[3] => 10
[4] => 0
[5] => 15
)
我想你想数一数评分的次数?您应该像往常一样循环,当没有值时,您应该/可以将其定义为
0
。在此之后,只需计算阵列中有多少重复项。这样,$xaxis
的键就是等级,值就是给出等级的次数
foreach($xmlBody->query[0] as $row)
{
$counter = (int) $row[0]->COUNTER1;
if(counter) $countPerResultaat[] = $counter;
else $countPerResultaat[] = 0;
}
$xaxis = array_count_values($counter);
不知怎么的,这对我不起作用。很遗憾PHP没有本机错误日志。。。然而,$row[0]->COUNTER1和$row[0]->GRADE_点来自一个XML对象,就像我在问题的第一段代码中编辑的那样。你能帮我吗?看起来你的解决方案很接近,但还不是真正的解决方案。。。