将键值对从SQL查询推送到数组中

时间:2017-06-22 19:36:54

标签: php arrays

我从数据库中提取数据,并希望将其插入数组中。我希望数组看起来像

$myArray = array(
           $id => array(
                        "title" => $title,
                        "description" => $description,
                        "tag" => $tag,
                        "link" => $link
                        )
                 )

其中$ id是从数据库中提取的ID,$ title从数据库的“title”列中提取,依此类推。

我需要连接所有这些数据,以便我可以使用模板轻松显示信息。

目前这是我的代码:

<?php
$query = mysqli_query($db_conn, $advice);
//Arrays for the key value pairs
$pregnancyArray = array();
$postpartumArray = array();
$babyArray = array();
$toddlerArray = array();
$parentingArray = array();

//Arrays for now
$pregnancyTitleArray = array();
$pregnancyLinkArray = array();
$pregnancyDescriptionArray=array();
$pregnancyTagArray=array();

$postpartumTitleArray = array();
$postpartumLinkArray = array();
$babyTitleArray = array();
$babyLinkArray = array();
$toddlerTitleArray = array();
$toddlerLinkArray = array();
$parentingTitleArray = array();
$parentingLinkArray = array();

function createSlug($slug)
{
    $LNSH = '/[^\-\s\pN\pL]+/u';
    $SDH = '/[\-\s]+/';
    $slug = preg_replace($LNSH, '', mb_strtolower($slug, 'UTF-8'));
    $slug = preg_replace($SDH, '-', $slug);
    $slug = trim($slug, '-');
    return $slug;
}

while ($row = mysqli_fetch_array($query)) {
    $id = $row['adviceID'];
    $title = $row['title'];
    $cat = $row['cat'];
    $link = createSlug($title);
    $description = $row['description'];
    $tag = $row['tag'];


    if ($cat === 'pregnancy') {

        array_push($pregnancyTitleArray, $title);
        array_push($pregnancyLinkArray,$link);
        array_push($pregnancyDescriptionArray, $description);
        if($tag){
            array_push($pregnancyTagArray, $tag);
        }
        else {
            array_push($pregnancyTagArray, "nt");
        }

    } elseif ($cat === 'postpartum') {
        array_push($postpartumTitleArray, $title);
        array_push($postpartumLinkArray,$link);
    } elseif ($cat === 'baby') {
        array_push($babyTitleArray, $title);
        array_push($babyLinkArray,$link);
    } elseif ($cat === 'toddler') {
        array_push($toddlerTitleArray, $title);
        array_push($toddlerLinkArray,$link);
    } elseif ($cat === 'parenting') {
        array_push($parentingTitleArray, $title);
        array_push($parentingLinkArray,$link);
    } else {
        continue;
    }
}
    ?>
<section class="content" id="pregnancy-advice">
    <h2 class="heading">Pregnancy Advice</h2>
    <ul class="justified-content" style="list-style-type: none;">
    <?php
        for($i = 0; $i < sizeof($pregnancyTitleArray) && $i <= 15; $i++){
          echo "<li class=\"extra-spacing\"><a href=\"atemplate.php?\"" . $pregnancyLinkArray[$i] .  ">" . $pregnancyTitleArray[$i] . "</a></li>";
        }
     ?>
        <a class="more-links" href="adviceCat.php?cat=pregnancy">More -></a>
    </ul>
</section>

1 个答案:

答案 0 :(得分:0)

你能用你的结果集构建一个数组:

$results = [];
while ($row = mysqli_fetch_array($query)) {
  $results[$row['adviceID']] = $row;
  $results[$row['adviceID']]['link'] = createSlug($title);
}

如果您只想在阵列中拥有某些数据,则可以采用不同的方式构建数据:

$results = [];
while ($row = mysqli_fetch_array($query)) {
  $results[$row['adviceID']] = array(
    'title' => $row['title'], 
    'description' => $row['description'],
    'tag' => $row['tag'],
    'link' => createSlug($title),
  );
}