Solved

Join Arrays from record returned

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now