简体   繁体   English

PHP:将两个multimensioanl数组合并为一个

[英]PHP: merge two multimensioanl arrays into one

I try to create a multi-dimensional array ( $list ) which should look like: 我尝试创建一个多维数组( $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"}
]

But, I don't know how best I can achieve this from the following data. 但是,我不知道如何从以下数据中实现这一目标。 The two data sources come from two If-Else and try to create an multi-dimensional array for each case. 这两个数据源来自两个If-Else尝试为每个案例创建一个多维数组。 But, at the end, I would like to generate a multidimensional array which combines the two multi-dimensional array with continuous ordered keys. 但是,最后,我想生成一个多维数组,它将两个多维数组与连续有序键组合在一起。 (So, from the above example, http://www.example.com/1 and http://www.example.com/2 comes from the outcome of the 1st Else-If , and http://www.example.com/3 comes from the 2nd Else-If ). (所以,从上面的例子中, http://www.example.com/1http://www.example.com/2来自1的结果Else-IfHTTP://www.example .com / 3来自第二个Else-If )。 Tricky bit is that there is foreach inside Else-If . 棘手的是,在Else-If中有foreach

// 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++;

It does not need to be that complicated. 它不需要那么复杂。 Adding to the array can be done without any need for the $counter like this. 添加到数组可以完成,而不需要像这样的$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++;

I am a bit confused about $traverseCount as it does not seem to change in your code until both loops are completed. 我对$traverseCount感到有点困惑,因为在完成两个循环之前,代码似乎没有改变。

You always reset this $counter to 0 in each iteration. 您总是在每次迭代中将此$counter重置为0。 Instead just append new element to array: 而只是将新元素追加到数组:

$list = [];

if () {
    foreach () {
        $list[] = [
            $uris->__toString(),
            $traverseCount
        ];
    }
}

Here I don't use counter, just adding new element to array using $list[] and indexes will auto-increment. 这里我不使用counter,只需使用$list[]向数组添加新元素,索引将自动递增。

$uris->__toString() and $traverseCount automatically put to 0 and 1 indexes $uris->__toString()$traverseCount自动放到0和1索引


I don't see how your $traverseCount changes as it's assigned 1 at the beginning and only at the end it's incremented. 我没有看到你的$traverseCount如何改变,因为它在开始时分配为1,并且只在最后它递增。

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

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