1

I have an existing array, and I want to add some items in it from a mysql row

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

        array_push($apiresults, $extendedadmindetailsrow);

This returns an array in an array:

 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
        )

)

while I need:

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


)

I believe I should use array_push to add to an existing array, but I'm not sure how to proceed from there. Do I need to loop trough the extendedadmindetailsrow array and add items 1 by 1? Any one can help me out with this?

Thanks!!

4 Answers 4

2

use of array_merge is better in case

// 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);
Sign up to request clarification or add additional context in comments.

Comments

2

Take a look at array_merge

array_merge($apiresults, $extendedadmindetailsrow);

Comments

2

You can:

$result = $apiresults + $extendedadmindetailsrow;

Comments

1

Use array_merge()

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

Output will be

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

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.