PHP Algorithm Performance - php

I am looking for ways to improve the performance of this simple PHP function.
The issue is I am pulling data into an array from an API which unfortunately does not provide much scope for filtering. As a result, the dataset is initially quite large, before I loop through and unset() the key/value pairs I have no use for in my application.
Is there a better way to do this? Load time is currently 2000ms.
$callBokun = new callBokun($this->_op, $this->_currentDate);
$callBokun->setResponse("POST", "/booking.json/product-booking-search", "https://api.bokun.is/booking.json/product-booking-search");
$s = $callBokun->getResponse();
unset($s["tookInMillis"], $s["totalHits"], $s["totalHits"], $s["query"]);
$unset_loop = count($s["results"]);
while ($unset_loop > 0) {
--$unset_loop;
unset($s["results"][$unset_loop]["id"], $s["results"][$unset_loop]["externalBookingReference"], $s["results"][$unset_loop]["parentBookingId"], $s["results"][$unset_loop]["vendor"]["id"], $s["results"][$unset_loop]["vendor"]["flags"], $s["results"][$unset_loop]["productCategory"], $s["results"][$unset_loop]["productExternalId"], $s["results"][$unset_loop]["resold"], $s["results"][$unset_loop]["seller"], $s["results"][$unset_loop]["hasNotes"], $s["results"][$unset_loop]["assignedResources"], $s["results"][$unset_loop]["channel"], $s["results"][$unset_loop]["agent"], $s["results"][$unset_loop]["affiliate"], $s["results"][$unset_loop]["vessel"], $s["results"][$unset_loop]["harbour"], $s["results"][$unset_loop]["saleSource"], $s["results"][$unset_loop]["extranetUser"], $s["results"][$unset_loop]["agentUser"], $s["results"][$unset_loop]["cancellationDate"], $s["results"][$unset_loop]["cancelledBy"], $s["results"][$unset_loop]["cancelNote"], $s["results"][$unset_loop]["endDate"], $s["results"][$unset_loop]["customer"]["contactDetailsHidden"], $s["results"][$unset_loop]["customer"]["contactDetailsHiddenUntil"], $s["results"][$unset_loop]["customer"]["phoneNumberCountryCode"], $s["results"][$unset_loop]["customer"]["country"], $s["results"][$unset_loop]["customer"]["id"], $s["results"][$unset_loop]["customer"]["language"], $s["results"][$unset_loop]["customer"]["nationality"], $s["results"][$unset_loop]["customer"]["created"], $s["results"][$unset_loop]["customer"]["uuid"], $s["results"][$unset_loop]["customer"]["sex"], $s["results"][$unset_loop]["customer"]["dateOfBirth"], $s["results"][$unset_loop]["customer"]["address"], $s["results"][$unset_loop]["customer"]["postCode"], $s["results"][$unset_loop]["customer"]["state"], $s["results"][$unset_loop]["customer"]["place"], $s["results"][$unset_loop]["customer"]["organization"], $s["results"][$unset_loop]["customer"]["passportId"], $s["results"][$unset_loop]["customer"]["passportExpMonth"], $s["results"][$unset_loop]["customer"]["passportExpYear"], $s["results"][$unset_loop]["customer"]["credentials"], $s["results"][$unset_loop]["creationDate"], $s["results"][$unset_loop]["totalPrice"], $s["results"][$unset_loop]["productType"], $s["results"][$unset_loop]["customerInvoice"], $s["results"][$unset_loop]["resellerInvoice"], $s["results"][$unset_loop]["currency"], $s["results"][$unset_loop]["prepaid"], $s["results"][$unset_loop]["paidAmount"], $s["results"][$unset_loop]["resellerPaidType"], $s["results"][$unset_loop]["discountPercentage"], $s["results"][$unset_loop]["discountAmount"], $s["results"][$unset_loop]["unconfirmedPayments"], $s["results"][$unset_loop]["sellerCommission"], $s["results"][$unset_loop]["affiliateCommission"], $s["results"][$unset_loop]["agentCommission"], $s["results"][$unset_loop]["notes"], $s["results"][$unset_loop]["fields"]["pickupPlaceRoomNumber"], $s["results"][$unset_loop]["fields"]["pickupPlaceDescription"], $s["results"][$unset_loop]["fields"]["customizedPrice"], $s["results"][$unset_loop]["fields"]["dropoff"], $s["results"][$unset_loop]["fields"]["pickup"], $s["results"][$unset_loop]["fields"]["flexible"], $s["results"][$unset_loop]["fields"]["partnerBookings"], $s["results"][$unset_loop]["fields"]["startTimeId"], $s["results"][$unset_loop]["fields"]["startHour"], $s["results"][$unset_loop]["fields"]["selectedFlexDayOption"], $s["results"][$unset_loop]["fields"]["dropoffPlaceDescription"], $s["results"][$unset_loop]["fields"]["comboParent"], $s["results"][$unset_loop]["fields"]["startMinute"], $s["results"][$unset_loop]["fields"]["priceCategoryBookings"], $s["results"][$unset_loop]["boxBooking"], $s["results"][$unset_loop]["boxProduct"], $s["results"][$unset_loop]["boxSupplier"], $s["results"][$unset_loop]["contactDetailsHidden"], $s["results"][$unset_loop]["contactDetailsHiddenUntil"]);
}
$this->_bookingInfo = $s;
Thanks in advance!

It is probably more efficient to build a new array with the properties you do want to keep. unset is a costly operation.
Here is the pattern you could use if you want to keep a field with name booking1 (just add any other field in the same way):
$s = $callBokun->getResponse();
foreach($s["results"] as $row) {
$result[] = [
"booking1" => $row["booking1"],
// assign any other fields you want to keep in the same way
// ...
];
}
$this->_bookingInfo = $result;
Alternative 1
An alternative way is with array_map:
$this->_bookingInfo = array_map(function ($row) {
return [
"booking1" => $row["booking1"],
// assign any other fields you want to keep in the same way
// ...
];
}, $s['result']);
Alternative 2
You could also try with array_intersect_key:
$filter = array_flip([
'booking1',
// list all your desired fields here
]);
foreach($s["results"] as $row) {
$result[] = array_intersect_key($row, $filter);
}
$this->_bookingInfo = $result;
You can of course combine the two alternatives.

Related

Issue with PHP recursive function

I need to traverse the following type of structures:
P
/ | \
E1 E2 E3 .....
/ \ / \ |
V1 V2 V1 V2 V3 .....
| | | | / \
T1 T2 T3 T4 T5 T6 .....
In order to form an associative array with the following elements:
V1(key) = [T1(E1), T3(E2), ...]
V2(key) = [T2(E1), T4(E2), ...]
V3(key) = [T5(E3), T6(E3), ...]
.....
Now here comes the tricky part: the structure is actually simplified. I don't know beforehand how many E-level nodes I'll actually need to deal with (3 in the drawing), or how many V-level nodes each of them has (but at least 1 will be there), and furthermore, each V-level node may also have multiple T-nodes.
I tried using a recursion function to do this (in PHP). I'll simplify the code because it has weird methods regarding some objects that don't really matter to the discussion. My current attempt results in:
V1(key) = [T1(E1)]
V2(key) = [T2(E1)]
That I think means that the traversal is only occurring going down the first E-level "branch".
This is my code:
$result = [];
$traverser = function($node) use (&$traverser, &$result) {
$children = $node->getChildrenArray();
foreach($children as $key=>$child){
if ($child->nodeType() == 'v_node') {
$v_node_key = $child->name;
$t_nodes = $child->getChildrenArray();
if ( !array_key_exists($v_node_key, $results) ){
$results[$v_node_key] = [];
}
foreach($t_nodes as $keyt=>$t_node) {
$info_array = $t_node->toArray();
array_push($results[$v_node_key], $info_array);
}
} else if ($child->nodeType() == 'e_node') {
// keep digging
return $traverser($child);
}
}
};
$traverser($p_node);
I think the problem is that once I call the $traverser function within the foreach it won't come back and resume from the previous state.
Can anyone advise on how I should be tackling this to get the result I placed above?
Well, this is a bit awkward and I'm still not entirely sure if this is the right motive, but I solved this by removing the return in my code.
I thought that the return would allow me to exit the nested function call, but rather I think it jumped out of the first function call (the $traverser($p_node); line).
Even so, by changing the return $traverser($child); line to $traverser($child); it did what it had to do.
Hope this helps anyone!
I not show about what error you got but i suggest you to change you function to be this
function traverser($results, $node) {
$children = $node->getChildrenArray();
foreach($children as $key=>$child){
if ($child->nodeType() == 'v_node') {
$v_node_key = $child->name;
$t_nodes = $child->getChildrenArray();
if ( !array_key_exists($v_node_key, $results) ){
$results[$v_node_key] = [];
}
foreach($t_nodes as $keyt=>$t_node) {
$info_array = $t_node->toArray();
array_push($results[$v_node_key], $info_array);
}
} else if ($child->nodeType() == 'e_node') {
// keep digging
return traverser($child);
}
}
return $results;
}
Hope this help
Well, since you know that you'll have P->E->V->T-nodes, you could simply go for multiple foreach-loops, like this
foreach($p_node->getChildren() as $e_node) {
$e_node_key = $e_node->name;
foreach($e_node->getChildren() as $v_node) {
$v_node_key = $v_node->name;
foreach($v_node->getChildren() as $t_node) {
$t_node_key = $t_node->name;
// do whatever it needs to array_push to results
}
}
}

PHP Processing stream_get_contents into an array

I am having troubles converting a string which contains tab delimited CSV output
into an array. While using the following code :
$data = stream_get_contents($request->getShipmentReport());
I get back data as following:
Date Shipped Comments Feedback Arrived on Time
5/11/15 2 comment response Yes
Now I would like to process this data into an array with the returned headers(line 1) as the index containing the value for each line which follows after that.
I am trying to end up with something like this :
$line['Date'] = 5/11/15
$line['Shipped'] = 2
$line['Feedback'] = response
$line['Arrived on Time'] yes
What would be the best way to achieve this ?
I recommend using a library for handling CSV for example the CSV-Package by The League of Extraordinary Packages
You can pass your data and start working with array structures right away:
$csvString = stream_get_contents($request->getShipmentReport());
$csvReader = \League\Csv\Reader::createFromString($csvString);
$data = $csvReader->fetchAssoc(
array('Date', 'Shipped', 'Comments', 'Feedback', 'Arrived on Time')
);
// Or since line 0 contains header
$data = $csvReader->fetchAssoc(0); // 0 is offset of dataset containing keys
Now your data should be in a structure like:
$data = array(
array(
'Date' => '5/11/15',
'Shipped' => '2',
...
),
array(
...
)
);
You can even filter out specific datasets and all kinds of fancy stuff. Just check the documentation I linked to.
League CSV reader gives me a bad result. I use this instead:
$csvReportString = stream_get_contents($request->getReport());
$csvReportRows = explode("\n", $csvReportString);
$report = [];
foreach ($csvReportRows as $c) {
var_dump($c);
if ($c) { $report[] = str_getcsv($c, "\t");}
}
var_dump($report);

Generate the set of data into Json instead of one big mixed Json

