grouping array values like mysqli 'like' functionality - php

friends, i don't it's possible or not, anyway i want to group users who have same like preference names ( mysql 'like' functionality)
[0]=>
array(5) {
["id"]=>"21"
["user_id"]=>"58"
["preference_id"]=>"4"
["sub_preference_id"]=>"7"
["preference_name"]=>"stephens collage"
}
[1]=>
array(5) {
["id"]=>"22"
["user_id"]=>"52"
["preference_id"]=>"4"
["sub_preference_id"]=>"1"
["preference_name"]=>"st stephens"
}
[2]=>
array(5) {
["id"]=>"25"
["user_id"]=>"61"
["preference_id"]=>"4"
["sub_preference_id"]=>"9"
["preference_name"]=>"joseph"
}
means i want output as like this :
1]stephens collage, st stephens
2] joseph
i tried this code :
$school_preference ="SELECT * FROM gic_user_preference where preference_id='4'";
$result_school_preference = mysqli_query($createCon->connect(), $school_preference);
$school = array();
while ($show_school_preference = mysqli_fetch_assoc($result_school_preference)) {
$school_preference = $show_school_preference['sub_preference_id'];
$get_all_school_preference =mysqli_query($createCon->connect(),"SELECT gic_user_preference.id,gic_user_preference.user_id,gic_user_preference.preference_id,gic_user_preference.sub_preference_id,gic_user_wise_school_preference.preference_name FROM gic_user_preference INNER JOIN gic_user_wise_school_preference ON gic_user_preference.sub_preference_id = gic_user_wise_school_preference.id where gic_user_preference.sub_preference_id='$school_preference'");
while ($schools = mysqli_fetch_assoc($get_all_school_preference)) {
$school[$schools['preference_name']][] = $schools;
}
}
it's give the output
array(4) {
["stephens collage"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "21"
["user_id"]=>
string(2) "58"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "7"
["preference_name"]=>
string(16) "stephens collage"
}
}
["st stephens"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "22"
["user_id"]=>
string(2) "52"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "1"
["preference_name"]=>
string(11) "st stephens"
}
}
["joseph"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "25"
["user_id"]=>
string(2) "61"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "9"
["preference_name"]=>
string(6) "joseph"
}
}
}
any idea how to grouping this data as mysqli like function works ...

I think you can use regular expression match for find same names!

Related

Group array of arrays php

I have a query that produces this kind of result when converting to JSON.
The result is from this query:
$query = "SELECT template_groups.id,template_groups.template_id , templates.color_ref
FROM `template_groups`,`templates`
where templates.template_id = template_groups.template_id ";
Result:
array(135) {
[0]=>
array(2) {
["template_groups"]=>
array(2) {
["id"]=>
string(1) "1"
["template_id"]=>
string(1) "1"
}
["templates"]=>
array(1) {
["color_ref"]=>
string(6) "F1F1F1"
}
}
[1]=>
array(2) {
["template_groups"]=>
array(2) {
["id"]=>
string(1) "1"
["template_id"]=>
string(1) "2"
}
["templates"]=>
array(1) {
["color_ref"]=>
string(6) "00326E"
}
}
[2]=>
array(2) {
["template_groups"]=>
array(2) {
["id"]=>
string(1) "1"
["template_id"]=>
string(1) "3"
}
["templates"]=>
array(1) {
["color_ref"]=>
string(6) "191919"
}
}
}
I would like to group the result so that for each template_groups, I have inside it all the templates. What could be the best solution in PHP?
The wanted result is an array of template_groups, each one has the corresponding array of template_id and its color.

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

Merging 3 key=>value arrays

