let obj=[
{
"type": "Bedroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test"
},
{
"type": "Bedroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test1"
},
{
"type": "Bedroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test"
},
{
"type": "Bathroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test"
},
{
"type": "Bathroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test"
},
{
"type": "Bathroom",
"int_input": 1,
"enum_input": 0,
"enum_select": "Yes",
"description": "Test"
}
]
//first take the type
let type_only=obj.map(o=>o.type);
type_only=Array.from(new Set(type_only));
let sum_obj={};
let arr=[];
//iterate types
{type_only.map((val,index) =>{
// c is an object, append description only and not object
// Also, check for blank description while appending
sumobj = obj.reduce((c, v) => v.type === val ? {'description': (c.description ? c.description + ','+ v.description : v.description),'type':v.type} : c, 0)
arr.push(sumobj);
}
)}
console.log(arr);
console.log(type_only);
我想迭代每个 div.reviews-summary ,但我没有进入下一个 p.reviews-title 和 li.rating__item rating__rated 标记,也计算 li.rating__item rating__rated ,以整数显示 li.rating__item rating__rated 。
<div class="reviews-summary__stats">
<div class="reviews-summary">
<p class="reviews-title">C</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">C</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
<div class="reviews-summary">
<p class="reviews-title">C</p>
<ul class="rating">
<li class="rating__item rating__rated"></li>
<li class="rating__item rating__rated"></li>
<li class="rating__item "></li>
<li class="rating__item "></li>
</ul>
</div>
</div>
我希望像这种类型的输出一样显示
的阵
(
[overalldata] =&gt;排列
(
[a] =&gt; 3
[b] =&gt; 4
[c] =&gt; 2
)
)
并计算<?php
include("simple_html_dom.php");
$obj = new simple_html_dom();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
$item = $factor->find('p[class=reviews-title]')->plaintext;
if(trim($item) == 'A')
{
$a = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
}
if(trim($item) == 'B')
{
$b = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
}
if(trim($item) == 'C')
{
$c = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
}
$final_array['overalldata'] = array
(
'a' => $a, // no of A have <li class="rating__item rating__rated"></li>
'b' => $b,
'c' => $c,
);
}
print_r($final_array);
die;
?>
它,并在列表中显示 li.rating__item rating__rated 的整数值
任何有任何想法的机构请帮忙解决。感谢
答案 0 :(得分:2)
我做了一些更改,但包含了几个版本,因为它们都以不同的方式格式化数据。我认为主要问题是,当您使用find()
时,这可能会返回找到的项目列表,因此在设置$a
等时,您使用了第二个参数来挑选plaintext
在第一项(使用,0
)中,您在查找$item
值时没有这样做。所以我已经在此次电话会议中添加了相同内容。
$final_array=array();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
$item = $factor->find('p[class=reviews-title]',0)->plaintext;
if(trim($item) == 'A')
{
$final_array['overalldata']['a'] = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
}
if(trim($item) == 'B')
{
$final_array['overalldata']['b'] = $factor->find('li[class=rating__item rating__rated]',0)->plaintext;
}
if(trim($item) == 'C')
{
$final_array['overalldata']['c'] = $factor->find('li[class="rating__item rating__rated"]',0)->plaintext;
}
}
print_r($final_array);
这给出了(带有您的样本数据)......
Array
(
[overalldata] => Array
(
[c] =>
)
)
...替代地
$final_array=array();
foreach ($obj->find('div[class=reviews-summary]') as $factor)
{
$a = null;
$b = null;
$c = null;
$item = trim($factor->find('p[class=reviews-title]',0)->plaintext);
$factor = $factor->find('li[class=rating__item rating__rated]');
$count = count($factor);
if($item == 'A')
{
$a = $factor[0]->plaintext;
}
if($item == 'B')
{
$b = $factor[0]->plaintext;
}
if($item == 'C')
{
$c = $factor[0]->plaintext;
}
$final_array['overalldata'] = array
(
'a' => $a,
'b' => $b,
'c' => $c,
'count' =>$count
);
}
print_r($final_array);
稍微修改了一组测试数据......
Array
(
[overalldata] => Array
(
[a] =>
[b] =>
[c] => Some content
)
)
<强>更新强>
我已经更新了第二个例子,我已经将一些常用代码移到了主要部分。 $count
只是<li class="rating__item rating__rated">
项目的数量(我认为这是您的要求)。