0

Необходимо получить массив вида:

Array ( 
    [028ebaa9-e8dd-4eb3-aec0-2be5632da1fe] => Array ( 
            [deliveryTime] => 2018-05-31 14:47 
            [token1] => token1 
            [tags] => tags1 
            [sendStatus] => sheduled 
            ) 
    [a6352709-47d2-422e-ad84-86ea1249d805] => Array ( 
            [deliveryTime] => 2018-05-31 14:47 
            [token1] => token2 
            [tags] => tags2 
            [sendStatus] => 0 
            )  
    [3716d438-7262-4bda-994d-fe055f4c5b56] => Array ( 
            [deliveryTime] => 2018-05-31 14:47 
            [token1] => token2 
            [tags] => tags2 
            [sendStatus] => 0 
            )
    )

Пробовал делать так:

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        if ($send_date){
            /*echo "id: " . $row["id"] . " playerID: " . $row["playerID"] . " deliveryTime: " . $row["deliveryTime"] . " token1: " . $row["token1"] . " tags: " . $row["tags"] . " sendStatus: " . $row["sendStatus"] ."<br>";*/
            $playerID = array("playerID" => $row["playerID"]);
            $deliveryTime = array("deliveryTime" => $row["deliveryTime"]);
            $token1 = array("token1" => $row["token1"]);
            $tags = array("tags" => $row["tags"]);
            $sendStatus = array("sendStatus" => $row["sendStatus"]);
            $temp_array0 = array_merge ($deliveryTime, $token1, $tags, $sendStatus);            
            $temp_array = array($row["playerID"] => $temp_array0);
        }
    }

} else {
    echo "0 results";
}
print_r($temp_array);

Но, в таком случае получается:

Array ( [3716d438-7262-4bda-994d-fe055f4c5b56] => Array ( [deliveryTime] => 2018-05-31 14:47 [token1] => token2 [tags] => tags2 [sendStatus] => 0 ) )
3
  • если выводить print_r($temp_array); в самом цикле, то получается три отдельных массива.
    – Artem
    31 мая 2018 в 14:49
  • Ну так перед while укажите $arr = [];, а потом в цикле указывайте $arr[$playerID] = $temp_array0; - это как вариант..
    – entithat
    31 мая 2018 в 14:51
  • 1
    @entithat Ваш вариант рабочий. Спасибо.
    – Artem
    31 мая 2018 в 14:59

1 ответ 1

0
$temp_array = FALSE;

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $temp_array[$row['playerID']] = [
            'deliveryTime' => $row['deliveryTime'],
            'token1' => $row['token1'],
            'tags' => $row['tags'],
            'sendStatus' => $row['sendStatus']
        ];
    }
}

echo '<pre>';
print_r($temp_array);
echo '</pre>';

Если нужна групировка по playerID

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $temp_array[$row['playerID']][] = [
            'deliveryTime' => $row['deliveryTime'],
            'token1' => $row['token1'],
            'tags' => $row['tags'],
            'sendStatus' => $row['sendStatus']
        ];
    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.