将mysql行添加到现有数组

时间:2017-02-26 11:55:59

标签: php mysql

我有一个现有的数组,我想从mysql行添加一些项目

 <table class='div 12'>
<thead  class='div 12'>...</thead>
<tbody  class='div 12'>
     ...
    <tr class='div 12'><td><div class='div 12'><form >....</form></div></td></tr>
     ...
</tbody>
</table class='div 12'>

这将返回数组中的数组:

        $extendedadmindetails = full_query("SELECT * FROM `tbladmins` WHERE `id`='{$_SESSION['adminid']}'");
        $extendedadmindetailsrow = mysql_fetch_assoc ($extendedadmindetails);

        array_push($apiresults, $extendedadmindetailsrow);

虽然我需要:

 Array
(
    [result] => success
    [adminid] => 1
    [name] => My Name
    [notes] => 
    [signature] => 
    [allowedpermissions] => My Name
    [departments] => 1
    [requesttime] => 2017-02-26 12:44:06
    [0] => Array
        (
            [id] => 1
            [uuid] => sqdqsdqsdqsdq454
            [roleid] => 1
            [username] => Myname
            [password] => $dfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf
            [passwordhash] => $jghjghjghjghjghjghjghjghjg                
            [updated_at] => 0000-00-00 00:00:00
        )

)

我相信我应该使用array_push添加到现有数组中,但我不确定如何从那里继续。我是否需要循环使用extendedadmindetailsrow数组并逐项添加项目? 任何人都可以帮我解决这个问题吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

使用array_merge()

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));

输出

Array ( 
    [0] => red 
    [1] => green
    [2] => blue
    [3] => yellow
)

答案 1 :(得分:1)

查看array_merge

array_merge($apiresults, $extendedadmindetailsrow);

答案 2 :(得分:1)

使用array_merge会更好

// Considering your mysql is returning only 1 row
foreach ($extendedadmindetailsrow as $key => $row) {
  $arr = $row;
}

// after this if you will try array_push also that will work
$result = array_merge($apiresults, $arr);
print_r($result);

答案 3 :(得分:1)

你可以:

$result = $apiresults + $extendedadmindetailsrow;