1

我有一个小问题,我似乎无法轻易解决。修复可能很容易,但我太菜鸟了,无法自己修复。也许你们可以对此有所了解。

问题

我需要在 PHP 中创建一个多维数组。这工作得很好。但是,最后一个嵌套数组有时包含 2 个元素,有时包含 3 个元素。因此,本文标题中的“不对称”。

该问题很可能是由基于我的 PHP 代码中的开关变量创建嵌套数组引起的。当未定义变量时($button例如case 'inactive'),键无论如何都会放在$elements数组中,但没有值。

PHP 代码

$player = Array();
$data = @$_POST['players'];

if (!empty($data) && is_array($data)) {
    foreach($data as $id){

        $player_info = mysqli_query($connection, "SELECT * FROM players WHERE id = '$id'");
        $player_info = mysqli_fetch_assoc($player_info);

        switch ($player_info['status']) {
            case 'active':
                $color = '#30B490';
                $status = 'active';
                $button = 'buttons';
                break;
            case 'inactive':
                $color = '#A1A1A1';
                $status = 'inactive';
                break;
            case 'standby':
                $color = '#F0AD4E';
                $status = 'standby';
                $button = 'buttons';
                break;
            default:
                $color = '#4FA1D9';
                $status = 'unknown';
                break;
        }

        // THIS PART SEEMS TO CAUSE MY ISSUE
        $elements = array(
            "indicator" => $color,
            "status" => $status,
            "button" => $button
        );
        // THIS PART SEEMS TO CAUSE MY ISSUE

        foreach ($elements as $label => $element) {
            $player[$id][$label] = $element;
        }
    }
}

print_r($player);

print_r() 结果

目前

Array (
    [1] => Array (
        [indicator] => #A1A1A1
        [status] => inactive
        [button] =>
    )
    [2] => Array (
        [indicator] => #30B490
        [status] => active
        [button] => yes
    )
    [3] => Array (
        [indicator] => #4FA1D9
        [status] => standby
        [button] => yes
    )
)

它应该是什么

Array (
    [1] => Array (
        [indicator] => #A1A1A1
        [status] => inactive
    )
    [2] => Array (
        [indicator] => #30B490
        [status] => active
        [button] => yes
    )
    [3] => Array (
        [indicator] => #4FA1D9
        [status] => standby
        [button] => yes
    )
)

您可以看到 2 个结果之间的差异。看[button] =>。当我 json_encode() 整个多维数组时,结果显示一个null看起来不正确的值。

{"1":{"indicator":"#A1A1A1","status":"inactive","button":null},"2":{"indicator":"#30B490","status":"active","button":"buttons"},"3":{"indicator":"#4FA1D9","status":"unknown","button":"buttons"}}

我想要什么

我希望多维数组是干净的(没有空值),如“应该是什么”print_r()结果中所示。

知道我应该在我的 PHP 代码中编辑什么以创建正确的多维数组吗?

最好的问候, Peter de Leeuw

4

3 回答 3

0

是的,这导致了问题

    // THIS PART SEEMS TO CAUSE MY ISSUE
    $elements = array(
        "indicator" => $color,
        "status" => $status,
        "button" => $button
    );

发生的情况是您使用空的 var $button 定义键按钮,并且下一次迭代 $button 未重置,您的脚本可能会导致一些意外行为

为什么不这样分配 $element 呢?

    switch ($player_info['status']) {
        case 'active':
            $elements = array(
            'color' => '#30B490',
            'status' => 'active',
            'button' => 'buttons');
            break;
        case 'inactive':
            $elements = array(
            'color' = '#A1A1A1',
            'status' = 'inactive');
            break;
        case 'standby':
            $elements = array(
            'color' => '#F0AD4E',
            'status'  => 'standby',
            'button' => 'buttons');
            break;
        default:
            $elements = array(
              'color' => '#4FA1D9';
              'status' => 'unknown');
            break;
    }

甚至像这样:

    $player[$id]['status'] = $player_info['status'];
    switch ($player_info['status']) {
        case 'active':
            $player[$id]['color'] = '#30B490';
            $player[$id]['button'] = 'buttons';
            break;
        case 'inactive':
            $player[$id]['color'] = '#A1A1A1';
            break;
        case 'standby':
            $player[$id]['color'] = '#F0AD4E';
            $player[$id]['button'] = 'buttons';
            break;
        default:
            $player[$id]['color'] = '#F0AD4E';
            $player[$id]['status'] = 'unknown'; //overwrite status from database to unknown
            break;
    }

或者更小

//Outside the loop
$colors = array('active' =>'#30B490', 'inactive' => '#A1A1A1', 'standby' => '#F0AD4E');

//Inside the loop
$player[$id]['status'] = $player_info['status'];
$player[$id]['color'] = isset($colors[$player_info['status']) ? $colors[$player_info['status'] : '#4Fa1D9'; 
if($player[$id]['status'] == 'active' || $player[$id]['status'] == 'standby') 
    $player[$id]['buttons'] = 'buttons';
于 2014-09-17T09:55:31.533 回答
0

你为什么不给 $button 分配一个“否”值呢?

switch ($player_info['status']) {
    case 'active':
        $element = array(
        'color' => '#30B490',
        'status' => 'active',
        'button' => 'buttons');
        break;
    case 'inactive':
        $element = array(
        'color' = '#A1A1A1',
        'status' = 'inactive');
        'button' => 'no');
        break;
    case 'standby':
        $element = array(
        'color' => '#F0AD4E',
        'status'  => 'standby',
        'button' => 'buttons');
        break;
    default:
        $element = array(
          'color' => '#4FA1D9';
          'status' => 'unknown');
        break;
}

这应该可以帮助您稍后使用 json.parse 处理您的答案

于 2014-09-17T10:04:32.373 回答
-1

你可以简单地使用array_filter()

$player = array_map('array_filter', $player);

这将清除中每个数组的空值$player

于 2014-09-17T10:00:54.750 回答