0

I have a matrix that contains a block with a category field. I want to loop over the matrix models, in PHP, to get data. Here's what I have so far:

foreach ($user->networksMatrix as $key => $networkLink)
{
    $netArr[$networkLink->id]["fields"]["networktype"] = $networkLink->networktype;
    $netArr[$networkLink->id]["fields"]["networkUrl"] = $networkLink->networkUrl;
}

networktype is a category field. How do I get the get the ID of the category a user selected?

Thanks!

UPDATE

I discovered that the following works:

foreach ($user->networksMatrix as $key => $networkLink)
{
    $netArr[$networkLink->id]["fields"]["networktype"] = $networkLink->networktype->ids()[0];
    $netArr[$networkLink->id]["fields"]["networkUrl"] = $networkLink->networkUrl;
}

Since networktype is a categories field and that field is limited to 1 category I'll only ever get a single result from the method:

ids();

So... I just do this and I'm good:

$networkLink->networktype->ids()[0];
3
  • 3
    That's great, Matt! Would you mind adding that as an official answer rather than an update to the original question?
    – Brad Bell
    Commented Apr 18, 2015 at 23:19
  • Not sure what you mean?
    – Matt V
    Commented Apr 21, 2015 at 17:04
  • 2
    The part where you updated your question with a solution. Can you add that under the "Your Answer" portion on the bottom of the page? That adds it as an official answer to the question.
    – Brad Bell
    Commented Apr 21, 2015 at 17:06

1 Answer 1

4

For the sake of completeness, the question was resolved by the asker and I will quote his answer here, so this fine question is recognized by Stack Exchange as answered!

Matt V says:

I discovered that the following works:

foreach ($user->networksMatrix as $key => $networkLink)
{
    $netArr[$networkLink->id]["fields"]["networktype"] = $networkLink->networktype->ids()[0];
    $netArr[$networkLink->id]["fields"]["networkUrl"] = $networkLink->networkUrl;
}

Since networktype is a categories field and that field is limited to 1 category I'll only ever get a single result from the method:

ids();

So... I just do this and I'm good:

$networkLink->networktype->ids()[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.