I am trying to get the value from a parsed json. But it just returns empty. I tried with and without quotes.
It’s not working because your syntax tries to pass ‘x-wp-total’ as if it were an array index.
Instead, use get(map())
to filter to the desired array item and extract its value.
Here’s an example from a very similar GET request. Let’s say we want the value for “cache-control”, which is “private, max-age=0”:
It’s the third item in the array, so we could extract it like this:
But we don’t know whether it’s guaranteed to be the third item. Here’s a more robust way:
- Filter to the array item we want. It’s effectively saying “Give me the
value
whosename
is'cache-control'
”. (Of course that would be'x-wp-total'
in your case).
So far, this produces a one-item array:
- Not wrong, but we actually want the string/numeric value from inside that item, so wrap the function above in
get()
with a path of1
:
Voilà!
4 Likes
Thank you so much for explaining this.
I had tried with the map function, but couldn’t get it to work. Your explanation made it crystal clear for me.
1 Like