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'];
Related
I have this array, it could look something like this:
array(756) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[2]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[3]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[4]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[5]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
[6]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[7]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[8]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[9]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[10]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[11]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
etc...};
How would i go about looping thru and splitting it up into arrays based on the value in the inner arrays[0] ex: "joint_temps5".
I have tested quite a few things but without success. My problem mainly is i dont know what might be in the string in the arrays.
I would like to end up with arrays like:
$array1[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
$array2[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
etc.
I would recommend to create a new array from your input array, using the value as an index of the array to be created, like so:
// test-set: input array is $a
$a[0] = array("joint_temps5","23.5");
$a[1] = array("joint_temps3","24");
$a[2] = array("joint_temps2","24.5");
$a[3] = array("joint_temps1","25");
$a[4] = array("joint_temps0","25.5");
$a[5] = array("joint_temps5","23.5");
$a[6] = array("joint_temps4","23.5");
$a[7] = array("joint_temps3","24");
$a[8] = array("joint_temps2","24.5");
$a[9] = array("joint_temps1","25");
foreach($a as $key => $value){
$b[$value[0]][] = $value; // *Explained below
}
*"Explained below": $a is the source array, $b is the newly created array.
$b[$value[0]][] means it wil create a new element for array $b[$value[0]]. And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits.
Example: the first element of $a is this array: array("joint_temps5","23.5"). So in the foreach loop, the text "joint_temps5" ($value[0] in the foreach) will be used as a key/index to create a new element for array $b. The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added.
It will result in:
array(6) {
["joint_temps5"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
["joint_temps3"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
}
["joint_temps2"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
}
["joint_temps1"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
}
["joint_temps0"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
}
["joint_temps4"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
You could loop through your array, and populate a new array using the string as a key, so something like:
foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }
Which would give you an array something like :
$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}
If you needed to you could then parse that into an array in the format you desire quite easily.
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!
so, I have this database, I need to know how many profile.stateX-profile.licenseX have value and also to pair them. I am able to pull them in a array but I can't find the propper php code to get what I need.
Again, what I need to know is this:
1: how many profile.state1/2/3/4/5-profile.license1/2/3/4/5 pairs I have with value
2:output the pairs
This is the array returned form query:
<pre>
array(16) {
[0]=>
array(2) {
[0]=>
string(15) "profile.address"
[1]=>
string(10) "Bld. Indep"
}
[1]=>
array(2) {
[0]=>
string(21) "profile.certification"
[1]=>
string(7) "cert112"
}
[2]=>
array(2) {
[0]=>
string(16) "profile.license1"
[1]=>
string(5) "12345"
}
[3]=>
array(2) {
[0]=>
string(16) "profile.license2"
[1]=>
string(0) ""
}
[4]=>
array(2) {
[0]=>
string(16) "profile.license3"
[1]=>
string(0) ""
}
[5]=>
array(2) {
[0]=>
string(16) "profile.license4"
[1]=>
string(0) ""
}
[6]=>
array(2) {
[0]=>
string(16) "profile.license5"
[1]=>
string(0) ""
}
[7]=>
array(2) {
[0]=>
string(21) "profile.licensenumber"
[1]=>
string(7) "lice112"
}
[8]=>
array(2) {
[0]=>
string(14) "profile.school"
[1]=>
string(5) "nr, 2"
}
[9]=>
array(2) {
[0]=>
string(14) "profile.state1"
[1]=>
string(4) "Ohio"
}
[10]=>
array(2) {
[0]=>
string(14) "profile.state2"
[1]=>
string(0) ""
}
[11]=>
array(2) {
[0]=>
string(14) "profile.state3"
[1]=>
string(0) ""
}
[12]=>
array(2) {
[0]=>
string(14) "profile.state4"
[1]=>
string(0) ""
}
[13]=>
array(2) {
[0]=>
string(14) "profile.state5"
[1]=>
string(0) ""
}
[14]=>
array(2) {
[0]=>
string(18) "profile.user_state"
[1]=>
string(8) "Roumania"
}
[15]=>
array(2) {
[0]=>
string(11) "profile.zip"
[1]=>
string(3) "123"
}
}
</pre>
I hope this makes sense.
Assuming the database is MySQL:
SELECT t1.profile_key, t1.profile_value, t2.profile_key, t2.profile_value
FROM profile AS t1
JOIN profile AS t2 ON t2.profile_key = CONCAT('profile.license', SUBSTR(t1.profile_key, 14))
WHERE t1.profile_key LIKE 'profile.state%' AND t1.profile_value != ''
AND t2.profile_key LIKE 'profile.license%' AND t2.profile_value != ''
SUBSTR(t1.profile_key, 14) gets the number after profile.state in profile_key column. Then we use CONCAT() to append that to profile.license to get the profile_key for the paired row.
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
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.