i have these 3 arrays
Global array
array(5) { [0]=> array(1) { ["shout_id"]=> string(1) "4" }
[1]=> array(1) { ["shout_id"]=> string(1) "6" }
[2]=> array(1) { ["shout_id"]=> string(2) "16" }
[3]=> array(1) { ["shout_id"]=> string(2) "17" }
[4]=> array(1) { ["shout_id"]=> string(2) "20" } }
Local array
array(1) { [0]=> array(1) { ["shout_id"]=> string(2) "13" } }
Country array
array(1) { [0]=> array(1) { ["shout_id"]=> string(2) "19" } }
and when i merge all 3 i get this
Result array
array(5) { [0]=> array(1) { ["shout_id"]=> string(2) "19" }
[1]=> array(1) { ["shout_id"]=> string(1) "6" }
[2]=> array(1) { ["shout_id"]=> string(2) "16" }
[3]=> array(1) { ["shout_id"]=> string(2) "17" }
[4]=> array(1) { ["shout_id"]=> string(2) "20" } }
However this is what i want
array(7) { [0]=> array(1) { ["shout_id"]=> string(2) "19" }
[1]=> array(1) { ["shout_id"]=> string(1) "6" }
[2]=> array(1) { ["shout_id"]=> string(2) "16" }
[3]=> array(1) { ["shout_id"]=> string(2) "17" }
[4]=> array(1) { ["shout_id"]=> string(2) "20" }
[5]=> array(1) { ["shout_id"]=> string(2) "4" }
[6]=> array(1) { ["shout_id"]=> string(2) "13" } }
For some reason it is missing out the values 4 and 13 and i can't work out why.
Here is the code for combining the arrays
$result_array = $country_array + $global_array + $local_array;
Use array_merge, it concatenates arrays with numeric keys.
$result_array = array_merge($country_array, $global_array, $local_array);
+ replaces elements with the same key.
Try
$result_array = array_merge($country_array, $global_array, $local_array);
What you're doing is called the 'union' operator in PHP.
It merges the arrays based in their keys (see: http://us3.php.net/manual/en/language.operators.array.php for more info).
And because you have numeric keys (for example three times the key 0) they will be overwritten.

Use PHP if() to match item in array

I am working on an admin panel in which the code will display different if the PDO query returns all the admins from the admin table and the userid matches 1 of the admin id results. Only there can be an unlimited amount of admins and I don't want to be editing the code for each of them.
Would it be possible to do this (pseudo code):
if($userid isfoundin $result['admin_user_id']{
-- admin code here
}
I haven't yet written the admin table as I want more info on it first but if I cycle through the user table and look for id $query->fetchAll() then I get this:
array(30) { [0]=> array(2) { ["id"]=> string(1) "7" [0]=> string(1) "7" } [1]=> array(2) { ["id"]=> string(1) "6" [0]=> string(1) "6" } [2]=> array(2) { ["id"]=> string(1) "8" [0]=> string(1) "8" } [3]=> array(2) { ["id"]=> string(2) "31" [0]=> string(2) "31" } [4]=> array(2) { ["id"]=> string(2) "26" [0]=> string(2) "26" } [5]=> array(2) { ["id"]=> string(1) "4" [0]=> string(1) "4" } [6]=> array(2) { ["id"]=> string(2) "35" [0]=> string(2) "35" } [7]=> array(2) { ["id"]=> string(2) "21" [0]=> string(2) "21" } [8]=> array(2) { ["id"]=> string(2) "38" [0]=> string(2) "38" } [9]=> array(2) { ["id"]=> string(2) "24" [0]=> string(2) "24" } [10]=> array(2) { ["id"]=> string(2) "34" [0]=> string(2) "34" } [11]=> array(2) { ["id"]=> string(2) "20" [0]=> string(2) "20" } [12]=> array(2) { ["id"]=> string(2) "19" [0]=> string(2) "19" } [13]=> array(2) { ["id"]=> string(2) "23" [0]=> string(2) "23" } [14]=> array(2) { ["id"]=> string(2) "33" [0]=> string(2) "33" } [15]=> array(2) { ["id"]=> string(2) "28" [0]=> string(2) "28" } [16]=> array(2) { ["id"]=> string(1) "3" [0]=> string(1) "3" } [17]=> array(2) { ["id"]=> string(2) "15" [0]=> string(2) "15" } [18]=> array(2) { ["id"]=> string(1) "9" [0]=> string(1) "9" } [19]=> array(2) { ["id"]=> string(2) "25" [0]=> string(2) "25" } [20]=> array(2) { ["id"]=> string(1) "1" [0]=> string(1) "1" } [21]=> array(2) { ["id"]=> string(2) "32" [0]=> string(2) "32" } [22]=> array(2) { ["id"]=> string(1) "5" [0]=> string(1) "5" } [23]=> array(2) { ["id"]=> string(2) "18" [0]=> string(2) "18" } [24]=> array(2) { ["id"]=> string(2) "29" [0]=> string(2) "29" } [25]=> array(2) { ["id"]=> string(2) "27" [0]=> string(2) "27" } [26]=> array(2) { ["id"]=> string(2) "30" [0]=> string(2) "30" } [27]=> array(2) { ["id"]=> string(2) "22" [0]=> string(2) "22" } [28]=> array(2) { ["id"]=> string(2) "10" [0]=> string(2) "10" } [29]=> array(2) { ["id"]=> string(2) "36" [0]=> string(2) "36" } }
this si what I am trying to use right now
require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
$query = $pdo->prepare("SELECT id FROM users");
$query->execute();
var_dump($query->fetchAll());
if (in_array($user['id'], $query->fetchAll())){
echo $user['id'];
}
Yes, you can use in_array() for this.
I assume that you have a set of users for admin. Say, it is:
$adminUsers = array("admin", "administrator");
# Or by your code
$adminUsers = $result['admin_user_id'];
Now the code part is like:
if (in_array($userid, $adminUsers))
// Admin code here
I think you're looking for in_array() to see if a given item is in an array.
You should use in_array() if all you need to know is that the user ID exists in your array.
But if you actually need a reference to the item, take a look at array_search() http://au1.php.net/manual/en/function.array-search.php
Instead of going through all the users in your admins table and then having another loop in order to find if a specific user exists , why not doing this check in the query itself?
For instance:
$stmt = $mysqli->prepare("SELECT name,email FROM admins WHERE id=?");
$stmt->bind_param("d", $user_id);
$stmt->execute();
$stmt->bind_result($name, $email);
if ($stmt->fetch()) {
//The user with user_id exists in the admins table , show him the admin panel.
//You can use $name and $email.
}
$stmt->close();

Calculate frequency of values in the array

I need to calculate the frequency of all values in the array $inputArr in order to build frequency histogram.
$query="SELECT delay FROM TestDB;";
$result=DatabaseConnector::ExecuteQueryArray($query);
$inputArr = array();
foreach ($result as $row) {
$inputArr[] = array($row['delay']);
}
$freq = array_count_values ($inputArr);
But var_dump($freq) returns array(0). It's strange, because var_dump($inputArr) returns the following result (just a sample):
array(429) {
[0]=> array(1) { [0]=> string(1) "0" }
[1]=> array(1) { [0]=> string(1) "0" }
[2]=> array(1) { [0]=> string(1) "0" }
[3]=> array(1) { [0]=> string(1) "9" }
[4]=> array(1) { [0]=> string(2) "12" }
[5]=> array(1) { [0]=> string(1) "7" }
[6]=> array(1) { [0]=> string(2) "15" }
[7]=> array(1) { [0]=> string(1) "3" }
[8]=> array(1) { [0]=> string(2) "13" }
[9]=> array(1) { [0]=> string(1) "0" }
[10]=> array(1) { [0]=> string(1) "1" }
[11]=> array(1) { [0]=> string(2) "35" }
[12]=> array(1) { [0]=> string(2) "24" }
[13]=> array(1) { [0]=> string(2) "14" }
[14]=> array(1) { [0]=> string(1) "4" }
[15]=> array(1) { [0]=> string(1) "0" }
[16]=> array(1) { [0]=> string(2) "26" }
[17]=> array(1) { [0]=> string(1) "0" }
As it can be seen from this output, inputArr has repeating values, e.g. 0.
So, why $freq = array(0)?
I would consider skipping the PHP part and changing your SQL statement to do all the work for you:
SELECT delay, count(*) AS freq FROM TestDB GROUP BY delay;
$inputArr[] = array($row['delay']);
You are making each element in $inputArr an array. You don't need a 2d array here, just do:
$inputArr[] = $row['delay'];

Categories