Help with array_filter() Function

Hey,
I’m almost completely new to the world of PHP… all my development so far is done in ASP / VBScript (please don’t hate me).
I’ve just integrated MessageBird into my web application using their PHP / REST API Library.
Everything is working well (sending and receiving messages).

The page where I display received messages however basically just displays a list of all messages (sent or received), and I just want the ones that were sent to us.
The results are sent back from the API as an array, which I have setup in my script to display the results in a nice table layout.
What I would like to do is filter the array so that only results where the recipient is set to the incoming number for our account are displayed (i.e. messages that have been sent to the system).

Any help with how to set this up with an array_filter() function would be great. Here’s my code:

::This at the top of the page::

<?php

require_once('MessageBird/autoload.php');

$MessageBird = new \MessageBird\Client('[my-api-key-here]'); // Set your own API access key here.

try {
    $MessageList = $MessageBird->messages->getList(array('limit' => 100));

    
    

} catch (\MessageBird\Exceptions\AuthenticateException $e) {
    // That means that your accessKey is unknown
    echo 'wrong login';

} catch (\Exception $e) {
    var_dump($e->getMessage());
}
?>

::This in the body::

<?php 

    foreach ($MessageList->items as $message) {
        
        ?>
    
[HTML FOR THE TABLE ROW GOES HERE]

 <?php
// etc
    }
  ?>

The value of the ‘Recipient’ is available as

$message->recipients->items[0]->recipient

This is what I use (with an echo command) to display the value of the recipient in the table output.

Any help with this would be great… thanks.

You don’t really need to filter array for this, you can skip rows right in foreach loop:

foreach ($MessageList->items as $message) {
    // if recipient field is not equal to what you want
    if ($message->recipients->items[0]->recipient != 'WHAT_YOU_WANT'){
        //skip this iteration and go to next
        continue;
    }

//...the rest of your code...

That was absolutely perfect and has done exactly what I wanted it to do.
Thank you so much!

Well first of all…what a poorly documented system this is, if they dont even specify parameters that are available to their API calls. Or define their endpoints. Or… well, much of anything, really. They declare their objects, and that’s about all. Fun.

From what i can tell from their examples, their endpoint doesnt even accept anything beyond “offset” and “limit”. So… you cant even guarantee to find yourself a “received” message if you set limit to 100, because incoming and outgoing messages are treated as the same object.

That said, megazoid is mostly correct; you can do it that way, as long as you are always the first recipient. (I’m uncertain if incoming messages display all recipients it was sent to - again, the documentation is poor enough that I cant tell.

doing it generically? The array_filter form is here; though the logic can just as easily be implemented in-loop as megazoid said.

  function filterrecip($object) {
       foreach($object->recipients->items AS $recipient) {
           if($recipient->recipient == "YourIDHere") { return true; }
      }
     return false;
  }

  $messages = array_filter($MessageList->items,'filterrecip');

EDIT: I actually think you should be able to tell the difference by $MessageList->items->direction rather than the recipient list…

Thanks very much. I implemented the suggestion from megazoid and it worked first time without any problems… but always wanting to increase my knowledge of something, I will certainly try what you have suggested.
I agree, the documentation is a little patchy! Although, the technical support from the MessageBird team is excellent (but just not available at the weekends!)

Next challenge - matching the numbers of the ‘sender’ to the users table in my database so that I can display names instead of numbers… but I think that’s for another day!

I did actually contact MessageBird to ask if there was a way of setting something up in their interface that would trigger a script on my server everytime a message is received. I have a very similar setup working with GoCARDLESS. The club subscriptions and event ticket fees and things for the scout campsite (for which I have developed this system) uses GoCARDLESS to process online payments. They use something that they have called WebHooks - every time the status of a payment being processed changes, the webhook is triggered, and because it passes IDs and so on back to my application, I can update the status of the financial records table accordingly.
I think something like this would be much more preferable - when a message is received, trigger a simple script that does an SQL INSERT INTO type command and writes the content of the message (and any other details you choose to save) into an SQL table… processing the details afterwards would be so much more straight forward as it could all be done in queries and views and things.

Anyway - I digress! thanks very much for your reply.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.