Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Join Arrays from record returned

Posted on 2013-11-10
5
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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