php convert array of objects to array of key value from properties
- php make array with keys values from object properties
- array_map set keys as well as values
$terms = $this->getAllTerms();
Gives me an array of objects that looks like this:
=> [
{#8116
+"tid": "215",
+"revision_id": "427",
+"vid": "findit_ages",
+"langcode": "en",
+"status": "1",
+"name": "Pre-natal",
+"description__value": null,
+"description__format": null,
+"weight": "0",
+"changed": "1580746712",
+"default_langcode": "1",
+"revision_translation_affected": "1",
+"depth": 0,
+"parents": [
"0",
],
},
{#8312
+"tid": "216",
+"revision_id": "428",
+"vid": "findit_ages",
+"langcode": "en",
+"status": "1",
+"name": "Infant",
+"description__value": null,
+"description__format": null,
+"weight": "1",
+"changed": "1580746712",
+"default_langcode": "1",
+"revision_translation_affected": "1",
+"depth": 0,
+"parents": [
"0",
],
},
I need to turn it into an array where the keys are the tid properties and the values are the name properties.
I wanted to do it in one line and i’m sure i can with a lambda function and array_walk but whatever if PHP does not have more help i’ll just do it in three-to-four lines:
$options = [];
foreach ($terms as $term) {
$options[$term->tid] = $term->name;
}