php,当一些数组成员缺少键时获取所有数组键

时间:2021-09-24 02:14:53

I'm searching in an array ['results'] where only some of the members of that array have the key 'taxonomicStatus'

我正在搜索数组['results'],其中只有该数组的某些成员具有键'taxonomicStatus'

There are 15 members of the array:

该阵列有15个成员:

http://api.gbif.org/v1/species?name=Micromussa%20amakusensis

When I search the array it returns a list where those with the key 'taxonomicStatus'

当我搜索数组时,它会返回一个列表,其中包含键'taxonomicStatus'的列表

$taxonomicStatus_search = array_column($taxon['results'], 'taxonomicStatus'); 

Array
(
[0] => HOMOTYPIC_SYNONYM
[1] => ACCEPTED
[2] => SYNONYM
[3] => ACCEPTED
[4] => ACCEPTED
[5] => ACCEPTED
)

Expected output:

Array
(
[0] => HOMOTYPIC_SYNONYM
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => ACCEPTED
[10] => SYNONYM
[11] => ACCEPTED
[12] => ACCEPTED
[13] => 
[14] => ACCEPTED
)

I am trying to set the return the first key=> ACCEPTED in this case 9

我试图在这种情况下设置返回第一个键=> ACCEPTED 9

2 个解决方案

#1


2  

Here you should use foreach loop instead of array_column.

在这里你应该使用foreach循环而不是array_column。

PHP code demo

PHP代码演示

