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.
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.
I have a php variable that I got from a _POST. var_dump shows this:
array(9) { [0]=> array(2) { ["age"]=> string(2) "62"
["amount"]=> string(5) "10878" } [1]=> array(2) { ["age"]=> string(2) "63"
["amount"]=> string(5) "10878" } [2]=> array(2) { ["age"]=> string(2) "64"
["amount"]=> string(5) "10878" } [3]=> array(2) { ["age"]=> string(2) "65"
["amount"]=> string(5) "10878" } [4]=> array(2) { ["age"]=> string(2) "66"
["amount"]=> string(5) "10878" } [5]=> array(2) { ["age"]=> string(2) "67"
["amount"]=> string(5) "28416" } [6]=> array(2) { ["age"]=> string(2) "68"
["amount"]=> string(5) "28416" } [7]=> array(2) { ["age"]=> string(2) "69"
["amount"]=> string(5) "28416" } [8]=> array(2) { ["age"]=> string(2) "70"
["amount"]=> string(5) "28416" } }
I loop through the array but can't get the properties to print:
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]->age;
$amt = $incomeSched[$i]->amount;
echo "age=$age, amount=$amt<br>";
}
age and amount are blank:
age=, amount=
There's a difference between associative arrays and objects.
$incomeSched[$i]->age;
is what you'd do to access the property of an object. For an associative array you'd want
$incomeSched[$i]["age"]
You can cast an array as an object if need be:
$obj = (object)$incomeSched;
learn more here:
PHP - associative array as an object
As far as I remember ->age is object syntax. You need array syntax which would be ['age'].
for ($i=0; $i<count($incomeSched); $i++) {
$age = $incomeSched[$i]['age'];
$amt = $incomeSched[$i]['amount'];
echo "age=$age, amount=$amt<br>";
}
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 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'];
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 );