【问题标题】:How to take value from multidimensional array and push another multidimensional array in PHP如何从多维数组中获取值并在 PHP 中推送另一个多维数组
【发布时间】:2018-11-28 14:43:01
【问题描述】:

这里有两个数组

新闻

$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 推送到频道,我们如何实现这一点,请帮助我解决这个问题,

【问题讨论】:

  • StackOverflow 不是代码编写服务。您应该先向我们展示您尝试过的内容,而不是简单地寻求解决方案。然后,有人可能会很乐意帮助您并“尽量避免循环内的循环,因为 [您] 在 [您的] 数组中有大量记录”。

标签: php multidimensional-array


【解决方案1】:

你可以试试这段代码来使用:

    <?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>';
?>

【讨论】:

  • 你的代码运行正常,可以根据activationDateASC的顺序推送数据
  • 我试过了,还是不行,请查看我的update code
  • @Prasanna 我的代码没有问题,它不能按activationDate 键中的值排序,因为这个值是一个字符串。你在这里排序问题的决定stackoverflow.com/questions/40462778/…
【解决方案2】:

您可以修改现有数组,如下所示

Watch Demo Here

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

unset($item);

print_r($pedagogyTopics);

【讨论】:

    【解决方案3】:

    请尝试以下代码。

        $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);
    

    【讨论】:

      【解决方案4】:
      $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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 2019-12-07
        • 2019-08-09
        • 2017-08-31
        • 1970-01-01
        相关资源
        最近更新 更多