jeudi 27 septembre 2018

PHP: merge two multimensioanl arrays into one

I try to create a multi-dimensional array ($list) which should look like:

[
  {"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. 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). Tricky bit is that there is foreach inside 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++;

Aucun commentaire:

Enregistrer un commentaire