簡體   English   中英

PHP:將兩個multimensioanl數組合並為一個

[英]PHP: merge two multimensioanl arrays into one

我嘗試創建一個多維數組( $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來自1的結果Else-IfHTTP://www.example .com / 3來自第二個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++;

它不需要那么復雜。 添加到數組可以完成,而不需要像這樣的$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感到有點困惑,因為在完成兩個循環之前,代碼似乎沒有改變。

您總是在每次迭代中將此$counter重置為0。 而只是將新元素追加到數組:

$list = [];

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

這里我不使用counter,只需使用$list[]向數組添加新元素,索引將自動遞增。

$uris->__toString()$traverseCount自動放到0和1索引


我沒有看到你的$traverseCount如何改變,因為它在開始時分配為1,並且只在最后它遞增。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM