数组中的特定值不起作用

时间:2017-10-18 07:23:09

标签: php arrays multidimensional-array foreach

我对我的阵列有疑问。如何在里程碑数组中仅获取里程碑数据?

我有一个带有前缀数组的查询。我获得里程碑和里程碑的数据(这是里程碑部分)。

这是我的代码:

$stones_fases = array();
while ($row = $db->fetchassoc($result)){
    $milestonefase = array();
    $milestone = array();

    foreach ($row as $mkey => $mvalue){
        $milestone[$mkey] = $mvalue;
        foreach ($row as $fkey => $fvalue){
            $milestonefase[$fkey] = $fvalue;
        }
    }

    if (!isset($stones_fases[$milestone['milestone_id']])){
        $stones_fases[$milestone['milestone_id']] = $milestone; //['client']['milestone_verkocht_id']
    }
    $stones_fases[$milestone['milestone_id']][$milestonefase['milestonefase_id']] = $milestonefase['milestonefase_titel'];
}

得到这个:

Array
(
    [int] => Array
        (
            [milestone_id] => int
            [milestone_titel] => string
            [client] => string
            [milestone_verkocht_id] => 99
            [milestonefase_id] => 10
            [milestonefase_titel] => string
            [milestonefase_milestone_id] => 6
            [10] => string
            [11] => string
        )
)

但我想要这个:

Array
(
    [int] => Array
        (
            [milestone_titel] => string
            [client] => string
            [milestone_verkocht_id] => int
            [10] => string
            [11] => string
        )
)

我的疑问是:

$project = $_COOKIE['project'];
$query = " SELECT 
                 a.id AS `milestone_id`, 
                 a.titel AS `milestone_titel`, 
                 a.client AS `client`,
                 a.verkocht_id AS `milestone_verkocht_id`,

                 b.id AS `milestonefase_id`, 
                 b.titel AS `milestonefase_titel`,
                 b.milestone_id AS `milestonefase_milestone_id`
                 FROM `milestones` a
                 INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
                 WHERE a.verkocht_id = '{$project}' ";
$result= $db->query($dbh, $query);

1 个答案:

答案 0 :(得分:1)

我解决了自己的问题!

如果我这样做:

while ($row = $db->fetchassoc($result)){
    $stones_fases[$row['milestone_id']]['milestone_titel'] = $row['milestone_titel'];
    $stones_fases[$row['milestone_id']]['milestone_client'] = $row['client'];
    $stones_fases[$row['milestone_id']]['milestonesfases'][$row['milestonefase_id']] = $row['milestonefase_titel'];
}

我得到了我的期望:

Array
(
    [2] => Array
        (
            [milestone_titel] => Beheer opleveren
            [milestone_client] => stackoverflow
            [milestonesfases] => Array
                (
                    [1] => Menu bouwen
                    [2] => Pagina beheer CMS
                    [3] => Projecten CMS
                    [4] => Portfolio
                    [5] => Footer inbouwen
                )

        )

)

看看我前一天写的那堆代码。

您是否看到我写的代码的不可见之处?

看一下我1分钟前写的代码!

我挣扎了3天,现在我解决了自己的问题!