Solved

Join Arrays from record returned

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

615 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