Extract count of multidimensional array - php

i have a multidimensional array from a ldap search stored in $data:
Array
(
[count] => 1
[0] => Array
(
[objectclass] => Array
(
[count] => 3
[0] => top
[1] => posixGroup
[2] => sambaGroupMapping
)
[0] => objectclass
[cn] => Array
(
[count] => 1
[0] => xxxxx
)
[1] => cn
[gidnumber] => Array
(
[count] => 1
[0] => xxxx
)
[2] => gidnumber
[sambasid] => Array
(
[count] => 1
[0] => xxxx
)
[3] => sambasid
[sambagrouptype] => Array
(
[count] => 1
[0] => 2
)
[4] => sambagrouptype
[displayname] => Array
(
[count] => 1
[0] => xxxx
)
[5] => displayname
[memberuid] => Array
(
[count] => 2
[0] => xxxxx
[1] => xxxxx
)
[6] => memberuid
[count] => 7
[dn] => cn=xx,ou=xx,dc=xx,dc=xx
)
)
I would like to extract the count of the memberuid: [count] => 2 but i'm not able to do it correctly. I can't figoure out the structure..
I tried: $data[0]['displayname']['memberuid']['count'] which gives me an Illegal string offset
Thanks for your help

Related

Grouping php arrays and showing highest and lowest value in the array

I have an array that looks like this:
Array
(
[0] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 1
)
[1] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 3
)
[3] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 4
)
[4] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 5
)
[5] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 6
)
[6] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 7
)
[7] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 8
)
[8] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 9
)
[9] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 10
)
[10] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 11
)
[11] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 12
)
[12] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 20
)
[13] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 21
)
[14] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 22
)
[15] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 23
)
[16] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 24
)
[17] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 25
)
[18] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 26
)
[19] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 27
)
[20] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 33
)
[21] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 30
)
[22] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 35
)
[23] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 46
)
[24] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 47
)
[25] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 48
)
[26] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 50
)
[27] => Array
(
[0] => 19.0001.2
[1] => 3
[2] => 1
)
[28] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 40
)
[29] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 41
)
[30] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 42
)
[31] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 7
)
[32] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 8
)
[33] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 9
)
[34] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 13
)
[35] => Array
(
[0] => 19.0001.3
[1] => 4
[2] => 41
)
[36] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 2
)
[37] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 3
)
[38] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 4
)
[39] => Array
(
[0] => 19.0001.8
[1] => 4
[2] => 40
)
[40] => Array
(
[0] => 19.0009.8
[1] => 3
[2] => 8
)
)
I have a function that groups the last value in an array into a range of numbers, however it doesn't work with the above array. I have tried a number of different arrays with no success. The above array is generated by a mysql query result.
$getlocations = $db->query("
SELECT barcode
, test_num
, magnum
, sect
, slidenum
FROM fas_log
WHERE barcode LIKE '%$bcinv'
AND notes = '$notes'
AND removed = '$removed'
AND staffid= '$sid'
ORDER BY magnum, sect, slidenum
");
while ($getlocationsrow = $getlocations->fetch()) {
extract($getlocationsrow);
$magrange[]=array("$magnum", "$sect", "$slidenum");
}
function rangeslides($arr) {
$previous = null;
$result = array();
$consecutiveArray = array();
// Slice array by consecutive sequences
foreach($arr as $number) {
if ($number == $previous + 1) {
$consecutiveArray[] = $number;
} else {
$result[] = $consecutiveArray;
$consecutiveArray = array($number);
}
$previous = $number;
}
$result[] = $consecutiveArray;
// Get length of each sub array
$count = array_map('count', $result);
$first = reset($count);
$last = end($count);
prettyarray($result);
}
I want to extract data from the array to look like this showing the first and last numbers in the range:
19.0001.2 1 - 12
The above array should produce 15 different results with 9 of them showing no range of numbers because they are singular.
ie: 19.0001.8 4 40

How do I use array_map recursively in PHP?

I am attempting to convert an associative array to a 2D array to allow me to export it to Google Sheets. I've figured out a simplistic solution that works as follows:
$headers = $data["resultSets"][0]["headers"];
$rowSet0 = $data["resultSets"][0]["rowSet"][0];
$rowSet1 = $data["resultSets"][0]["rowSet"][1];
$hackresults = array_map(null, $headers, $rowSet0, $rowSet1);
This produces the following:
(
[0] => Array
(
[0] => SEASON_ID
[1] => 22017
[2] => 22017
)
[1] => Array
(
[0] => Player_ID
[1] => 203954
[2] => 203954
)
[2] => Array
(
[0] => Game_ID
[1] => 0021701118
[2] => 0021701105
)
[3] => Array
(
[0] => GAME_DATE
[1] => MAR 28, 2018
[2] => MAR 26, 2018
)
[4] => Array
(
[0] => MATCHUP
[1] => PHI vs. NYK
[2] => PHI vs. DEN
)
[5] => Array
(
[0] => WL
[1] => W
[2] => W
)
[6] => Array
(
[0] => MIN
[1] => 9
[2] => 27
)
[7] => Array
(
[0] => FGM
[1] => 2
[2] => 6
)
[8] => Array
(
[0] => FGA
[1] => 6
[2] => 12
)
[9] => Array
(
[0] => FG_PCT
[1] => 0.333
[2] => 0.5
)
[10] => Array
(
[0] => FG3M
[1] => 0
[2] => 0
)
[11] => Array
(
[0] => FG3A
[1] => 1
[2] => 1
)
[12] => Array
(
[0] => FG3_PCT
[1] => 0
[2] => 0
)
[13] => Array
(
[0] => FTM
[1] => 1
[2] => 8
)
[14] => Array
(
[0] => FTA
[1] => 2
[2] => 10
)
[15] => Array
(
[0] => FT_PCT
[1] => 0.5
[2] => 0.8
)
[16] => Array
(
[0] => OREB
[1] => 2
[2] => 1
)
[17] => Array
(
[0] => DREB
[1] => 1
[2] => 12
)
[18] => Array
(
[0] => REB
[1] => 3
[2] => 13
)
[19] => Array
(
[0] => AST
[1] => 0
[2] => 2
)
[20] => Array
(
[0] => STL
[1] => 0
[2] => 1
)
[21] => Array
(
[0] => BLK
[1] => 0
[2] => 2
)
[22] => Array
(
[0] => TOV
[1] => 1
[2] => 4
)
[23] => Array
(
[0] => PF
[1] => 1
[2] => 5
)
[24] => Array
(
[0] => PTS
[1] => 5
[2] => 20
)
[25] => Array
(
[0] => PLUS_MINUS
[1] => 7
[2] => 20
)
[26] => Array
(
[0] => VIDEO_AVAILABLE
[1] => 1
[2] => 1
)
)
This is the output I'm looking for, but there are 27 "rowSet"s, and it seems there must be a recursive way of performing this task.
I've looked at a number of custom array_map_recursive style functions but haven't had any success. Apologies and thanks in advance, I am a terrible novice coder!
You can use argument unpacking.
With the ... operator, you can use all the elements under $data["resultSets"][0]["rowSet"] as additional arguments to array_map.
$headers = $data["resultSets"][0]["headers"];
$rowSets = $data["resultSets"][0]["rowSet"];
$results = array_map(null, $headers, ...$rowSets);
(This isn't recursion, but I think it does what you're trying to do.)

looping through multidimensional array from post and save to mysql

This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise

Rebuild php array structure

I've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);

Is this the correct syntax for 'array_unique()' in PHP?

I want to get rid of duplicates in my array but I'm still printing duplicates with this:
$getuser = ltrim($top10['url'], " users/" ); //trim url to get user id
$array[$i++] = $getuser;
$dirty = $array;
$clean = array_unique($dirty);
print_r($clean)."<br />";
Input print_r($array)
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Output print_r($clean);
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Yes, it seems to be correct. Basically you specify an array to array_unique and it gives you unique items out of the array.
$unique_array = array_unique($your_array);

Categories