Group array of arrays php - 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.

Related

grouping array values like mysqli 'like' functionality

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!

how to return multidimensional array in codeigniter

guys i have multidimensional array which i got it from var_dump of $menu_order with this following array :
array(5) {
[0]=>
array(1) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "small"
}
}
[2]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Salty"
}
[1]=>
array(1) {
["variant_name"]=>
string(6) "medium"
}
}
[3]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(12) "Mix of Herbs"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "large"
}
}
[4]=>
array(0) {
}
}
from that array, i need to get the variant_name become variant_menu_id with this following code :
foreach ($menu_order as $item) {
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant_id[] = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
}
} else {
$variant_id[] = array();
}
}
the model of m_get_choice_id have this following code :
Function m_get_choice_id($variant_name){
$this->db->select("variant_menu_id");
$this->db->from("uhd_variant_menu");
$this->db->where("variant_name",$variant_name);
$query = $this->db->get();
return $query->row_array();
}
the variant_id will be return to this multidimensional array :
array(8) {
[0]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[1]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[2]=>
array(1) {
["variant_menu_id"]=>
string(1) "6"
}
[3]=>
array(1) {
["variant_menu_id"]=>
string(1) "4"
}
[4]=>
array(1) {
["variant_menu_id"]=>
string(1) "7"
}
[5]=>
array(1) {
["variant_menu_id"]=>
string(1) "5"
}
[6]=>
array(1) {
["variant_menu_id"]=>
string(1) "8"
}
[7]=>
array(0) {
}
}
but i want the result variant_id become this multidimensional array :
array(5) {
[0]=>
array(1) {
[0]=>
string(1) "3"
}
[1]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "6"
}
[2]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "7"
}
[3]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "8"
}
[4]=>
array(0) {
}
}
guys can you help me how to get the multidimensional array?
thank you (:
Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:
$result = array();
foreach ($menu_order as $item) {
$temp = array(); // initialize temporary storage
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
$temp[] = $variant['variant_menu_id']; // push single id into temporary storage
}
}
$result[] = $temp; // push ending batch
}

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.

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'];

How do I sort this array

I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );

Categories