在精确索引中将值存储在数组中

时间:2019-01-03 10:15:35

标签: php mysql

我正在制作二元遗传树。我得到了所有下线,但想知道如何构造它们?

这是给我的mlm项目使用的,我以前从未做过这样的项目,所以我对如何设置这种二进制结构一无所知。

这是迄今为止我在遗传学模型中尝试过的方法。

public $leaves = array();

public function __construct() {
  $this->db = new Database;
  $this->getGenealogy($_SESSION['activationCode']);
}

public function leaves() {
  return $this->leaves;
}

public function getGenealogy($parent) {
  $result;
  $l = array();
  $this->db->sql('SELECT * FROM accounts WHERE sponsorUpline = ?');
  $this->db->bindValue(1, $parent);
  $this->db->execute();
  $rows = $this->db->fetchAll();
  $result = $rows;
  foreach($result as $r) {
    array_push($this->leaves, $r->serialNumber);
    $this->getGenealogy($r->serialNumber);
  }
}

这是我的仪表板控制器

public function __construct() {
  $this->db = $this->model('Dashboard');
  $this->ddrs = $this->model('DashboardDirectReferralStructure');
  $this->bg = $this->model('Genealogy');
}

public function index() {
  $memberAccounts = $this->db->getMemberAccounts();
  $getDirectReferal = $this->ddrs->getDirectReferal();
  $leaves = $this->bg->leaves();
  $data = [
    'memberAccounts' => $memberAccounts,
    'getDirectReferal' => $getDirectReferal,
    'leaves' => $leaves
  ];
  $this->view('dashboards/dashboard', $data);
}

我想要实现的是从数据库中获取数据时,我想将它们存储在一个数组中以供参考。

enter image description here

在上图中,如果我将其可视化为数组(不是多维),我们将有类似的东西。

  array(
    [0] => '01',
    [1] => '02',
    [2] => '03',
    [3] => '04',
    [4] => 'new member',
    [5] => '06',
    [6] => '07',
    [7] => '08',
    [8] => 'new member',
    [9] => 'empty',
    [10] => 'empty',
    [11] => '12',
    [12] => '13',
    [13] => '14',
    [14] => '15'
  )

我在[9]和[10]中放了空,因为这是我为家谱所做的计划。

但是,如果我将值放在数组中,我会得到这个

      array(
    [0] => '01',
    [1] => '02',
    [2] => '03',
    [3] => '04',
    [4] => '06',
    [5] => '07',
    [6] => '08',
    [7] => '12',
    [8] => '13',
    [9] => '14',
    [10] => '15'
  )

所以我想做一个包含15个索引的数组,并将每个值存储在它们的精确索引中,例如,值1将放置在索引[0]上,而值2将不存在{{3} }将会被跳过,而是使用“空”值。

有什么办法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

我能想到的是,您可以简单地保存“ -1”代替“空或空”值。

array(
    [0] => '01',
    [1] => '02',
    [2] => '03',
    [3] => '04',
    [4] => 'new member',
    [5] => '06',
    [6] => '07',
    [7] => '08',
    [8] => 'new member',
    [9] => '-1',
    [10] => '-1',
    [11] => '12',
    [12] => '13',
    [13] => '14',
    [14] => '15'
  )

“-1”然后可以用来识别值的存在。 希望对您有所帮助。