如何从多维数组中获取价值并在PHP中推送另一个多维数组

时间:2018-11-28 14:43:02

标签: php multidimensional-array

这里我有两个数组

新闻

$news= Array
            (
                "100" => Array
                    (
                        "activationDate" => "2018-11-28 16:19:22"
                    ),

                "200" => Array
                    (
                        "activationDate" => "2018-11-28 16:21:06"
                    ),

                "300" => Array
                    (
                        "activationDate" => "2018-11-28 16:23:18"
                    )

            );

频道

 $channel= Array
(
    "0" => Array
        (
            "id" => 200,
            "status" => "available"
        ),

    "1" => Array
        (
            "id" => 100,
            "status" => "No"
        )

);

我的要求是,channel id等于news索引意味着我想使用activationDate并推送到频道,我们如何实现这一目标,请帮助我,

4 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码:

    <?php
$activeTopics = Array
(
    "100" => Array
    (
        "activationDate" => "2018-11-28 16:19:22"
    ),

    "200" => Array
    (
        "activationDate" => "2018-11-28 16:21:06"
    ),

    "300" => Array
    (
        "activationDate" => "2018-11-28 16:23:18"
    )

);
$pedagogyTopics = Array
(
    "0" => Array
    (
        "pedagogyID" => 200,
        "higherLevelStatus" => "available"
    ),

    "1" => Array
    (
        "pedagogyID" => 100,
        "higherLevelStatus" => "No"
    )

);

foreach ($pedagogyTopics as $key => $pedagogyTopic) {
    if (is_array($activeTopics[$pedagogyTopic['pedagogyID']])) {
        $pedagogyTopics[$key]['activationDate'] = $activeTopics[$pedagogyTopic['pedagogyID']]['activationDate'];
    }
}
echo '<pre>';
var_dump($pedagogyTopics);
echo '</pre>';
?>

答案 1 :(得分:0)

$News = Array (
    "100" => Array (
        "activationDate" => "2018-11-28 16:19:22"
    ),

    "200" => Array (
        "activationDate" => "2018-11-28 16:21:06"
    ),

    "300" => Array (
        "activationDate" => "2018-11-28 16:23:18"
    )
);

$Channel = Array (
    "0" => Array (
        "uniqueIDs" => 200,
        "higherLevelStatus" => "available"
    ),

    "1" => Array (
        "uniqueIDs" => 100,
        "higherLevelStatus" => "No"
    )
);

$result = array();

foreach ($News as $k => $v)
{
    foreach ($Channel  as $k2 => $v2)
    {
        if ($v2["uniqueIDs"] == $k) {
            array_push($result, array_merge($v, $v2));
        }
    }
}

print_r($result);

当第一个数组中的键等于第二个元素的唯一ID时,将两个数组合并并将其推入结果数组。然后打印

编辑:

如果您不希望有多个foreach,则可以执行以下操作(但它将修改原始数组)

foreach ($Channel as $k => &$v) { // note the use of a reference with &$v
    if (isset($News[$v["uniqueIDs"]]))
        $v["activationDate"] = $News[$v["uniqueIDs"]]["activationDate"];
}

print_r($Channel);

答案 2 :(得分:0)

您可以按如下所示修改现有阵列

Watch Demo Here

foreach($pedagogyTopics as &$item )
{
       if(isset( $activeTopics[ $item['pedagogyID'] ] ))
       {
          $item["activationDate"] = $activeTopics[$item['pedagogyID']]["activationDate"];
       }
}

unset($item);

print_r($pedagogyTopics);

答案 3 :(得分:0)

请尝试以下代码。

    $activeTopics = Array
    (
        "100" => Array
            (
                "activationDate" => "2018-11-28 16:19:22"
            ),
         "200" => Array
            (
                "activationDate" => "2018-11-28 16:21:06"
            ),
         "300" => Array
            (
                "activationDate" => "2018-11-28 16:23:18"
            )
);

$pedagogyTopics = Array
(
"0" => Array
    (
        "pedagogyID" => 200,
        "higherLevelStatus" => "available"
    ),

"1" => Array
    (
        "pedagogyID" => 100,
        "higherLevelStatus" => "No"
    )

);

 foreach ($pedagogyTopics as &$pedagogyTopic) {
    if(isset($activeTopics[$pedagogyTopic["pedagogyID"]]) && 
    !empty($activeTopics[$pedagogyTopic["pedagogyID"]])){
         $pedagogyTopic["activationDate"] = 
         $activeTopics[$pedagogyTopic["pedagogyID"]]["activationDate"]; 
    }
}

print_r($pedagogyTopics);