<?php
ini_set('display_errors', 1);
$json='{"offset":0,"limit":20,"endOfRecords":true,"results":[{"key":4880483,"nubKey":4880483,"nameKey":7024762,"taxonID":"gbif:4880483","sourceTaxonKey":106044866,"kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Mussidae","genus":"Acanthastrea","species":"Acanthastrea amakusensis","kingdomKey":1,"phylumKey":43,"classKey":206,"orderKey":714,"familyKey":7002,"genusKey":2260342,"speciesKey":2260347,"datasetKey":"d7dddbf4-2cf0-4f39-9b2a-bb099caae36c","constituentKey":"2d59e5db-57ad-41ff-97d6-11f5fb264527","parentKey":2260342,"parent":"Acanthastrea","acceptedKey":2260347,"accepted":"Acanthastrea amakusensis Veron, 1990","basionymKey":2260347,"basionym":"Acanthastrea amakusensis Veron, 1990","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"HOMOTYPIC_SYNONYM","nomenclaturalStatus":[],"remarks":"Originally found in sources as accepted taxon within Micromussa Veron, 2000","publishedIn":"Veron, J. (1990). New Scleractinia from Japan and other Indo-West Pacific Countries. <em>Galaxea.</em> 9: 95-173.","numDescendants":0,"lastCrawled":"2017-02-14T13:42:21.926+0000","lastInterpreted":"2017-02-14T13:00:20.059+0000","issues":["ORIGINAL_NAME_DERIVED","CONFLICTING_BASIONYM_COMBINATION"],"synonym":true,"class":"Anthozoa"},{"key":100173150,"nubKey":4880483,"nameKey":7024754,"taxonID":"9ab7c4e2-4e69-40ed-a128-e684ae550128","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.104+0000","issues":[],"synonym":false},{"key":100173154,"nubKey":4880483,"nameKey":7024754,"taxonID":"12559e75-a90a-4c25-aea3-ccdb4fdb83bf","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.121+0000","issues":[],"synonym":false},{"key":100173158,"nubKey":4880483,"nameKey":7024754,"taxonID":"92ea7343-22df-4873-b4e9-40409f74b4af","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.124+0000","issues":[],"synonym":false},{"key":100173162,"nubKey":4880483,"nameKey":7024754,"taxonID":"12ee3ff9-af4c-4e8f-8081-e7d4f6e5299b","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.127+0000","issues":[],"synonym":false},{"key":100173164,"nubKey":4880483,"nameKey":7024754,"taxonID":"84c84f33-746b-44b5-8b4d-4bbda7d2c4df","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.130+0000","issues":[],"synonym":false},{"key":100173168,"nubKey":4880483,"nameKey":7024754,"taxonID":"c4d607ef-2411-42d1-a467-69c1c9f26659","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.132+0000","issues":[],"synonym":false},{"key":100173171,"nubKey":4880483,"nameKey":7024754,"taxonID":"7924f639-de7e-43e4-9432-b0d5e6dca968","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.135+0000","issues":[],"synonym":false},{"key":100173175,"nubKey":4880483,"nameKey":7024754,"taxonID":"32d56d2e-f30c-48f6-a026-872cbe079b09","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.138+0000","issues":[],"synonym":false},{"key":103844245,"nubKey":4880483,"nameKey":7024754,"taxonID":"541204","kingdom":"Metazoa","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":103832354,"phylumKey":103840581,"classKey":103840587,"orderKey":103842303,"familyKey":103844141,"genusKey":103844241,"speciesKey":103844245,"datasetKey":"fab88965-e69d-4491-a04d-e3198b626e52","parentKey":103844241,"parent":"Micromussa","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-04-05T12:35:20.730+0000","lastInterpreted":"2017-04-05T16:19:21.040+0000","issues":[],"synonym":false,"class":"Anthozoa"},{"key":104437493,"nubKey":4880483,"nameKey":7024762,"taxonID":"e275620","kingdom":"Metazoa","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":103832354,"phylumKey":103840581,"classKey":103840587,"orderKey":103842303,"familyKey":103844141,"genusKey":103844241,"speciesKey":103844245,"datasetKey":"fab88965-e69d-4491-a04d-e3198b626e52","parentKey":103844241,"parent":"Micromussa","acceptedKey":103844245,"accepted":"Micromussa amakusensis","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","origin":"SOURCE","taxonomicStatus":"SYNONYM","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-04-05T12:35:20.730+0000","lastInterpreted":"2017-04-05T16:19:21.076+0000","issues":[],"synonym":true,"class":"Anthozoa"},{"key":106044866,"nubKey":4880483,"nameKey":7024762,"taxonID":"578144","kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":105759303,"phylumKey":106038449,"classKey":106038458,"orderKey":106041523,"familyKey":106044737,"genusKey":106044865,"speciesKey":106044866,"datasetKey":"2d59e5db-57ad-41ff-97d6-11f5fb264527","parentKey":106044865,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"publishedIn":"Veron, J. (1990). New Scleractinia from Japan and other Indo-West Pacific Countries. <em>Galaxea.</em> 9: 95-173.","numDescendants":0,"references":"http://www.marinespecies.org/aphia.php?p=taxdetails&id=578144","lastCrawled":"2017-04-05T12:35:47.557+0000","lastInterpreted":"2017-04-05T23:33:39.530+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED","CLASSIFICATION_NOT_APPLIED"],"synonym":false,"class":"Anthozoa"},{"key":107964469,"nubKey":4880483,"nameKey":7024762,"taxonID":"11137807","kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Mussidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":106350111,"phylumKey":107950494,"classKey":107950495,"orderKey":107961048,"familyKey":107964371,"genusKey":107964468,"speciesKey":107964469,"datasetKey":"0938172b-2086-439c-a1dd-c21cb0109ed5","parentKey":107964468,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"accordingTo":"CAAB (Jul 2007)","numDescendants":0,"lastCrawled":"2016-07-28T00:58:37.055+0000","lastInterpreted":"2016-05-02T18:57:57.778+0000","issues":[],"synonym":false,"class":"Anthozoa"},{"key":114325584,"nubKey":4880483,"nameKey":7024762,"taxonID":"cbce9f16-454e-4bd3-9b67-be3c724d3a31","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","phylumKey":114319504,"classKey":114319514,"orderKey":114322547,"familyKey":114325449,"genusKey":114325583,"speciesKey":114325584,"datasetKey":"a5358a8d-9b1a-43a3-b769-50d3e6ef8bd9","parentKey":114325583,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-02-15T20:02:28.156+0000","lastInterpreted":"2016-01-18T23:13:36.578+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED"],"synonym":false,"class":"Anthozoa"},{"key":121496079,"nubKey":4880483,"nameKey":7024760,"taxonID":"160121","kingdom":"Animalia","order":"Scleractinia","family":"Mussidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":121280070,"classKey":121477750,"orderKey":121483436,"familyKey":121495529,"genusKey":121496073,"speciesKey":121496079,"datasetKey":"c33ce2f2-c3cc-43a5-a380-fe4526d63650","parentKey":121496073,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"accordingTo":"J.E.N. Veron. 2000. Corals of the World","numDescendants":0,"lastCrawled":"2016-11-23T11:10:22.160+0000","lastInterpreted":"2016-11-23T11:25:41.551+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED"],"synonym":false,"class":"Anthozoa"}]}';
$array=  json_decode($json,true);

foreach($array["results"] as $result)
{
    $data[]=isset($result["taxonomicStatus"]) ? $result["taxonomicStatus"] : false;
}
print_r($data);

Solution 2:

array_map(function($result) use (&$data){
    $data[]=isset($result["taxonomicStatus"]) ? $result["taxonomicStatus"] : false;
}, $array["results"]);
print_r($data);

Output:

Array
(
    [0] => HOMOTYPIC_SYNONYM
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => ACCEPTED
    [10] => SYNONYM
    [11] => ACCEPTED
    [12] => ACCEPTED
    [13] => 
    [14] => ACCEPTED
)

#2


0  

I would use array map for this particular problem.

我会使用数组映射来解决这个特殊问题。

There are 2 ways of doing it. Explicitly defining the function or, anonymous function. I'll do both, use whichever you feel easy for you.

有两种方法可以做到这一点。明确定义函数或匿名函数。我会做两件事,用你觉得容易的。

function taxonomicStatusMap ($item) {
    return empty($item['taxonomicStatus']) ? null : $item['taxonomicStatus'];
}
$statusArray = array_map("taxonomicStatusMap", $taxon['results']);

DEMO 1

OR the 2nd option,

或第二个选项,

$statusArray = array_map(function ($item) {
    return empty($item['taxonomicStatus']) ? null : $item['taxonomicStatus'];
}, $taxon['results']);

DEMO 2

In both the cases the $statusArray will return exactly what you need.

在这两种情况下,$ statusArray将完全返回您需要的内容。

Personally I like to use the 2nd one if it is only going to be performed once if it is going to be used in multiple places in my code then I use the first approach.

我个人喜欢使用第二个,如果它只会被执行一次,如果它将在我的代码中的多个地方使用,那么我使用第一种方法。

Hope this helps.

希望这可以帮助。

#1


2  

Here you should use foreach loop instead of array_column.

在这里你应该使用foreach循环而不是array_column。

PHP code demo

PHP代码演示

<?php
ini_set('display_errors', 1);
$json='{"offset":0,"limit":20,"endOfRecords":true,"results":[{"key":4880483,"nubKey":4880483,"nameKey":7024762,"taxonID":"gbif:4880483","sourceTaxonKey":106044866,"kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Mussidae","genus":"Acanthastrea","species":"Acanthastrea amakusensis","kingdomKey":1,"phylumKey":43,"classKey":206,"orderKey":714,"familyKey":7002,"genusKey":2260342,"speciesKey":2260347,"datasetKey":"d7dddbf4-2cf0-4f39-9b2a-bb099caae36c","constituentKey":"2d59e5db-57ad-41ff-97d6-11f5fb264527","parentKey":2260342,"parent":"Acanthastrea","acceptedKey":2260347,"accepted":"Acanthastrea amakusensis Veron, 1990","basionymKey":2260347,"basionym":"Acanthastrea amakusensis Veron, 1990","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"HOMOTYPIC_SYNONYM","nomenclaturalStatus":[],"remarks":"Originally found in sources as accepted taxon within Micromussa Veron, 2000","publishedIn":"Veron, J. (1990). New Scleractinia from Japan and other Indo-West Pacific Countries. <em>Galaxea.</em> 9: 95-173.","numDescendants":0,"lastCrawled":"2017-02-14T13:42:21.926+0000","lastInterpreted":"2017-02-14T13:00:20.059+0000","issues":["ORIGINAL_NAME_DERIVED","CONFLICTING_BASIONYM_COMBINATION"],"synonym":true,"class":"Anthozoa"},{"key":100173150,"nubKey":4880483,"nameKey":7024754,"taxonID":"9ab7c4e2-4e69-40ed-a128-e684ae550128","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.104+0000","issues":[],"synonym":false},{"key":100173154,"nubKey":4880483,"nameKey":7024754,"taxonID":"12559e75-a90a-4c25-aea3-ccdb4fdb83bf","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.121+0000","issues":[],"synonym":false},{"key":100173158,"nubKey":4880483,"nameKey":7024754,"taxonID":"92ea7343-22df-4873-b4e9-40409f74b4af","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.124+0000","issues":[],"synonym":false},{"key":100173162,"nubKey":4880483,"nameKey":7024754,"taxonID":"12ee3ff9-af4c-4e8f-8081-e7d4f6e5299b","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.127+0000","issues":[],"synonym":false},{"key":100173164,"nubKey":4880483,"nameKey":7024754,"taxonID":"84c84f33-746b-44b5-8b4d-4bbda7d2c4df","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.130+0000","issues":[],"synonym":false},{"key":100173168,"nubKey":4880483,"nameKey":7024754,"taxonID":"c4d607ef-2411-42d1-a467-69c1c9f26659","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.132+0000","issues":[],"synonym":false},{"key":100173171,"nubKey":4880483,"nameKey":7024754,"taxonID":"7924f639-de7e-43e4-9432-b0d5e6dca968","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.135+0000","issues":[],"synonym":false},{"key":100173175,"nubKey":4880483,"nameKey":7024754,"taxonID":"32d56d2e-f30c-48f6-a026-872cbe079b09","datasetKey":"cd174407-5632-4090-ba61-9ff3fe41bc03","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2016-03-18T03:32:43.271+0000","lastInterpreted":"2016-03-18T03:33:11.138+0000","issues":[],"synonym":false},{"key":103844245,"nubKey":4880483,"nameKey":7024754,"taxonID":"541204","kingdom":"Metazoa","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":103832354,"phylumKey":103840581,"classKey":103840587,"orderKey":103842303,"familyKey":103844141,"genusKey":103844241,"speciesKey":103844245,"datasetKey":"fab88965-e69d-4491-a04d-e3198b626e52","parentKey":103844241,"parent":"Micromussa","scientificName":"Micromussa amakusensis","canonicalName":"Micromussa amakusensis","authorship":"","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-04-05T12:35:20.730+0000","lastInterpreted":"2017-04-05T16:19:21.040+0000","issues":[],"synonym":false,"class":"Anthozoa"},{"key":104437493,"nubKey":4880483,"nameKey":7024762,"taxonID":"e275620","kingdom":"Metazoa","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":103832354,"phylumKey":103840581,"classKey":103840587,"orderKey":103842303,"familyKey":103844141,"genusKey":103844241,"speciesKey":103844245,"datasetKey":"fab88965-e69d-4491-a04d-e3198b626e52","parentKey":103844241,"parent":"Micromussa","acceptedKey":103844245,"accepted":"Micromussa amakusensis","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","origin":"SOURCE","taxonomicStatus":"SYNONYM","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-04-05T12:35:20.730+0000","lastInterpreted":"2017-04-05T16:19:21.076+0000","issues":[],"synonym":true,"class":"Anthozoa"},{"key":106044866,"nubKey":4880483,"nameKey":7024762,"taxonID":"578144","kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":105759303,"phylumKey":106038449,"classKey":106038458,"orderKey":106041523,"familyKey":106044737,"genusKey":106044865,"speciesKey":106044866,"datasetKey":"2d59e5db-57ad-41ff-97d6-11f5fb264527","parentKey":106044865,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"publishedIn":"Veron, J. (1990). New Scleractinia from Japan and other Indo-West Pacific Countries. <em>Galaxea.</em> 9: 95-173.","numDescendants":0,"references":"http://www.marinespecies.org/aphia.php?p=taxdetails&id=578144","lastCrawled":"2017-04-05T12:35:47.557+0000","lastInterpreted":"2017-04-05T23:33:39.530+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED","CLASSIFICATION_NOT_APPLIED"],"synonym":false,"class":"Anthozoa"},{"key":107964469,"nubKey":4880483,"nameKey":7024762,"taxonID":"11137807","kingdom":"Animalia","phylum":"Cnidaria","order":"Scleractinia","family":"Mussidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":106350111,"phylumKey":107950494,"classKey":107950495,"orderKey":107961048,"familyKey":107964371,"genusKey":107964468,"speciesKey":107964469,"datasetKey":"0938172b-2086-439c-a1dd-c21cb0109ed5","parentKey":107964468,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"accordingTo":"CAAB (Jul 2007)","numDescendants":0,"lastCrawled":"2016-07-28T00:58:37.055+0000","lastInterpreted":"2016-05-02T18:57:57.778+0000","issues":[],"synonym":false,"class":"Anthozoa"},{"key":114325584,"nubKey":4880483,"nameKey":7024762,"taxonID":"cbce9f16-454e-4bd3-9b67-be3c724d3a31","phylum":"Cnidaria","order":"Scleractinia","family":"Lobophylliidae","genus":"Micromussa","species":"Micromussa amakusensis","phylumKey":114319504,"classKey":114319514,"orderKey":114322547,"familyKey":114325449,"genusKey":114325583,"speciesKey":114325584,"datasetKey":"a5358a8d-9b1a-43a3-b769-50d3e6ef8bd9","parentKey":114325583,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron, 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","nomenclaturalStatus":[],"numDescendants":0,"lastCrawled":"2017-02-15T20:02:28.156+0000","lastInterpreted":"2016-01-18T23:13:36.578+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED"],"synonym":false,"class":"Anthozoa"},{"key":121496079,"nubKey":4880483,"nameKey":7024760,"taxonID":"160121","kingdom":"Animalia","order":"Scleractinia","family":"Mussidae","genus":"Micromussa","species":"Micromussa amakusensis","kingdomKey":121280070,"classKey":121477750,"orderKey":121483436,"familyKey":121495529,"genusKey":121496073,"speciesKey":121496079,"datasetKey":"c33ce2f2-c3cc-43a5-a380-fe4526d63650","parentKey":121496073,"parent":"Micromussa","scientificName":"Micromussa amakusensis (Veron 1990)","canonicalName":"Micromussa amakusensis","authorship":" (Veron, 1990)","nameType":"SCIENTIFIC","rank":"SPECIES","origin":"SOURCE","taxonomicStatus":"ACCEPTED","nomenclaturalStatus":[],"accordingTo":"J.E.N. Veron. 2000. Corals of the World","numDescendants":0,"lastCrawled":"2016-11-23T11:10:22.160+0000","lastInterpreted":"2016-11-23T11:25:41.551+0000","issues":["SCIENTIFIC_NAME_ASSEMBLED"],"synonym":false,"class":"Anthozoa"}]}';
$array=  json_decode($json,true);

