如何将字符串数组转换为整数数组?

发布于 2025-01-02 01:57:56 字数 989 浏览 0 评论 0原文

我当前的 PHP 代码遇到此错误:

注意:TO id 不是整数:1, 2。 1) APNS::queueMessage ->

如何将字符串数组转换为整数数组,就像另一个问题一样: ID 不是整数。 .. EasyAPNS

我基本上试图将 id(从我的数据库)传递给 newMessage(),就像这个苹果示例:

// SEND MESSAGE TO MORE THAN ONE USER
// $apns->newMessage(array(1,3,4,5,8,15,16)); 
// ($destination contain a string with the Ids like "1,2,3")

下面是我的代码:

if (isset($destination))
{
    //$destination = 1,2,.. 
    $dest = explode(",", $destination);
    if (isset($time))
    {
        $apns->newMessage($dest, $time);
    }
    else
    {
        $apns->newMessage($dest);
    }

    $apns->addMessageAlert($message);
    $apns->addMessageBadge($badge);
    $apns->addMessageSound('bingbong.aiff');
    $apns->queueMessage();
    header("location:$url/index.php?success=1");
}

I'm getting this error with my current PHP code:

Notice: TO id was not an integer: 1, 2. 1) APNS::queueMessage ->

How can I convert to an array of strings to an array of integers like in this other question: ID not integer... EasyAPNS ?

I'm basically trying to pass ids (from my database,) to newMessage() like this Apple example:

// SEND MESSAGE TO MORE THAN ONE USER
// $apns->newMessage(array(1,3,4,5,8,15,16)); 
// ($destination contain a string with the Ids like "1,2,3")

Here is my code below:

if (isset($destination))
{
    //$destination = 1,2,.. 
    $dest = explode(",", $destination);
    if (isset($time))
    {
        $apns->newMessage($dest, $time);
    }
    else
    {
        $apns->newMessage($dest);
    }

    $apns->addMessageAlert($message);
    $apns->addMessageBadge($badge);
    $apns->addMessageSound('bingbong.aiff');
    $apns->queueMessage();
    header("location:$url/index.php?success=1");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮萍、无处依 2025-01-09 01:57:56

我将创建一个接受数组的包装函数,然后调用它。

function newMessageArray($array) {
    foreach ($array as $element) {
        $apns->newMessage($element);
    }
}

这样,您可以使用整数数组调用 newMessageArray(),例如 array(1,2,3,4,5),它们都会被发送。

另外,您应该将变量名称(从 $array$element)更改为更有意义的名称。我不知道你想做什么,所以我不确定要使用什么名称。

I would create a wrapper function that accepts an array, and then call it.

function newMessageArray($array) {
    foreach ($array as $element) {
        $apns->newMessage($element);
    }
}

This way, you can call newMessageArray() with an array of integers, such as array(1,2,3,4,5), and they will all be sent.

Also, you should change the variable names (from $array and $element) to something more meaningful. I don't know what you're trying to do, so I wasn't sure what names to use.

童话 2025-01-09 01:57:56

你的问题不清楚,只是猜测。
转换整数似乎有错误,请尝试

$dest = explode(",", $destination);
$destArray = array();
foreach($dest as $key => $val) {
  $destArray[$key] = intval($val);
}

if (isset($time))
{
    $apns->newMessage($destArray, $time);
}
else
{
    $apns->newMessage($destArray);
}

使用“intval”转换字符串不是整数的地方。

Your Question is not clear, It's only a guess work.
It seems to be error in convert integer, try

$dest = explode(",", $destination);
$destArray = array();
foreach($dest as $key => $val) {
  $destArray[$key] = intval($val);
}

if (isset($time))
{
    $apns->newMessage($destArray, $time);
}
else
{
    $apns->newMessage($destArray);
}

Convert where the string is not integer using 'intval'.

琴流音 2025-01-09 01:57:56

我相信您可能正在寻找的是如何执行此操作:

您的数据库表中有 id,对吗?并且您正在尝试将多个 id 放入一个数组中,以便该数组可以在 $apns->newMessage() 调用中使用,对吗? (我检查了来源。 .) 但是,id 不知何故以 string 的形式出现,而不是 int

因此,您可能只想确保新数组由 int 组成,如下所示:

function to_int($x) {
    return (int)$x;
}

$dest = array_map("to_int", $dest);

可能还有其他方法可以做到这一点,但这样,您至少知道您有该数组中的 int 变量。希望有帮助!

I believe what you may be looking for is how to do this:

You have ids in your database table, right? And you are trying to get multiple ids into an array so that the array can be used in $apns->newMessage() call, right? (I checked the source for this...) But, the ids are somehow coming over as strings instead of ints.

So, you probably want to just make sure that the new array is made up of ints, like this:

function to_int($x) {
    return (int)$x;
}

$dest = array_map("to_int", $dest);

There are probably other ways to do this, but this way, you at least know that you have int variables in that array. Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文