0

I hope you all having a great day. I'm developing a simple wordpress plugin and I've never worked with JSON encoded data on PHP before so I need some help here.

I have this simple function with a single parameter that's called in a 'foreach' loop and inside this function I have an array of values pulled from DB and it's JSON encoded, it goes like this:

array (size=4)
  0 => string '[{"videoUrl":"https://vimeo.com/493156200","playlistID":"7992"}]' (length=64)
  1 => string '[{"videoUrl":"https://vimeo.com/365531165","playlistID":"7992"}]' (length=64)
  2 => string '[{"videoUrl":"https://vimeo.com/365531139","playlistID":"7992"}]' (length=64)
  3 => string '[{"videoUrl":"https://vimeo.com/521605944","playlistID":"7992"}]' (length=64) 

My function goes like this:

$playlist_id = 7992;    
function videostatuschecker($x){
    $unlockedvideos = ['json encoded data illustrated above'];
    // x value is like this:  $x = 'https://vimeo.com/493156200';
           
    if (in_array($x, $unlockedvideos)){
       return 'unlocked';  
    }
    else{
      return 'locked';
   }
}

Video URL will be passed through my function's parameter variable and playlist ID is defined in the parent function so it's static.

Now what I need is, I need to compare that $x value to "videoUrl" values in the array and compare its 'playlistID' to $playlist_id variable and if they both match, I need the function to return 'unlocked' and vice versa for the else statement. Any help is much appreciated, thank you so much.

  • [json_decode](https://www.php.net/manual/en/function.json-decode.php) is what you are looking for – ciekals11 Apr 21 '21 at 07:37
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – El_Vanja Apr 21 '21 at 07:37
  • Not really, thanks anyways. –  Apr 21 '21 at 07:42
  • @Laclogan check link. that I have posted previously. There are simple examples that will help you understand. – ciekals11 Apr 21 '21 at 07:44
  • The proposed duplicate shows just about every conceivable way of reading and handling JSON. You can't convince me you've tried everything from that thread in mere 5 minutes and concluded it doesn't help in your case. Take your time, invest some effort into it. Once you decode JSON, you've got your data in array/object form and it should be easy to continue from there. If you run into additional problems, then please edit your question to show your progress and describe the newly encountered issue. – El_Vanja Apr 21 '21 at 07:48

1 Answers1

0

You could do something like this:

function videostatuschecker($video_url, $playlist_id){
    $unlockedvideos = [
        '[{"videoUrl":"https://vimeo.com/493156200","playlistID":"7992"}]',
        '[{"videoUrl":"https://vimeo.com/365531165","playlistID":"7992"}]',
        '[{"videoUrl":"https://vimeo.com/365531139","playlistID":"7992"}]',
        '[{"videoUrl":"https://vimeo.com/521605944","playlistID":"7992"}]'
    ];
    
    foreach ($unlockedvideos as $unlockedvideo) {
      $decoded = json_decode($unlockedvideo);
      if ($playlist_id == $decoded[0]->playlistID && $video_url == $decoded[0]->videoUrl) {
        return 'unlocked'; 
      }
    }
    
    return 'locked';
}
 
print(videostatuschecker('https://vimeo.com/493156200', 7992));

Please note that your $unlockedvideos is an array of arrays. Is that what you want?

user1915746
  • 466
  • 4
  • 16
  • Hells yea man worked like a charm thank you! Do you know how can I add the playlistID comparing to that if statement? It's the key here, I've had to change the data to JSON just for that. And yes, each element of that parent array is actually a single line of row in the database so I do have to get them all in one variable. So it's fine unless you see a problem with the way I've saved the json data –  Apr 21 '21 at 08:02
  • 1
    See updated post. I would store the json object directly and not the object within an array. e.g. instead of `[{"videoUrl":"https://vimeo.com/493156200","playlistID":"7992"}]` just `{"videoUrl":"https://vimeo.com/493156200","playlistID":"7992"}` – user1915746 Apr 21 '21 at 09:15
  • Okay great man, I just changed the code and removed that array stuff, now it's like this: {"videoUrl":"https://vimeo.com/519611694","playlistID":"7992"} -- Should I go ahead and remove the [0] from decoded variable? –  Apr 21 '21 at 09:28
  • Yes. The [0] accesses the first element of the array. If you do no longer have an array, you don't need the [0] – user1915746 Apr 21 '21 at 09:35
  • Thank you so much man, everything worked perfectly without any issues. I would give you 99999 rep points if I could, cheers. –  Apr 21 '21 at 13:50