0

I have a slice of PHP code that's throwing an error;

Notice: Array to string conversion.

It is because for some reason $deviceToken is an array when it should be a string.

I don't why PHP thinks it is an array. When I print results array of my mysqli query, it shows an array in an array, and because of that, it thinks $deviceToken is an array. I only want one array, $rows and $deviceToken to be the value variables of the array. Because of this, later in the code where $deviceToken is read in the binary, its throwing another error because apparently its an array, not a string. See below for code:

// Create an array of units assigned to call
$unitsarray = explode(",", $units);

for ($i = 0; $i < count($unitsarray); $i++){

    echo "Units row $i = $unitsarray[$i] <br />";

    $result = $mysqli->query("SELECT devicetoken FROM `department devices` WHERE unit LIKE $unitsarray[$i]");

    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }

    echo "ResultsArray = ";
    print_r($rows);

    // Loop APNS for each device token in $devicetoken array
    foreach ($rows as $deviceToken)
    {    
    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack("H*", $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result2 = fwrite($fp, $msg, strlen($msg));

    // Create APNS operation output
    if (!$result2)
        echo 'Failed message'.PHP_EOL;
    else
        echo "<b>Successful message sent:</b>&nbsp;&nbsp; $call - $location - $station - $units to device(s):&nbsp;&nbsp;'$deviceToken </br>".PHP_EOL;
    }
}

Results array output looks like this:

ResultsArray = Array (
    [0] => Array (
        [devicetoken] => 773f5436825a7115417d3d1e036da20e806efeef547b7c3fe4da724d97c01b30
    )
)
5
  • 2
    var_dump($deviceToken); - stop guessing, work with facts Commented Dec 16, 2012 at 23:10
  • in the place you want to see the real variable contents Commented Dec 16, 2012 at 23:11
  • 1
    Psst: Mysqli has a fetch_all method, no need for while any longer: php.net/manual/en/mysqli-result.fetch-all.php - e.g. $rows = $result->fetch_all(MYSQLI_ASSOC); Commented Dec 16, 2012 at 23:12
  • Alright thanks, I have also one quick question, how do I prevent while state from throwing an error if the sql query returns no row? Right now, it goes down and returns a result, but when it reaches one where the LIKE returns nothing, it throws an error. Commented Dec 16, 2012 at 23:22
  • @JonErickson I use: $result = $db->query($query);if ($result == false){ // no rows} else {// row! } Commented Dec 16, 2012 at 23:27

5 Answers 5

2

$deviceToken is an array, you want the key devicetoken from that array, not the array itself.

foreach ($rows as $key => $row)
{    
    $deviceToken = $row['devicetoken'];  
    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack("H*", $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result2 = fwrite($fp, $msg, strlen($msg));

    // Create APNS operation output
    if (!$result2)
        echo 'Failed message'.PHP_EOL;
    else
        echo "<b>Successful message sent:</b>&nbsp;&nbsp; $call - $location - $station - $units to device(s):&nbsp;&nbsp;'$deviceToken </br>".PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

Comments

2
while($row = $result->fetch_assoc()) {
    $rows[] = $row;
}

$row is a 1-dimensional array
$rows is an array of arrays because each array $row is assigned as a new item in the $rows array when you print_r the array $rows it consists of other arrays

$rows as $deviceToken each value in array $rows(ie. each array $row) is taken out and assigned to $deviceToken

Comments

1

$deviceToken is indeed an array, probably the first row and the only row in your resultArray

You need

$deviceToken['deviceToken']

Comments

1

It is an array because your using fetch_assoc, the results return an array (keys are the database column names, the values are the values of the fields). It returns the keys and values from the database and stores it in an array object. fetch_rows returns just the rows. Look into using a different query function method. Also I agree with the other two. You'll have to separate the rows as $key => $value.

Comments

0

You're creating an array of arrays when you loop over the resultset returned by ->fetch_assoc(). This is apparant in the print_r(). It is because ->fetch_assoc() returns an array of the data, even if there's one column.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.