Solved

Join Arrays from record returned

Posted on 2013-11-10
5
326 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

829 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