Skip to content
Advertisement

php array access top object text in a foreach loop

I have an array in php.

array(2) { 
    ["Some text"]=> array(174) { 
        ["type"]=> string(4) "None" 
        ["modified"]=> string(0) "" 
        ["language"]=> string(2) "en" 
        ["license"]=> string(4) "None" 
        ["rightsHolder"]=> string(4) "None" 
        ["accessRights"]=> string(4) "None" 
        ["bibliographicCitation"]=> string(4) "None" 
        ["references"]=> string(4) "None" 
        ["institutionID"]=> string(4) "None" 
        ["collectionID"]=> string(4) "None" ["datasetID"]=> string(13) "5ff457cf0dfe0" 
        ["institutionCode"]=> string(4) "None" 
        ["collectionCode"]=> string(4) "None" 
        ["datasetName"]=> string(4) "None" 
        ["ownerInstitutionCode"]=> string(10) "phenome10k" 
        ["basisOfRecord"]=> string(4) "None" 
        ["informationWithheld"]=> string(4) "None" 
        ["dataGeneralizations"]=> string(4) "None" 
        ["dynamicProperties"]=> string(4) "None" 
        ["occurrenceID"]=> string(4) "None" 
        ["catalogNumber"]=> string(4) "None" 
        ["recordNumber"]=> string(4) "None" 
        ["recordedBy"]=> string(4) "None" 
        ["individualCount"]=> string(4) "None" 
        ["organismQuantity"]=> string(4) "None" 
        ["organismQuantityType"]=> string(4) "None" 
        ["sex"]=> string(4) "None" 
        ["lifeStage"]=> string(4) "None" 
        ["reproductiveCondition"]=> string(4) "None" 
        ["behavior"]=> string(4) "None" 
        ["establishmentMeans"]=> string(4) "None" 
        ["degreeOfEstablishment"]=> string(4) "None" 
        ["pathway"]=> string(4) "None" 
        ["occurrenceStatus"]=> string(4) "None" 
        ["preparations"]=> string(4) "None" 
        ["disposition"]=> string(4) "None" 
        ["associatedMedia"]=> string(4) "None" 
        ["associatedReferences"]=> string(4) "None" 
        ["associatedSequences"]=> string(4) "None" 
        ["associatedTaxa"]=> string(4) "None" 
        ["occurrenceRemarks"]=> string(4) "None" 
        ["organismID"]=> string(4) "None" 
        ["organismName"]=> string(4) "None" 
        ["organismScope"]=> string(4) "None" 
        ["associatedOccurrences"]=> string(4) "None" 
        ["associatedOrganisms"]=> string(4) "None" 
        ["previousIdentifications"]=> string(4) "None" 
        ["organismRemarks"]=> string(4) "None" 
        ["materialSampleID"]=> string(4) "None" 
        ["eventID"]=> string(4) "None" 
        ["parentEventID"]=> string(4) "None" 
        ["fieldNumber"]=> string(4) "None" 
        ["eventDate"]=> string(4) "None" 
        ["eventTime"]=> string(4) "None" 
        ["startDayOfYear"]=> string(4) "None" 
        ["endDayOfYear"]=> string(4) "None" 
        ["year"]=> string(4) "None" 
        ["month"]=> string(4) "None" 
        ["day"]=> string(4) "None" 
        ["verbatimEventDate"]=> string(4) "None" 
        ["habitat"]=> string(4) "None" 
        ["samplingProtocol"]=> string(4) "None" 
        ["sampleSizeValue"]=> string(4) "None" 
        ["sampleSizeUnit"]=> string(4) "None" 
        ["samplingEffort"]=> string(4) "None" 
        ["fieldNotes"]=> string(4) "None" 
        ["eventRemarks"]=> string(4) "None" 
        ["locationID"]=> string(4) "None" 
        ["higherGeographyID"]=> string(4) "None" ["higherGeography"]=> string(4) "None" ["continent"]=> string(4) "None" ["waterBody"]=> string(4) "None" ["islandGroup"]=> string(4) "None" ["island"]=> string(4) "None" ["country"]=> string(4) "None" ["countryCode"]=> string(4) "None" ["stateProvince"]=> string(4) "None" ["county"]=> string(4) "None" ["municipality"]=> string(4) "None" ["locality"]=> string(4) "None" ["verbatimLocality"]=> string(4) "None" ["minimumElevationInMeters"]=> string(4) "None" ["maximumElevationInMeters"]=> string(4) "None" ["verbatimElevation"]=> string(4) "None" ["minimumDepthInMeters"]=> string(4) "None" ["maximumDepthInMeters"]=> string(4) "None" ["verbatimDepth"]=> string(4) "None" ["minimumDistanceAboveSurfaceInMeters"]=> string(4) "None" ["maximumDistanceAboveSurfaceInMeters"]=> string(4) "None" ["locationAccordingTo"]=> string(4) "None" ["locationRemarks"]=> string(4) "None" ["decimalLatitude"]=> string(4) "None" ["decimalLongitude"]=> string(4) "None" ["geodeticDatum"]=> string(4) "None" ["coordinateUncertaintyInMeters"]=> string(4) "None" ["coordinatePrecision"]=> string(4) "None" ["pointRadiusSpatialFit"]=> string(4) "None" ["verbatimCoordinates"]=> string(4) "None" ["verbatimLatitude"]=> string(4) "None" ["verbatimLongitude"]=> string(4) "None" ["verbatimCoordinateSystem"]=> string(4) "None" ["verbatimSRS"]=> string(4) "None" ["footprintWKT"]=> string(4) "None" ["footprintSRS"]=> string(4) "None" ["footprintSpatialFit"]=> string(4) "None" ["georeferencedBy"]=> string(4) "None" ["georeferencedDate"]=> string(4) "None" ["georeferenceProtocol"]=> string(4) "None" ["georeferenceSources"]=> string(4) "None" ["georeferenceVerificationStatus"]=> string(4) "None" ["georeferenceRemarks"]=> string(4) "None" ["geologicalContextID"]=> string(4) "None" ["earliestEonOrLowestEonothem"]=> string(4) "None" ["latestEonOrHighestEonothem"]=> string(4) "None" ["earliestEraOrLowestErathem"]=> string(4) "None" ["latestEraOrHighestErathem"]=> string(4) "None" ["earliestPeriodOrLowestSystem"]=> string(4) "None" ["latestPeriodOrHighestSystem"]=> string(4) "None" ["earliestEpochOrLowestSeries"]=> string(4) "None" ["latestEpochOrHighestSeries"]=> string(4) "None" ["earliestAgeOrLowestStage"]=> string(4) "None" ["latestAgeOrHighestStage"]=> string(4) "None" ["lowestBiostratigraphicZone"]=> string(4) "None" ["highestBiostratigraphicZone"]=> string(4) "None" ["lithostratigraphicTerms"]=> string(4) "None" ["group"]=> string(4) "None" ["formation"]=> string(4) "None" ["member"]=> string(4) "None" ["bed"]=> string(4) "None" ["identificationID"]=> string(4) "None" ["identificationQualifier"]=> string(4) "None" ["typeStatus"]=> string(4) "None" ["identifiedBy"]=> string(4) "None" ["dateIdentified"]=> string(4) "None" ["identificationReferences"]=> string(4) "None" ["identificationVerificationStatus"]=> string(4) "None" ["identificationRemarks"]=> string(4) "None" ["taxonID"]=> string(4) "None" ["scientificNameID"]=> string(4) "None" ["acceptedNameUsageID"]=> string(4) "None" ["parentNameUsageID"]=> string(4) "None" ["originalNameUsageID"]=> string(4) "None" ["nameAccordingToID"]=> string(4) "None" ["namePublishedInID"]=> string(4) "None" ["taxonConceptID"]=> string(4) "None" ["scientificName"]=> string(4) "None" ["acceptedNameUsage"]=> string(4) "None" ["parentNameUsage"]=> string(4) "None" ["originalNameUsage"]=> string(4) "None" ["nameAccordingTo"]=> string(4) "None" ["namePublishedIn"]=> string(4) "None" ["namePublishedInYear"]=> string(4) "None" ["higherClassification"]=> string(7) "Eukarya" ["kingdom"]=> string(8) "Animalia" ["phylum"]=> string(8) "Chordata" ["class"]=> string(4) "Aves" ["order"]=> string(15) "Aegotheliformes" ["family"]=> string(12) "Aegothelidae" ["genus"]=> string(10) "Aegotheles" ["subgenus"]=> string(4) "None" ["specificEpithet"]=> string(4) "None" ["infraspecificEpithet"]=> string(4) "None" ["taxonRank"]=> string(4) "None" ["verbatimTaxonRank"]=> string(4) "None" ["scientificNameAuthorship"]=> string(4) "None" ["vernacularName"]=> string(25) "australian_owlet-nightjar" ["nomenclaturalCode"]=> string(4) "None" ["taxonomicStatus"]=> string(4) "None" ["nomenclaturalStatus"]=> string(4) "None" ["taxonRemarks"]=> string(4) "None" ["commonName"]=> string(4) "None" ["elementName"]=> string(7) "Cranium" ["commonElementName"]=> string(4) "None" ["fileName"]=> string(33) "australian_owlet-nightjar_cranium" } 
["Some more text"]=> array(174) { ["type"]=> string(4) "None" ["modified"]=> string(0) "" ["language"]=> string(2) "en" ["license"]=> string(4) "None" ["rightsHolder"]=> string(4) "None" ["accessRights"]=> string(4) "None" ["bibliographicCitation"]=> string(4) "None" ["references"]=> string(4) "None" ["institutionID"]=> string(4) "None" ["collectionID"]=> string(4) "None" ["datasetID"]=> string(13) "5ff4a281dced4" ["institutionCode"]=> string(4) "None" ["collectionCode"]=> string(4) "None" ["datasetName"]=> string(4) "None" ["ownerInstitutionCode"]=> string(3) "GIA" ["basisOfRecord"]=> string(4) "None" ["informationWithheld"]=> string(4) "None" ["dataGeneralizations"]=> string(4) "None" ["dynamicProperties"]=> string(4) "None" ["occurrenceID"]=> string(4) "None" ["catalogNumber"]=> string(4) "None" ["recordNumber"]=> string(4) "None" ["recordedBy"]=> string(4) "None" ["individualCount"]=> string(4) "None" ["organismQuantity"]=> string(4) "None" ["organismQuantityType"]=> string(4) "None" ["sex"]=> string(4) "None" ["lifeStage"]=> string(4) "None" ["reproductiveCondition"]=> string(4) "None" ["behavior"]=> string(4) "None" ["establishmentMeans"]=> string(4) "None" ["degreeOfEstablishment"]=> string(4) "None" ["pathway"]=> string(4) "None" ["occurrenceStatus"]=> string(4) "None" ["preparations"]=> string(4) "None" ["disposition"]=> string(4) "None" ["associatedMedia"]=> string(4) "None" ["associatedReferences"]=> string(4) "None" ["associatedSequences"]=> string(4) "None" ["associatedTaxa"]=> string(4) "None" ["occurrenceRemarks"]=> string(4) "None" ["organismID"]=> string(4) "None" ["organismName"]=> string(4) "None" ["organismScope"]=> string(4) "None" ["associatedOccurrences"]=> string(4) "None" ["associatedOrganisms"]=> string(4) "None" ["previousIdentifications"]=> string(4) "None" ["organismRemarks"]=> string(4) "None" ["materialSampleID"]=> string(4) "None" ["eventID"]=> string(4) "None" ["parentEventID"]=> string(4) "None" ["fieldNumber"]=> string(4) "None" ["eventDate"]=> string(4) "None" ["eventTime"]=> string(4) "None" ["startDayOfYear"]=> string(4) "None" ["endDayOfYear"]=> string(4) "None" ["year"]=> string(4) "None" ["month"]=> string(4) "None" ["day"]=> string(4) "None" ["verbatimEventDate"]=> string(4) "None" ["habitat"]=> string(4) "None" ["samplingProtocol"]=> string(4) "None" ["sampleSizeValue"]=> string(4) "None" ["sampleSizeUnit"]=> string(4) "None" ["samplingEffort"]=> string(4) "None" ["fieldNotes"]=> string(4) "None" ["eventRemarks"]=> string(4) "None" ["locationID"]=> string(4) "None" ["higherGeographyID"]=> string(4) "None" ["higherGeography"]=> string(4) "None" ["continent"]=> string(4) "None" ["waterBody"]=> string(4) "None" ["islandGroup"]=> string(4) "None" ["island"]=> string(4) "None" ["country"]=> string(4) "None" ["countryCode"]=> string(4) "None" ["stateProvince"]=> string(4) "None" ["county"]=> string(4) "None" ["municipality"]=> string(4) "None" ["locality"]=> string(4) "None" ["verbatimLocality"]=> string(4) "None" ["minimumElevationInMeters"]=> string(4) "None" ["maximumElevationInMeters"]=> string(4) "None" ["verbatimElevation"]=> string(4) "None" ["minimumDepthInMeters"]=> string(4) "None" ["maximumDepthInMeters"]=> string(4) "None" ["verbatimDepth"]=> string(4) "None" ["minimumDistanceAboveSurfaceInMeters"]=> string(4) "None" ["maximumDistanceAboveSurfaceInMeters"]=> string(4) "None" ["locationAccordingTo"]=> string(4) "None" ["locationRemarks"]=> string(4) "None" ["decimalLatitude"]=> string(4) "None" ["decimalLongitude"]=> string(4) "None" ["geodeticDatum"]=> string(4) "None" ["coordinateUncertaintyInMeters"]=> string(4) "None" ["coordinatePrecision"]=> string(4) "None" ["pointRadiusSpatialFit"]=> string(4) "None" ["verbatimCoordinates"]=> string(4) "None" ["verbatimLatitude"]=> string(4) "None" ["verbatimLongitude"]=> string(4) "None" ["verbatimCoordinateSystem"]=> string(4) "None" ["verbatimSRS"]=> string(4) "None" ["footprintWKT"]=> string(4) "None" ["footprintSRS"]=> string(4) "None" ["footprintSpatialFit"]=> string(4) "None" ["georeferencedBy"]=> string(4) "None" ["georeferencedDate"]=> string(4) "None" ["georeferenceProtocol"]=> string(4) "None" ["georeferenceSources"]=> string(4) "None" ["georeferenceVerificationStatus"]=> string(4) "None" ["georeferenceRemarks"]=> string(4) "None" ["geologicalContextID"]=> string(4) "None" ["earliestEonOrLowestEonothem"]=> string(4) "None" ["latestEonOrHighestEonothem"]=> string(4) "None" ["earliestEraOrLowestErathem"]=> string(4) "None" ["latestEraOrHighestErathem"]=> string(4) "None" ["earliestPeriodOrLowestSystem"]=> string(4) "None" ["latestPeriodOrHighestSystem"]=> string(4) "None" ["earliestEpochOrLowestSeries"]=> string(4) "None" ["latestEpochOrHighestSeries"]=> string(4) "None" ["earliestAgeOrLowestStage"]=> string(4) "None" ["latestAgeOrHighestStage"]=> string(4) "None" ["lowestBiostratigraphicZone"]=> string(4) "None" ["highestBiostratigraphicZone"]=> string(4) "None" ["lithostratigraphicTerms"]=> string(4) "None" ["group"]=> string(4) "None" ["formation"]=> string(4) "None" ["member"]=> string(4) "None" ["bed"]=> string(4) "None" ["identificationID"]=> string(4) "None" ["identificationQualifier"]=> string(4) "None" ["typeStatus"]=> string(4) "None" ["identifiedBy"]=> string(4) "None" ["dateIdentified"]=> string(4) "None" ["identificationReferences"]=> string(4) "None" ["identificationVerificationStatus"]=> string(4) "None" ["identificationRemarks"]=> string(4) "None" ["taxonID"]=> string(4) "None" ["scientificNameID"]=> string(4) "None" ["acceptedNameUsageID"]=> string(4) "None" ["parentNameUsageID"]=> string(4) "None" ["originalNameUsageID"]=> string(4) "None" ["nameAccordingToID"]=> string(4) "None" ["namePublishedInID"]=> string(4) "None" ["taxonConceptID"]=> string(4) "None" ["scientificName"]=> string(4) "None" ["acceptedNameUsage"]=> string(4) "None" ["parentNameUsage"]=> string(4) "None" ["originalNameUsage"]=> string(4) "None" ["nameAccordingTo"]=> string(4) "None" ["namePublishedIn"]=> string(4) "None" ["namePublishedInYear"]=> string(4) "None" ["higherClassification"]=> string(7) "Eukarya" ["kingdom"]=> string(8) "Animalia" ["phylum"]=> string(8) "Chordata" ["class"]=> string(8) "Mammalia" ["order"]=> string(12) "Artiodactyla" ["family"]=> string(7) "Bovidae" ["genus"]=> string(4) "Ovis" ["subgenus"]=> string(4) "None" ["specificEpithet"]=> string(4) "None" ["infraspecificEpithet"]=> string(4) "None" ["taxonRank"]=> string(4) "None" ["verbatimTaxonRank"]=> string(4) "None" ["scientificNameAuthorship"]=> string(4) "None" ["vernacularName"]=> string(5) "sheep" ["nomenclaturalCode"]=> string(4) "None" ["taxonomicStatus"]=> string(4) "None" ["nomenclaturalStatus"]=> string(4) "None" ["taxonRemarks"]=> string(4) "None" ["commonName"]=> string(4) "None" ["elementName"]=> string(5) "Tibia" ["commonElementName"]=> string(4) "None" ["fileName"]=> string(11) "sheep_tibia" }
}

In this case there are 2 objects but there could be more. I use a foreach loop and I get 2 bootstrap cards. But I can’t figure out how to get the Some text and Some more text to display as the cards h5 text. How do I do this?

  <?php
  $result = array();
  foreach ($data as $element) {
      $result[$element['ownerInstitutionCode']] = $element;
  }
echo var_dump($result);
  echo "<div class='row'>";
  foreach($result as $elem)  {
      echo(
      '
  <div class="col-sm-3 pt-2">
    <div class="card">
      <div class="card-body">
        <h5 class="card-title">'.$elem.' </h5>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="" class="btn btn-primary float-right">Open</a>
      </div>
    </div>
  </div>
'
);}
echo "</div>";
?>

Advertisement

Answer

You could use the $key => $value syntax to get both the key and the value with foreach:

foreach($result as $key => $elem)  {
      echo(
      '
  <div class="col-sm-3 pt-2">
    <div class="card">
      <div class="card-body">
        <h5 class="card-title">'.$key.' </h5>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="" class="btn btn-primary float-right">Open</a>
      </div>
    </div>
  </div>
'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement