1

Here i am having two arrays

News

$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

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

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

);

My requirement is, channel id is equal to news index means i want to take activationDate and push to channel,How we can achieve this, kindly help me out on this,

4 Answers4

0

You can try this code to use:

    <?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>';
?>
Oleg Nurutdinov
  • 617
  • 4
  • 17
  • Your code is working fine, it is possible to push data based on `activationDate` is `ASC` order –  Nov 28 '18 at 15:49
  • I tried but it is not working,please check my `update code` –  Nov 28 '18 at 16:26
  • @Prasanna there's no problem of my code it can't be sorted by values in `activationDate` keys, cause this values is a strings. Decision of you sorting question's here https://stackoverflow.com/questions/40462778/how-to-sort-date-array-in-php – Oleg Nurutdinov Nov 28 '18 at 17:36
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);

when a key in the first array is equal to a uniqueIDs of an element of the second merge both arrays and push it in a result array. then print

EDIT:

if you don't want multiple foreach you can do something like this (but it will modify your original array)

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);
Community
  • 1
  • 1
RomMer
  • 113
  • 12
0

You may modify your existing array like below

Watch Demo Here

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

unset($item);

print_r($pedagogyTopics);
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0

Please try below code.

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