dede
dede

Reputation: 841

How can I combine two-dimensional arrays in php

I need to combine 2 arrays. for example:

$array1 = array(
   [0] = array([0]=>1, [1]=>12-19-2014, [3]=>1113, [4]=>credit, [5]=>50000)
);

and

$array2 = array([0]=>1, [1]=>12-19-2014, [3]=>1111, [4]=>debet, [5]=>50000);

and become

$array = array(
    [0]=>array([0]=>1, [1]=>12-19-2014, [3]=>1113, [4]=>credit, [5]=>50000)
    [1]=>array([0]=>1, [1]=>12-19-2014, [3]=>1111, [4]=>debet,  [5]=>50000)
);

How I code that? Thanks for all answer.... :)

Thank for all who give me answer or comment. This is just an example. Now, I try to implement it into my script but I don't know why it is not working.

here is my table called table1:

||id || Date || explanation || debit_account || credit_account || debit || credit ||

Then if I have data (1, '12-19-2014', "buying mineral water", 1113, 1112, 5000, 0), Users (or accountant ) want to see it as journal accounting standard like below:

||   Date    ||    Explanation       || Account || Debit || Credit ||
||12-19-2014 || Buying mineral water ||  1113   ||  5000 ||        ||
||12-19-2014 || Buying mineral water ||  1112   ||       ||  5000  ||

So I code them using CodeIgniter and my code in Models as below:

function listRecord()
$sql = mysql_query("SELECT * from table1");
while($row = mysql_fetch_array($sql)) {
    $array1 = array (
      "id"=>$row["id"],
      "Date"=>$row["Date"],
      "explanation"=>$row["explanation"],
      "account"=>$row["debit_account"],
      "debit" =>$row["debit"],
      "credit"=>""   // or null
    );

   $array2 = array (
      "id"=>$row["id"],
      "Date"=>$row["Date"],
      "explanation"=>$row["explanation"],
      "account"=>$row["debit_account"],
      "debit" =>"", // or null,
      "credit"=>$row["debit"] 
    );
array_push($array1, $array2);
}

return $array1;
}

Then in Views :

foreach ($listRecord as $row) {
echo '<tr><td>'.$row['id'].'</td><td>'.$row['date'].'</td<td>'.$row['explanation'].'</td><td>'.$row['account'].'</td><td>'.$row['debit'].'</td><td>'.$row['credit'].'</td></tr>';
}

Unfortunately, I don't get the result as I expected. All answer will be thankful.

Upvotes: 0

Views: 135

Answers (4)

husnixs
husnixs

Reputation: 37

    function listRecord(){
$sql = mysql_query("SELECT * from table1");
$rows=array();
while($row = mysql_fetch_array($sql)) {
    $array[] = $row["id"];
    $array[] = $row["Date"];
$array[] = $row["explanation"];
$array[] = $row["debit_account"];
$array[] = $row["credit"];
array_push($rows, $array);
} return json_encode($rows);
}

Upvotes: 0

Ateoa
Ateoa

Reputation: 116

$array = $array1;
$array[] = $array2;

Upvotes: 4

Sharma Vikram
Sharma Vikram

Reputation: 2470

$array1 = array(
       '0' => array('1', '12-19-2014', '1113', 'credit', '50000')
    );
    echo 'Array 1<pre>';
    print_r($array1);

    $array2 = array('1', '12-19-2014', '1111', 'debet', '50000');
    $array_push=array($array2);
    echo 'Array 2<pre>';
    print_r($array2);
    $array3=array_merge($array1,$array_push);
    echo 'Array 3<pre>';
    print_r($array3);
    ?>

array output

Upvotes: 1

Karthi Skb
Karthi Skb

Reputation: 322

Use array_push:

$array1 = array(
   array(1, '12-19-2014', 1113, 'credit', 50000)
);

$array2 = array(1, '12-19-2014', 1111, 'debet', 50000);

array_push($array1, $array2);

print_r($array1);

Upvotes: 0

Related Questions