Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Assistance with example #24

Open
kdm122 opened this issue Sep 17, 2013 · 3 comments
Open

Assistance with example #24

kdm122 opened this issue Sep 17, 2013 · 3 comments

Comments

@kdm122
Copy link

kdm122 commented Sep 17, 2013

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!

@lpotherat
Copy link
Contributor

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 ?

@kdm122
Copy link
Author

kdm122 commented Sep 18, 2013

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!

@rodneyrehm
Copy link
Contributor

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 ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants