I have an array like this:
array(53) { [0]=> string(2) "11" [1]=> string(1) "1" [2]=> string(2) "11" [3]=> string(1) "7" [4]=> string(2) "11" [5]=> string(1) "7" [6]=> string(2) "10" [7]=> string(2) "10" [8]=> string(1) "9" [9]=> string(1) "8" [10]=> string(1) "8" [11]=> string(2) "12" [12]=> string(1) "6" [13]=> string(2) "10" [14]=> string(2) "10" [15]=> string(2) "12" [16]=> string(1) "2" [17]=> string(1) "3" [18]=> string(1) "8" [19]=> string(1) "5" [20]=> string(1) "4" [21]=> string(1) "3" [22]=> string(1) "2" [23]=> string(1) "2" [24]=> string(1) "3" [25]=> string(1) "7" [26]=> string(1) "3" [27]=> string(1) "6" [28]=> string(1) "9" [29]=> string(1) "3" [30]=> string(1) "3" [31]=> string(2) "12" [32]=> string(2) "12" [33]=> string(1) "1" [34]=> string(2) "12" [35]=> string(1) "5" [36]=> string(1) "2" [37]=> string(1) "8" [38]=> string(1) "6" [39]=> string(1) "9" [40]=> string(1) "9" [41]=> string(2) "10" [42]=> string(1) "1" [43]=> string(1) "2" [44]=> string(1) "3" [45]=> string(2) "10" [46]=> string(1) "7" [47]=> string(1) "4" [48]=> string(2) "11" [49]=> string(2) "12" [50]=> string(1) "1" [51]=> string(1) "9" [52]=> string(1) "9" }
I want to every value in this array to have two character length, if array value is:9 to change to 09.
With this I can`t achieve... somewhere I missed something
foreach ($outMonth as $key => $value) {
if (strlen($outMonth[$value]) == 1 ) {
$outMonth[$value] = "0".$value;
//echo (strlen($outMonth[$value])). "<br>";
}
}
If I dump, I got:
array(53) { [0]=> string(2) "11" [1]=> string(2) "01" [2]=> string(2) "11" [3]=> string(2) "03" [4]=> string(2) "11" [5]=> string(2) "05" [6]=> string(2) "10" [7]=> string(2) "10" [8]=> string(2) "08" [9]=> string(2) "09" [10]=> string(3) "010" [11]=> string(2) "12" [12]=> string(3) "012" [13]=> string(2) "10" [14]=> string(2) "10" [15]=> string(2) "12" [16]=> string(1) "2" [17]=> string(1) "3" [18]=> string(1) "8" [19]=> string(1) "5" [20]=> string(1) "4" [21]=> string(1) "3" [22]=> string(1) "2" [23]=> string(1) "2" [24]=> string(1) "3" [25]=> string(1) "7" [26]=> string(1) "3" [27]=> string(1) "6" [28]=> string(1) "9" [29]=> string(1) "3" [30]=> string(1) "3" [31]=> string(2) "12" [32]=> string(2) "12" [33]=> string(1) "1" [34]=> string(2) "12" [35]=> string(1) "5" [36]=> string(1) "2" [37]=> string(1) "8" [38]=> string(1) "6" [39]=> string(1) "9" [40]=> string(1) "9" [41]=> string(2) "10" [42]=> string(1) "1" [43]=> string(1) "2" [44]=> string(1) "3" [45]=> string(2) "10" [46]=> string(1) "7" [47]=> string(1) "4" [48]=> string(2) "11" [49]=> string(2) "12" [50]=> string(1) "1" [51]=> string(1) "9" [52]=> string(1) "9" }
You can see that only few values are changed...
Thanks your time!
You should use the key ($outMonth[$key]) for accessing the array element and change the value
foreach ($outMonth as $key => $value) {
if (strlen($value) === 1 ) {
$outMonth[$key] = "0".$value;
}
}
You are giving wrong index while updating array value:
foreach ($outMonth as $key => $value) {
if (strlen($value) == 1 ) {
$outMonth[$key] = "0".$value; //<-----------Need to add $key as index
//echo (strlen($outMonth[$value])). "<br>";
}
}
What you want is the str_pad function, check out the documentation. It will left or right pad a string with another string to a given length.
foreach ($outMonth as $key => $value) {
$outMonth[$key] = str_pad($value, 2, '0', STR_PAD_LEFT);
}
Related
I have an array and it returns me as follows. I need to add their values, but depending on the account "cuenta", branch "ramo", sub-branch "subramo", specific "especifica" or sub-specific "subespecifica".
array(5) {
[0]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "2"
["especifica"]=>
string(2) "19"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "354.00"
}
[1]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "2"
["especifica"]=>
string(2) "11"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "543.74"
}
[2]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "2"
["especifica"]=>
string(2) "11"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "985.54"
}
[3]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "1"
["especifica"]=>
string(1) "8"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "177.00"
}
[4]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "1"
["especifica"]=>
string(1) "2"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(5) "88.50"
}
}
I need to return the total amount depending on what I just mentioned. that is to say:
array(4) {
[0]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "2"
["especifica"]=>
string(2) "19"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "354.00"
}
[1]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "2"
["especifica"]=>
string(2) "11"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "1529.28"
}
[2]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "1"
["especifica"]=>
string(1) "8"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(6) "177.00"
}
[3]=>
array(6) {
["cuenta"]=>
string(1) "1"
["ramo"]=>
string(1) "1"
["subramo"]=>
string(1) "1"
["especifica"]=>
string(1) "2"
["subespecifica"]=>
string(1) "0"
["costo"]=>
string(5) "88.50"
}
}
You could use array_map:
<?php
$input = array(
array(
'first' => 1,
'second' => 2,
),
array(
'third' => 3
)
);
echo array_sum( array_map( function( $arr ) {
return array_sum( $arr );
}, $input ) ); // Outputs 6
guys i have 4 table uhd_voucher_menu, uhd_menu, uhd_voucher_variant_menu, uhd_variant_menu
which uhd_voucher_menu and uhd_menu is connected by menu_id, than uhd_voucher_variant_menu and uhd_voucher_menu is connected by voucher_menu_id and the last, uhd_variant_menu and voucher_variant_menu is connected by variant_menu_id
this is my code how i get the data :
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,uhd_variant_menu.variant_name,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$res = $this->db->get()->result_array();
from that active recored will be return to this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(4) "Sour"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(14) "Red Crown Crab"
["qty"]=>
string(1) "2"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(8) "Omellete"
["qty"]=>
string(1) "3"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
as you see the golden_salmon with same voucher_menu_id is return twice
i want if the voucher_menu_id is same, so from this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
i want that data return to become this :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
array(2){
[1]=>
string(5) "Spicy"
[2]=>
string(6) "Medium"
}
["voucher_menu_id"]=>
string(1) "3"
}
same as the other golden_salmon with same voucher_menu_id 4
i want the variant_name is become array so the if data with same voucher_menu_id will not call twice
guys can you help me how to execute the data as i want?
thank you (:
You have to use group by, I slightly modified you query:
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,GROUP_CONCAT(uhd_variant_menu.variant_name SEPARATOR ',') AS variants,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$this->db->group_by("uhd_voucher_menu.voucher_id");
$res = $this->db->get()->result_array();
we can convert the comma seperated values to array after query output.
Try and post the result...
i have an array in nested set model https://en.wikipedia.org/wiki/Nested_set_model, And i need convert the array to json parent-children with infinite levels.
Array example:
array(6) {
[0]=>
array(8) {
["title"]=>
string(5) "cat 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "128"
["lft"]=>
string(1) "1"
["rgt"]=>
string(1) "4"
["depth"]=>
string(1) "0"
}
[1]=>
array(8) {
["title"]=>
string(5) "sub 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "129"
["lft"]=>
string(1) "2"
["rgt"]=>
string(1) "3"
["depth"]=>
string(1) "1"
}
[2]=>
array(8) {
["title"]=>
string(5) "cat 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "130"
["lft"]=>
string(1) "5"
["rgt"]=>
string(2) "10"
["depth"]=>
string(1) "0"
}
[3]=>
array(8) {
["title"]=>
string(5) "sub 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "131"
["lft"]=>
string(1) "6"
["rgt"]=>
string(1) "9"
["depth"]=>
string(1) "1"
}
[4]=>
array(8) {
["title"]=>
string(5) "sub 3"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value3"
["id"]=>
string(3) "132"
["lft"]=>
string(1) "7"
["rgt"]=>
string(1) "8"
["depth"]=>
string(1) "2"
}
[5]=>
array(8) {
["title"]=>
string(4) "item"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "133"
["lft"]=>
string(2) "11"
["rgt"]=>
string(2) "12"
["depth"]=>
string(1) "0"
}
}
json example:
[{"name":"Cat 1","value":"value1","children":[{"name":"sub 2", "value":"value2","children":[{"name":"sub 3", "value":"value 3"}]}]}, {"name":"Cat 2","value":"value 2","children":[{"name":"sub 1", "value":"value1"}]},{"name":"item","value":"value1"}]
can someone help me create a function to do the Convert?
json_encode should be the answer here http://php.net/manual/en/function.json-encode.php
Hi I have a array with lets say 3 arrays with another few arrays inside those like this:
array(3) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(1) "1"
["disease_mbr_5"]=>
string(4) "1234"
["disease_mtr_5"]=>
string(2) "12"
["disease_cpe_14"]=>
string(1) "1"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "4"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "4"
["disease_mbr_16"]=>
string(2) "21"
["disease_mtr_16"]=>
string(3) "122"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "1"
["disease_mbr_4"]=>
string(1) "1"
["disease_mtr_4"]=>
string(1) "9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
}
}
[1]=>
array(3) {
["disease_id"]=>
array(8) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(1) "8"
[6]=>
string(2) "15"
[7]=>
string(1) "4"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
}
["params"]=>
object(stdClass)#235 (27) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "1"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "1"
["disease_mbr_16"]=>
string(1) "5"
["disease_mtr_16"]=>
string(1) "6"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
}
}
[2]=>
array(3) {
["disease_id"]=>
array(6) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#184 (24) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
Now I need to merge these arrays in to one and where the values match up in the ["disease_id"] and ["risk_id"] sub array I should delete the duplicate, yet when the keys match up in the ["params"] object I need to keep both values and yet dump the duplicated key in the object, so that this is kind of the result:
array(1) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
[13]=>
string(1) "8"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(7) "1, 1, 2"
["disease_mbr_5"]=>
string(10) "1234, 1, 1"
["disease_mtr_5"]=>
string(8) "12, 1, 1"
["disease_cpe_14"]=>
string(7) "1, 2, 2"
["disease_mbr_14"]=>
string(7) "2, 2, 2"
["disease_mtr_14"]=>
string(7) "2, 2, 2"
["disease_cpe_12"]=>
string(10) "12, 12, 12"
["disease_mbr_12"]=>
string(10) "12, 12, 12"
["disease_mtr_12"]=>
string(10) "12, 12, 12"
["disease_cpe_17"]=>
string(4) "4, 1"
["disease_mbr_17"]=>
string(4) "1, 1"
["disease_mtr_17"]=>
string(4) "1, 1"
["disease_cpe_16"]=>
string(4) "4, 1"
["disease_mbr_16"]=>
string(5) "21, 5"
["disease_mtr_16"]=>
string(3) "122,6"
["disease_cpe_8"]=>
string(6) "11, 11"
["disease_mbr_8"]=>
string(1) "1, 1"
["disease_mtr_8"]=>
string(1) "1, 1"
["disease_cpe_15"]=>
string(13) "132, 132, 132"
["disease_mbr_15"]=>
string(13) "132, 132, 132"
["disease_mtr_15"]=>
string(7) "1, 1, 1"
["disease_cpe_4"]=>
string(7) "1, 7, 7"
["disease_mbr_4"]=>
string(7) "1, 8, 8"
["disease_mtr_4"]=>
string(7) "9, 9, 9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
["risk_cpe_4"]=>
string(1) "1, 1"
["risk_mbr_4"]=>
string(1) "2, 2"
["risk_mtr_4"]=>
string(1) "3, 3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
I have tried many thing but have not found an answer that effectively solves this issue. Here is one of my attempts:
$newArray = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
foreach ($value["disease_id"] as $pointer => $disease){
if (in_array($disease, $value["disease_id"])){
$newArray["disease_id"][] = $selected[$key]["disease_id"][$pointer];
unset($selected[$key]["disease_id"][$pointer]);
$cpe = "disease_cpe_".$disease;
$mbr = "disease_mbr_".$disease;
$mtr = "disease_mtr_".$disease;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
foreach ($value["risk_id"] as $pointer => $risk){
if (in_array($risk, $value["risk_id"])){
$newArray["risk_id"][] = $selected[$key]["risk_id"][$pointer];
unset($selected[$key]["risk_id"][$pointer]);
$cpe = "risk_cpe_".$risk;
$mbr = "risk_mbr_".$risk;
$mtr = "risk_mtr_".$risk;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
}
}
Here is another attempt:
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
}
$newArray["disease_id"] = array_unique($newArray["disease_id"]);
$newArray["risk_id"] = array_unique($newArray["risk_id"]);
}
}
out putt was:
array(3) {
["disease_id"]=>
array(14) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
[21]=>
string(2) "17"
[22]=>
string(2) "16"
[39]=>
string(1) "3"
[40]=>
string(2) "18"
[41]=>
string(1) "9"
[42]=>
string(1) "2"
[43]=>
string(2) "20"
[44]=>
string(1) "1"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["parpams"]=>
object(stdClass)#236 (0) {
}
}
But the ['params'] are still not included.
I have read many treads on stackoverflow, but non actuality address this complexity. If I missed a thread please point me in that direction. Please do remember that these arrays can become as much as twenty arrays. Tanks!
I think this is the answer if it can be of any help to any one else.
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
$newArray["params"] = array();
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
if(is_object($value["params"])){
$newArray["params"] = array_merge_recursive((array) $value["params"], (array) $newArray["params"]);
}
}
$newArray_risk = array_unique($newArray["risk_id"]);
if(sort($newArray_risk)){
$newArray["risk_id"]= array();
foreach ($newArray_risk as $risk_id){
$newArray["risk_id"][] = $risk_id;
}
}
$newArray_disease = array_unique($newArray["disease_id"]);
if(sort($newArray_disease)){
$newArray["disease_id"] = array();
foreach ($newArray_disease as $disease_id){
$newArray["disease_id"][] = $disease_id;
}
}
foreach ($newArray["params"] as $key => $value){
if (is_array($value)){
$i=0;
foreach ($value as $val){
if (!is_array($val)){
if ($i == 0){
$newArray["params"][$key] = $val;
} else {
$newArray["params"][$key] .= ', '.$val;
}
$i++;
}
}
}
}
$newArray["params"] = (object)$newArray["params"];
}
I am starting with the answer on this page PHP/mySQL - how to fetch nested rows into multidimensinal array because I am working with the same kind of relationship between tables. My code now looks like this:
$query=sprintf("SELECT * FROM product INNER JOIN production ON product.ID = production.product_ID");
$data = mysql_query($query);
$products = array();
while ($row = mysql_fetch_assoc($data))
{
if (!isset($products[$row['product_ID']]))
{
$row['production'] = array();
$products[$row['product_ID']] = $row;
}
$products[$row['product_ID']]['production'][] = $row;
}
foreach ($products as $product)
{
foreach ($product['production'] as $item)
//... do stuff
}
The query is solid - I have tried this directly on the database.
My problem is that I can't work out the final bit where it says //... do stuff
No matter what combination of variables etc I stick in there I get ArrayArrayArray (etc). What I want is to get a table of output which I can work with.
I hope this is enough information, this is my first question on StackOverflow. Thank you.
When I do var_dump($item) I get a long display which looks like this:
array(9) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(1) "4" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(3) "130" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "21" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "705" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "23" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "26" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "28" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(2) "12" } array(9) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(1) "5" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "10" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "14" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "140" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "16" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "220" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "17" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-04" ["quantity"]=> string(3) "120" } array(9) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(1) "6" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "11" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "13" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-10-10" ["quantity"]=> string(2) "75" } array(9) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(1) "7" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(2) "12" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" } array(9) { ["ID"]=> string(1) "1" ["product_name"]=> string(11) "Thighs 12kg" ["order"]=> string(3) "105" ["hidden"]=> string(1) "1" ["id"]=> string(1) "8" ["product_ID"]=> string(1) "1" ["date"]=> string(10) "2012-10-01" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(9) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "29" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "30" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "31" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "32" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" }
You are going to construct your table's html inside your last two foreach loops. I would recommend naming your columns better as the text that identifies your array keys in item is confusing (you have three ID fields).
print "<table><thead><tr><th>ID</th><th>Product</th><th>Order</th><th>Hidden</th><th>ID</th><th>Product ID</th><th>date</th><th>Quantity</th></tr></thead><tbody>";
foreach ($products as $product)
{
foreach ($product['production'] as $item)
{
print "<tr>";
print "<td>$item['ID']</td>";
print "<td>$item['product_name']</td>";
print "<td>$item['order']</td>";
print "<td>$item['hidden']</td>";
print "<td>$item['id']</td>";
print "<td>$item['product_id']</td>";
print "<td>$item['date']</td>";
print "<td>$item['quantity']</td>";
print "</tr>";
}
}
print "</tbody></table>";