How can i create one Json with many small sets of data separated by comma?
Instead of one big Json enclosed by double curly brackets?
I do receive a Json and with php i do use foreach to loop over it, making a lot of data processing inside.
Then generate a new Json, just to avoid the data processing on the client side wich will be processed by angularjs ng-repeat.
All the json data is mixed into one big json set (inside double curly brackets)
My goal is to separate into small sets of data.
I can use the NrType property. In this script the NrType receives the last atribution and just the last received is available.
//The php script
$arr = json_decode($returnedJson); //The original json to be pre-processed
$processedData = "[]";
$processedJson = json_decode($processedData,true);
foreach($arr as $key=>$value) {
foreach($value as $vkey=>$vvalue) {
if( $value[$i]->NrType == 1 ) {
$VlMIni = $value[$i]->QttyInitial;
$VlMSub = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
$VlMRec = $value[$i]->RealValue;
$VlMTotal += $VlMesRece;
//much more data processing going on here ...
} elseif( $value[$i]->NrType == 2 ) {
.
.
.
//and much more data processing going on here ...
}
}
//simple data atribution here
$processedJson['labelDesIni'] = 'Instruments';
$processedJson['labelValueMIni'] = $lblVlMIni;
$processedJson['labelValuePIni'] = $lblVlPlIni;
$processedJson['labelValueAIni'] = $lblVlAIni;
$processedJson['labelValuePAIni'] = $lblVlPAIni;
$processedJson['labelValuePercInic'] = $lblVlPercInic;
$processedJson['labelValuePerc2Inic'] = $lblVlPerc2Inic;
//much more data atribution ...
echo json_encode($processedJson); //the new hgenerated Json
The generated Json :
{
labelDesI: "Inspection",
labelValueMI: "2357",
labelValuePlI: "3914066",
labelValueAI: "1389406",
labelValuePAI: "2431425",
labelValuePercI: 57.143691456656,
labelValuePerc2I: 35.497766261478,
labelDesR: "Instruments",
labelValueMR: "734.54",
labelValuePR: "819.14",
labelValueAR: "660.05",
labelValuePAR: "877.94",
labelValuePercR: 80.087,
labelValuePerc2R: 44.739,
labelDesAcfi: "Fiscalização",
labelValueMAcfi: "343",
labelValuePlAcfi: "29907",
labelValueAAcfi: "16718",
labelValuePAAcfi: "16493",
labelValuePercAcfi: 101.36421512157,
labelValuePerc2Acfi: 55.899956531916,
labelDesT: "Totals",
labelValueMT: 365.59,
labelValuePlT: 547.62,
labelValueAnT: 909.63,
labelValuePAnT: 957.63,
labelValuePercT: 22949,
labelValuePerc2T: 25065
}
The desired format Would be this :
{
label: "Inspection",
labelValue1: "2357",
labelValue2: "3914066",
labelValue3: "1389406",
labelValue4: "2431425",
labelValue5: 57.1456656,
labelValue6: 35.4961478
},
{
labelDesR: "Instruments",
labelValue1: "734.54",
labelValue2: "819.14",
labelValue3: "660.05",
labelValue4: "877.94",
labelValue5: 80.087,
labelValue6: 44.739
},
{
labelDesT: "Totals",
labelValue1: 365.59,
labelValue2: 547.62,
labelValue3: 909.63,
labelValue4: 957.63,
labelValue5: 22949,
labelValue6: 25065
}
Thank´s in advance
generate all of your object separately, create an array of these objects and json_encode the array:
$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];
and add it to you main object:
$processedJson[] = $processedJsonElement;
do this for each section of Json you want to represent. Not sure how you're structuring your Foreach loop as your code doesn't match the output, but whatever you structure is whatever you will will output when you call json_encode.
Basically, you need to structure your foreach loop to be able to compartmentalize the objects you wish to represent as an array of json objects.

php - Remove duplicates from current array while looping through the same array?

