Solved

Join Arrays from record returned

Posted on 2013-11-10
5
329 Views
Last Modified: 2013-11-10
I'm trying to pull a load of setting from a database table, and I'm trying to add the returned records to an existing array by using:-
array_merge($arrPrivileges,array($rowPrivileges['setting']->$rowPrivileges['value']));

Open in new window


However all Im getting back is "Trying to get property of non-object".

      function getPrivileges($userID,$section) {
        $resPrivileges = parent::query("select * from `userPrivs` where `userID`='" . mysqli::escape_string($userID) . "' AND `page`='" . mysqli::escape_string($section) . "';");

        $arrPrivileges = array();

        if ($resPrivileges->num_rows > 0) {
          while ($rowPrivileges = $resPrivileges->fetch_array()) {
            array_merge($arrPrivileges,array($rowPrivileges['setting']->$rowPrivileges['value']));
          }
        }
        return $arrPrivileges;
      }


var_dump(getPrivileges(1,'Menu'));

Open in new window


What should return is:-

Array
  (
     [Allow Users] => true,
     [Allow Addition] => false,
     [Allow Deletion] => true
  )

So what my question is how do I add the values from the row into the existing array?
0
Comment
Question by:tonelm54
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 39637117
It seems to me that $rowPrivileges IS an array already (not an object). So try:
          while ($rowPrivileges = $resPrivileges->fetch_array()) {
            $arrPrivileges = array_merge($arrPrivileges, $rowPrivileges );
          }
0
 

Author Comment

by:tonelm54
ID: 39637132
$rowPrivileges is an array, which returns:-

array(4) {
  ["userID"]=>
  string(1) "1"
  ["page"]=>
  string(4) "Menu"
  ["setting"]=>
  string(23) "Allow Addition of Users"
  ["value"]=>
  string(4) "true"
}

array(4) {
  ["userID"]=>
  string(1) "1"
  ["page"]=>
  string(4) "Menu"
  ["setting"]=>
  string(24) "Allow Addition of Points"
  ["value"]=>
  string(4) "true"
}

However I want only the 2 elements from the existing arrays so the resulting array should be is:-

array(4) {
  ["Allow Addition of Users"]=>
  string(4) "true"
  ["Allow Addition of Points"]=>
  string(4) "true"
}
0
 

Author Comment

by:tonelm54
ID: 39637155
So the value is
$rowPrivileges['setting'] = "Allow Addition of Users"
$rowPrivileges['value'] = "true"

So what my code (I belive is doing is):-
array_merge($arrPrivileges,array("Allow Addition of Users"->"true"));

But with variables instead of strings.
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39637156
OK, that helps.  You need "=>" (not "->") AND you also need to assign the result of array_merge() to $arrPrivileges

while ($rowPrivileges = $resPrivileges->fetch_array()) {
	$arrPrivileges = array_merge($arrPrivileges, array( $rowPrivileges['setting'] => $rowPrivileges['value']) );
}

Open in new window

0
 

Author Closing Comment

by:tonelm54
ID: 39637161
Oh, F£$&£%$&%^&""$"^ Thats it? that simple? Feel like a complete idiot now!!!!!!!

Thank you :-)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question