【问题标题】:PHP: merge two multimensioanl arrays into onePHP:将两个多维度数组合并为一个
【发布时间】:2018-09-27 11:11:28
【问题描述】:

我尝试创建一个多维数组 ($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/1http://www.example.com/2 来自第一个 Else-If 的结果,http://www.example.com/3 来自第二个 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++;

【问题讨论】:

    标签: php if-statement multidimensional-array foreach


    【解决方案1】:

    不需要那么复杂。不需要像这样的$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++;
    

    我对@9​​87654323@ 有点困惑,因为在两个循环完成之前它似乎不会在您的代码中发生变化。

    【讨论】:

    • 非常感谢!确实,它并没有那么复杂。有时,我可能会想太多而迷失方向...$traverseCount 有点混乱,因为它与这里的问题没有直接关系。相反,它将用于我想实现的下一个事情,这将是另一个挑战。
    • 祝你下一个挑战好运:)
    【解决方案2】:

    您总是在每次迭代中将此 $counter 重置为 0。而是将新元素附加到数组:

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

    这里我不使用计数器,只是使用$list[] 向数组添加新元素,索引会自动递增。

    $uris-&gt;__toString()$traverseCount 自动放入 0 和 1 索引


    我看不到您的 $traverseCount 会发生怎样的变化,因为它在开始时被分配为 1,并且只有在结束时才会增加。

    【讨论】:

    • 非常感谢!知道替代方案总是好的。我更喜欢使用关联数组,这是将答案归功于 @RiggsFolly 的唯一原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2023-03-28
    • 2019-10-08
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多