php - PHP:将两个多维度数组合并为一个
问题描述
我尝试创建一个多维数组 ( $list
),它应该如下所示:
[
{"uri": "http://www.example.com/1", "traverseCount": "1"},
{"uri": "http://www.example.com/2", "traverseCount": "1"},
{"uri": "http://www.example.com/3", "traverseCount": "1"}
]
但是,我不知道如何从以下数据中最好地实现这一点。这两个数据源来自两个If-Else
,并尝试为每个案例创建一个多维数组。但是,最后,我想生成一个多维数组,它将两个多维数组与连续的有序键结合起来。(所以,从上面的例子来看,http://www.example.com/1和http://www.example.com/2来自 1st 的结果Else-If
,而http://www.example.com/ 3来自于 2 Else-If
)。棘手的一点是foreach
里面有Else-If
。
// Create an empty multidimentional array to accommodate 2 datasets (2 If-Else cases) below:
$traverseCount = 1;
$list = array(
array(),
array()
);
//This is the first data source. Say, it returns 2 URIs when doing the foreach loop:
$match = $graph->resourcesMatching('skos:exactMatch');
if (isset($match) == true && count($match) > 0){
$graphuris = $match[0]->all('skos:exactMatch');
echo '<b>skos:exactMatch Links: </b><br>';
foreach ($graphuris as $uris) {
$counter = 0;
$list[$counter][0] = $uris->__toString();
$list[$counter][1] = $traverseCount;
$counter++;
echo '<a href="'.$uris.'" target="_blank">'.$uris.'</a><br>';
}
}
else {
echo '<p>No skos:exactMatch found</p>';
}
//This is the second data source, whose multidimensional array should be added to the end of the previous multidimensional array
$match2 = $graph->resourcesMatching('rdfs:seeAlso');
if (isset($match2) == true && count($match2) > 0){
$graphuris2 = $match2[0]->all('rdfs:seeAlso');
echo '<b>rdfs:seeAlso Links: </b><br>';
foreach ($graphuris2 as $uris2) {
$counter = 0;
$list[$counter][0] = $uris2->__toString();
$list[$counter][1] = $traverseCount;
$counter++;
echo '<a href="'.$uris2.'" target="_blank">'.$uris2.'</a><br>';
}
}
else{
echo '<p>No rdfs:seeAlso found</p><br>';
}
// $traverseCount is always 1 until here, but it needs to add 1 for the next script to run.
$traverseCount++;
解决方案
它不需要那么复杂。添加到数组中无需任何$counter
类似的操作即可完成。
// Create an empty multidimentional array to accommodate 2 datasets (2 If-Else cases) below:
$traverseCount = 1;
$list = array();
//This is the first data source. Say, it returns 2 URIs when doing the foreach loop:
$match = $graph->resourcesMatching('skos:exactMatch');
if (isset($match) == true && count($match) > 0){
$graphuris = $match[0]->all('skos:exactMatch');
echo '<b>skos:exactMatch Links: </b><br>';
foreach ($graphuris as $uris) {
$list[] = array('uri' => $uris->__toString(),
'traversecount' => $traverseCount
);
echo '<a href="'.$uris.'" target="_blank">'.$uris.'</a><br>';
}
}
else {
echo '<p>No skos:exactMatch found</p>';
}
//This is the second data source, whose multidimensional array should be added to the end of the previous multidimensional array
$match2 = $graph->resourcesMatching('rdfs:seeAlso');
if (isset($match2) == true && count($match2) > 0){
$graphuris2 = $match2[0]->all('rdfs:seeAlso');
echo '<b>rdfs:seeAlso Links: </b><br>';
foreach ($graphuris2 as $uris2) {
$list[] = array('uri' => $uris2->__toString(),
'traversecount' => $traverseCount
);
echo '<a href="'.$uris2.'" target="_blank">'.$uris2.'</a><br>';
}
}
else{
echo '<p>No rdfs:seeAlso found</p><br>';
}
// $traverseCount is always 1 until here, but it needs to add 1 for the next script to run.
$traverseCount++;
我有点困惑,
$traverseCount
因为在两个循环完成之前,您的代码似乎没有改变。
推荐阅读
- python - python中face_recognition模块期间的未知错误
- php - 使用驱动器 SDK 将大型 zip 文件上传到谷歌驱动器给出错误
- html - Bootstrap 4 卡片页脚未与页面底部对齐
- c++ - 同步访问以提升 async_write
- c - 避免使用相似结构重复代码 C
- c# - Odata V4 $Select after $expand on one to one 关系
- vba - vlookup 和索引/匹配组合的字符限制
- reactjs - 当你失去焦点时如何做某事,没有 onBlur
- php - 上传的文件超过了 php.ini 中的 upload_max_filesize 指令。无法在 wordpress 中上传新主题
- python - 无法卸载“熊猫”(distutils 安装项目)