创建一个多维数组和json_encode

时间:2018-04-05 12:49:10

标签: php arrays multidimensional-array mysqli

我必须从数据库中获取JSON响应:

id和last_active:

$users = array();

$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
    $users[]['id'] = $row['id'];
    $users[]['last_active'] = $row['last_active'];
}

echo json_encode($users);

数组必须如下:

$users = array (
  0 => array(1, 1522921015),
  1 => array(2, 1522921019),
  2 => array(3, 1522921102),
  3 => array(4, 1522921195),
  4 => array(5, 1522921034)
);

如何使用查询结果正确构建多维数组?

5 个答案:

答案 0 :(得分:1)

试试此代码

  $users = array();

  $stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
  $i=0;
  while ($row = $stmt->fetch_assoc()) {
    $users[$i]['id'] = $row['id'];
    $users[$i]['last_active'] = $row['last_active'];
    $i++;
  }

    echo json_encode($users);

答案 1 :(得分:1)

你可以像这样使用临时数组:

TabbedPage.CurrentPage

或直接指定两个值:

public class MasterPage : TabbedPage
{
    public MasterPage()
    {
        var page1 = new Page1();
        var page2 = new Page2();

        page1.ItemSelected += (sender, args) =>
        {
            if (args.SelectedItem is string answer && answer.Equals("Yes"))
            {
                CurrentPage = page2;
            }
        };

        Children.Add(page1);
        Children.Add(page2);
    }
}

public class Page1 : ContentPage
{
    public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;

    public Page1()
    {
        Title = "Page1";

        var labelQuestion = new Label()
        {
            Text = "Do you want to switch to Page2?"
        };

        var listViewAnswers = new ListView()
        {
            ItemsSource = new string[] { "Yes", "No" },
            ItemTemplate = new DataTemplate(() =>
            {
                var cell = new TextCell();
                cell.SetBinding(TextCell.TextProperty, ".");
                return cell;
            })
        };

        listViewAnswers.ItemSelected += (sender, args) => ItemSelected?.Invoke(this, args);

        Content = new StackLayout()
        {
            Children =
            {
                labelQuestion,
                listViewAnswers
            }
        };
    }
}

public class Page2 : ContentPage
{
    public Page2()
    {
        Title = "Page2";

        Content = new StackLayout()
        {
            Children =
            {
                 new Label()
                    {
                        Text = "Page2"
                    }
            }
        };
    }
}

答案 2 :(得分:0)

删除json_encode只需打印$ user

答案 3 :(得分:0)

你应该推送一个没有键的数组来获得预期的数据:

$users = array();
$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
    $users[] = [ $row['id'], $row['last_active'] ];
}

因此,$users将等于:

$users = array (
  0 => array(1, 1522921015),
  1 => array(2, 1522921019),
  2 => array(3, 1522921102),
  3 => array(4, 1522921195),
  4 => array(5, 1522921034)
);

或者,使用fetch_array()MYSQLI_NUM选项:

while ($row = $stmt->fetch_array(MYSQLI_NUM)) {
    $users[] = $row;
}

答案 4 :(得分:-1)

我的选择将是这样的:

$users = array();

$stmt = $mysqli->query('SELECT id, last_active FROM users WHERE status = 1');
while ($row = $stmt->fetch_assoc()) {
      $users[] = [
          $row['id'],
          $row['last_active'],
     ];
}

echo json_encode($users);