I've scoured trying to find an example to pull a selected field from an array. I can pull fields based on key name just fine, however having an issue determining how to utilize CFP to pull a value from an array, using PHP.
Any assistance would be greatly appreciated. I've greatly appreciated the library having been made available thus far, thanks guys!
Hi,
I think I don't understand what you mean by "pull a value from an array". Maybe I can help you if you can clarify your need to me.
If you can do it based on key name, have you tried to use array_flip, in order to use values as keys in your array ?
My apologies, I didn't quite explain properly.
I had an array of Entries, and then within each entry a set of definitions, along with another array. It wasn't pretty, and there are likely better ways to go about it, but this was my method of resolution:
$root = $plist->getValue(true);
$entries = $root->get('Entries');
foreach ($entries as $entry) {
$topalias = $entry->get('versions')->getValue();
foreach ($topalias as $subvalue) {
$finalvalue = $subvalue->get('versionAliases')->getValue();
}
$versionalias = $finalvalue;
}
The $versionalias having been the entry I was pulling from that second tier array. Again, not pretty, but it worked.
Thanks for the quick response, and if there's a more efficient method, I'd love to hear it. Thanks!
As the API stands today, there is not a more efficient approach than the one you chose. In terms of working with $plist
that is. One could probably optimize the looping (unless you really want the very last $finalvalue
of the very last $entry
).
That said, it would be possible to implement an xPath / CSS-selector like approach to navigate within a $plist
. Something that would convert your above looping thing into $versionalias = $plist->find('versions:last/versionAliases:last')->getValue();
I'd be happy to merge a PR resolving this ;)