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 );
Related
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
}
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.
How can I fetch the value "3" from this set of arrays:
array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "0" }
The arrays are output from a foreach statement of parenting array, which is:
array(3) { [0]=> string(8) "St" [1]=> string(1) "1" [2]=> string(1) "0" }
array(3) { [0]=> string(16) "Fu" [1]=> string(1) "3" [2]=> string(1) "0" }
array(3) { [0]=> string(13) "Pa" [1]=> string(1) "0" [2]=> string(1) "0" }
Where I am going for the second line value: "Fu" [1]=> string(1) "3"
Maybe I am doing it wrong from the first array?
You're not giving us much to go on. Are the 3 arrays already in a parent array, in an object, etc.? Below is how to get the # 3 from the 3 arrays...but I'm guessing this is not actually what you are asking, we likely need much more detail...the real problem you are trying to solve.
function getThree($arr1, $arr2, $arr3) {
$array = array();
$array[] = $arr1;
$array[] = $arr2;
$array[] = $arr3;
foreach( $array AS $subArray ) {
// whichever condition works for you
if( $subArray[0] == 'Fu' || $subArray[1] == 3 ) {
return $subArray;
}
}
}
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.
Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.