foreach($array["results"] as $result)
{
    $data[]=isset($result["taxonomicStatus"]) ? $result["taxonomicStatus"] : false;
}
print_r($data);

Solution 2:

array_map(function($result) use (&$data){
    $data[]=isset($result["taxonomicStatus"]) ? $result["taxonomicStatus"] : false;
}, $array["results"]);
print_r($data);

Output:

Array
(
    [0] => HOMOTYPIC_SYNONYM
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => ACCEPTED
    [10] => SYNONYM
    [11] => ACCEPTED
    [12] => ACCEPTED
    [13] => 
    [14] => ACCEPTED
)

#2


0  

I would use array map for this particular problem.

我会使用数组映射来解决这个特殊问题。

There are 2 ways of doing it. Explicitly defining the function or, anonymous function. I'll do both, use whichever you feel easy for you.

有两种方法可以做到这一点。明确定义函数或匿名函数。我会做两件事,用你觉得容易的。

function taxonomicStatusMap ($item) {
    return empty($item['taxonomicStatus']) ? null : $item['taxonomicStatus'];
}
$statusArray = array_map("taxonomicStatusMap", $taxon['results']);

DEMO 1

OR the 2nd option,

或第二个选项,

$statusArray = array_map(function ($item) {
    return empty($item['taxonomicStatus']) ? null : $item['taxonomicStatus'];
}, $taxon['results']);

DEMO 2

In both the cases the $statusArray will return exactly what you need.

在这两种情况下,$ statusArray将完全返回您需要的内容。

Personally I like to use the 2nd one if it is only going to be performed once if it is going to be used in multiple places in my code then I use the first approach.

我个人喜欢使用第二个,如果它只会被执行一次,如果它将在我的代码中的多个地方使用,那么我使用第一种方法。

Hope this helps.

希望这可以帮助。