1

code:

<?php

$this->db->select('*');
$this->db->from('student');
$this->db->order_by('studentID','DESC');
$sql1 = $this->db->get();
$result1 = $sql1->result_array();
foreach($result1 as $arr1)
{
    $array1[] = array(
                        'link' => 'student',
                        'values' => '<b>'.$arr1['create_username'].'</b> added new student <b>'.$arr1['name'].'</b>',
                        'dates' => $arr1['s_date']
                    );
}

$this->db->select('*');
$this->db->from('professor');
$this->db->order_by('professorID','DESC');
$sql2 = $this->db->get();
$result2 = $sql2->result_array();
foreach($result2 as $arr2)
{
    $array2[] = array(
                        'link' => 'professor',
                        'values' => '<b>'.$arr2['create_username'].'</b> added new professor <b>'.$arr2['name'].'</b>',
                        'dates' => $arr2['s_date']
                    );
}

$this->db->select('*');
$this->db->from('classes');
$this->db->order_by('classesID','DESC');
$sql3 = $this->db->get();
$result3 = $sql3->result_array();
foreach($result3 as $arr3)
{
    $array3[] = array(
                        'link' => 'classes',
                        'values' => '<b>'.$arr3['create_username'].'</b> added new course <b>'.$arr3['name'].'</b>',
                        'dates' => $arr3['s_date']
                    );
}

foreach(array_combine($array1,$array2,$array3) as $rowss)
{
    echo '<li>
            <a href="'.base_url().''.$rowss['link'].'">
                <p>'.$rowss['values'].'</p>
            </a>
            <a href="javascript:void(0)">'.time_elapsed_string($rowss['dates']).'</p>
        </li>';
}

In this code I am simply run three query simultaneously for table student, professor and classes. Now, I want to combine or map all array i.e. array1, array2, array3 which is inside the foreach loop into one single array. So, How can I resolve this? Please help me.

Thank You

3
  • 2
    The method you are looking for is array_merge php.net/manual/fr/function.array-merge.php Commented Sep 20, 2019 at 7:35
  • This has nothing to do with jQuery. Please use appropriate tags. Commented Sep 20, 2019 at 7:37
  • array_merge($array1,$array2,....) Commented Sep 20, 2019 at 8:09

2 Answers 2

1

this is not codeigniter specific. In php you can merge multiple arrays using array_merge() native function

$finalArray = array_merge($array1, $array2, $array3);

Sign up to request clarification or add additional context in comments.

Comments

0

Merging two or more arrays using array_merge()

Syntax: array array_merge($array1, $array2, ......, $arrayn)

Here is your code:

<?php

    $array1 = array(
      array(
        'link' => 'student',
        'values' => '1',
        'dates' =>  '01'
      ),
      array(
        'link' => 'student',
        'values' => '2',
        'dates' =>  '02'
      )
    );
    $array2 = array(
      array(
        'link' => 'professor',
        'values' => '1',
        'dates' =>  '01'
      ),
      array(
        'link' => 'professor',
        'values' => '2',
        'dates' =>  '02'
      )
    );
    $array3 = array(
      array(
        'link' => 'classes',
        'values' => '1',
        'dates' =>  '01'
      ),
      array(
        'link' => 'classes',
        'values' => '2',
        'dates' =>  '02'
      )
    );
    $res = array_merge($array1, $array2, $array3); 
    echo '<pre>';
    print_r($res);

?>

Output:

Array
(
    [0] => Array
        (
            [link] => student
            [values] => 1
            [dates] => 01
        )

    [1] => Array
        (
            [link] => student
            [values] => 2
            [dates] => 02
        )

    [2] => Array
        (
            [link] => professor
            [values] => 1
            [dates] => 01
        )

    [3] => Array
        (
            [link] => professor
            [values] => 2
            [dates] => 02
        )

    [4] => Array
        (
            [link] => classes
            [values] => 1
            [dates] => 01
        )

    [5] => Array
        (
            [link] => classes
            [values] => 2
            [dates] => 02
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.