i'm playing with cURL to crawl pages and extract links. This is some code that targets my issue.
for($i = 0; $i < sizeof($links); $i++){
$response = crawl($links[$i]);
//inside this loop i extract links for each crawled html
$newLinks = getLinks($response);
//I need to append these new links to current array in loop
$links= array_values(array_unique(array_merge($links, $newLinks));
}
I need to prevent for duplicate links so i don't crawl twice. I wonder if this is a safe approach or if it's right at all, since array_values whould reindex the elements of the array and while in loop the crawling could run twice for some link.
I could test with in_array() against $links and $newLinks to avoid duplicates but i wonder what happens when doing like my sample here.
This example work if you get only one link by function getLinks();
$newLink = getLinks($response);
if(!in_array($newLink,$links)){
$links=array_merge($links, array($newLink));
}
If you get more link, you can use foreach for links
foreach($newLinks as $newLink){
if(!in_array($newLink,$links)){
$links=array_merge($links, array($newLink));
}
}
TL/DR: Use array_merge($links, array_diff($newLinks, $links));
My justification:
Welcome to the land of exponential growth. As your collected links array grows, the time it takes will go through the roof. Give array_merge(array_diff($links, $new_links)) a chance instead. Using this benchmarking code:
function get($num)
{
$links = array();
for($i=0;$i<rand(5,20);$i++)
{
$links[] = rand(1,$num);
}
return $links;
}
function test($iter, $num_total_links)
{
$unique_time = 0;
$unique_links = array();
$diff_time = 0;
$diff_links = array();
for($i=0;$i<$iter;$i++)
{
$new_links = get($num_total_links);
$start = microtime(true);
$unique_links =
array_values(array_unique(array_merge($unique_links, $new_links)));
$end = microtime(true);
$unique_time += $end - $start;
$start = microtime(true);
$diff_links = array_values(array_merge(array_diff($new_links, $diff_links)));
$end = microtime(true);
$diff_time += $end - $start;
}
echo $unique_time . ' - ' . $diff_time;
}
You can tweak the values; if you expect to surf a large number of pages with relatively few links in common, pick a large (not too large or it'll take forever) $iter and $num_total_links. If you're likely to see many of the same link, reduce the $num_total_links accordingly.
What it boils down to is that a merge and then unique operation always requires you to merge in the same number of links, while a diff and then merge only requires you to merge in the links you want to add. This is nearly always more efficient, even at numbers that aren't exactly huge; surfing 500 pages with between 5 and 20 links makes a huge difference in time, and even a small number of pages can show a marked difference.
Looking at the data, foreach isn't a bad way to go as compared with array_unique; doing a similar benchmark, foreach consistently beat array_unique. But both are O(n^2), with foreach just growing more slowly. array_diff preserves O(n) time, which is your best case; your algorithm is never going to get any faster than some multiple of the number of pages you visit. PHP's built-in array functions are going to be faster than pretty much any solution written in PHP.
Here is the data, with the random factors taken out because all they did was cause deviations, not affect the results. I've also ramped up the possible "universe" of URLs to a large number for illustration purposes; at a smaller number, both array_unique and foreach produce graphs which look almost linear, albeit that they continue to be outperformed by array_diff. If you plug this into Google Charts, you can see just what I'm talking about.
['Number of Pages', 'array_unique', 'array_diff', 'foreach'],
[1, 6.9856643676758E-5, 6.1988830566406E-5, 0.00012898445129395],
[11, 0.0028481483459473, 0.00087666511535645, 0.0014169216156006],
[21, 0.0091345310211182, 0.0017409324645996, 0.0029785633087158],
[31, 0.019546031951904, 0.0023491382598877, 0.0046005249023438],
[41, 0.036402702331543, 0.0032360553741455, 0.006026029586792],
[51, 0.055278301239014, 0.0039372444152832, 0.0078754425048828],
[61, 0.082642316818237, 0.0048537254333496, 0.010209321975708],
[71, 0.11405396461487, 0.0054631233215332, 0.012364625930786],
[81, 0.15123820304871, 0.0062053203582764, 0.014509916305542],
[91, 0.19236493110657, 0.007127046585083, 0.017033576965332],
[101, 0.24052715301514, 0.0080602169036865, 0.01974892616272],
[111, 0.29827189445496, 0.0085773468017578, 0.023083209991455],
[121, 0.35718178749084, 0.0094895362854004, 0.025837421417236],
[131, 0.42515468597412, 0.010404586791992, 0.029412984848022],
[141, 0.49908661842346, 0.011186361312866, 0.033211469650269],
[151, 0.56992983818054, 0.011844635009766, 0.036608695983887],
[161, 0.65314698219299, 0.012562274932861, 0.039996147155762],
[171, 0.74602556228638, 0.013403177261353, 0.04484486579895],
[181, 0.84450364112854, 0.014075994491577, 0.04839038848877],
[191, 0.94431185722351, 0.01488733291626, 0.052026748657227],
[201, 1.0460951328278, 0.015958786010742, 0.056291818618774],
[211, 1.2530679702759, 0.016806602478027, 0.060890197753906],
[221, 1.2901678085327, 0.017560005187988, 0.065101146697998],
[231, 1.4267380237579, 0.018605709075928, 0.070043087005615],
[241, 1.5581474304199, 0.018914222717285, 0.075717210769653],
[251, 1.8255474567413, 0.020106792449951, 0.08226203918457],
[261, 1.8533885478973, 0.020873308181763, 0.085562705993652],
[271, 1.999392747879, 0.021762609481812, 0.15557670593262],
[281, 2.1670596599579, 0.022242784500122, 0.098419427871704],
[291, 2.4296963214874, 0.023237705230713, 0.10490798950195],
[301, 3.0475504398346, 0.031109094619751, 0.13519287109375],
[311, 3.0027780532837, 0.02937388420105, 0.13496232032776],
[321, 2.9123396873474, 0.025942325592041, 0.12607669830322],
[331, 3.0720682144165, 0.026587963104248, 0.13313007354736],
[341, 3.3559355735779, 0.028125047683716, 0.14407730102539],
[351, 3.5787575244904, 0.031508207321167, 0.15093517303467],
[361, 3.6996841430664, 0.028955698013306, 0.15273785591125],
[371, 3.9983749389648, 0.02990198135376, 0.16448092460632],
[381, 4.1213915348053, 0.030521154403687, 0.16835069656372],
[391, 4.3574469089508, 0.031461238861084, 0.17818260192871],
[401, 4.7959914207458, 0.032914161682129, 0.19097280502319],
[411, 4.9738960266113, 0.033754825592041, 0.19744348526001],
[421, 5.3298072814941, 0.035082101821899, 0.2117555141449],
[431, 5.5753719806671, 0.035769462585449, 0.21576929092407],
[441, 5.7648482322693, 0.035907506942749, 0.2213134765625],
[451, 5.9595069885254, 0.036591529846191, 0.2318480014801],
[461, 6.4193341732025, 0.037969827651978, 0.24672293663025],
[471, 6.7780020236969, 0.039541244506836, 0.25563311576843],
[481, 7.0454154014587, 0.039729595184326, 0.26160192489624],
[491, 7.450076341629, 0.040610551834106, 0.27283143997192]

Access Control Problem

I am developing an access control library for my project and I am looking to the best solution to do this:
I am getting all my access list from database to an array. In result it looks like this:
$array = array(
'*' => array('administrator' => TRUE),
'frontend/*' => array(
'user' => TRUE,
'unregistered' => TRUE
),
'backend/*' => array(
'user' => FALSE,
'unregistered' => FALSE
),
'backend/user/*' => array(
'moderator' => FALSE,
'supermoderator' => TRUE,
),
'backend/article/*' => array(
'supermoderator' => TRUE
),
'backend/article/add/new' => array(
'moderator' => TRUE
)
);
The " * " means this user has access all of that related options backend/article/* means that group have access to all article options (article/add, article/remove, ...).
As you see the there is no item in backend/article/add for supermoderator but it has the master access to all article pages.
What is the best way to check this? I tried array_walk() but I guess it wont help me.
Thank you for advices...
I can share my whole code if you want.
* Edit *
Am I storing wrong? If you have the better solution to store it I will be happy to hear it.
Thank you for any advices
No matter what this is going to be a complex algorithm, a simple array_walk wont do. Unless someone is feeling particularly generous and will write one for you, I suggest you hire a programmer.
Am I storing wrong? If you have the better solution to store it I will be happy to hear it.
It totally depends on your algorithm. You can probably write one that uses your current data format. You can also probably write a simpler one if you change your data format. But what your data format should look like then, well, that's a job for a programmer.
I found the answer myself:
lets say the user trying to access backend/article/add/new and this user in the supermoderator group. So I need to look for backend/*, backend/article/*, backend/article/add/*. array_slice() and for() enough for this:
I am using CodeIgniter by the way. I modified it a little bit to seperate frontend and backend controllers. I am not using application/controller directory. I am using application/backend and application/frontend directories for controllers.
So an uri pattern is this: http://site.com/[backend]*/[directory]*/class/method
// This is the page that user trying to reach
$requested_page = "backend/article/add/new";
// pharsing...
$x = explode('/', $requested_page);
// this is needed to cut last 3, 2, 1 items of $x
$i = count($x) > 3 ? -4 : -count($x);
for (; $i < 0; $i++) {
$resource = implode('/', array_slice($x, 0, $i)) . '/*';
// echoing for debug
echo $resource;
}
// Outputs:
// backend/*
// backend/article/*
// backend/article/add/*
function userHasPermissions($permissionsArray, $user, $path) {
// Check exact
if(isset($permissionsArray[$path]) &&
isset($permissionsArray[$path][$user])) {
return $permissionsArray[$path][$user];
}
// Check lower and lower
$partArr = explode('/', $path);
for($i = substr_count($path, '/'); $i >= 0; $i--) {
if($i > 0) {
$choppedPartArr = array_slice($partArr, 0, $i);
$newPath = implode($choppedPartArr, '/') . '/*';
} else {
$newPath = '*';
}
if(isset($permissionsArray[$newPath]) &&
isset($permissionsArray[$newPath][$user])) {
return $permissionsArray[$newPath][$user];
}
}
return false;
}
echo "Result: " . (userHasPermissions($array, 'supermoderator', 'backend/article/add') ? "true" : "false");
Note that 'backend/article' will return false for 'supermoderator' since 'backend/article/*' does not match it. To change this, simply change $i = substr_count($path, '/'); to $i = substr_count($path, '/')+1;.

Categories