i have an array
Array
(
[0] => Array
(
[folding_knives__no_assist__possession19] => G
[folding_knives__no_assist__possession___length20] =>
[folding_knives__no_assist___open_cary21] => G
[folding_knives__no_assist__open_carry_length22] =>
[folding_knives__no_assist__concealed23] => G
[folding_knives__no_assisted__concealed_length24] =>
[folding_knives__no_assist__concealed_w__ccw_required25] => R
[folding_knives__no_assist__concealed_w__ccw_required_l26] =>
[folding_knives__no_assist___notes27] =>
)
[1] => Array
(
[folding_knives__assisted_opening__possession28] => G
[folding_knives__assisted_opening__possession_length29] =>
[folding_knives__assisted_opening__open_carry30] => G
[folding_knives__assisted_opening__open_carry_length31] =>
[folding_knives__assisted_opening___concealed32] => G
[folding_knives__assisted_opening__concealed_length33] =>
[folding_knives__assisted_opening___concealed_w__ccw_re34] => R
[folding_knives__assisted_opening__concealed_w__ccw_req35] =>
[folding_knives___assisted_opening___notes36] =>
)
)
I already trying to show my array data into another array but cant
foreach ($chunks as $key => $val)
{
$allknife[] = array(
'name'=>$key[0],
'possession'=>$val[0],
'possession_length'=>$val[1]
);
}
Here name'=>$key[0] here will be arry first item key like folding_knives__no_assist__possession19
It should be link this, and you want field count link 1/2/3/4... then you have to use flag logic like, i=0; and i++
$allknife[] = array();
foreach ($chunks as $key => $val)
{
$allknife['name']=>$val['yourvaluesoeshere'],
$allknife['possession']=>$val['yourvaluesoeshere'],
$allknife['possession_length']=>$val['yourvaluesoeshere']
}
Since your arrays have inconsistent key names you can't rely on this an need consistent names first!
Map the array more like this:
array(
0 => array(
'assist' => true,
'open_carry_nr' => 21,
'open_carry_length' => 22
// and so on...
),
2 => array(
'assist' => false,
'open_carry_nr' => 18,
'open_carry_length' => 1337
// and so on...
)
);
Way better practice.
Related
Is it possible to create a new array from the keys of another like following?
it is a dynamic array chk_values are dynamically changed depends on condition
Array
(
[actBtn] => update
[chkCount] => 5
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3
)
and i want array like this for update database
$chckpoint = Array(
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3)
Simply process the original array and only move to the new array where the key starts with chk_
$in = ['actBtn' => 'update',
'chkCount' => 5,
'chk_1' => 2,
'chk_2' => 3,
'chk_3' => 2,
'chk_4' => 3,
'chk_5' => 3
];
foreach($in as $k=>$v){
if ( strpos($k,'chk_') !== false ){ $chckpoint[$k] = $v; }
}
print_r($chckpoint);
RESULT
Array
(
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3
)
You can simply take the input array and check for all keys beginning with chk_. If the key matches, take it to the new array.
$chckpoint = [];
foreach($input as $key => $value)
{
if(substr($key, 0, 4) == 'chk_') $chkpoint[$key] = $value;
}
I have data like this :
[1] => Array
(
[COMPANY_SERVICE_ID] => CS01
[COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
[TARIFF_CURRENCY] => IDR
[SELLING_SERVICE_ID] => SS01
[CONTAINER_TYPE_ID] => DC
[SERVICE_NAME] => CONTAINER TRUCKING SERVICE
[FROM_QTY] => 1
[TO_QTY] => 100
[FROM_NAME] => TANJUNG PRIOK
[FROM_LOCATION_ID] => L096
[TO_NAME] => BALARAJA
[TO_LOCATION_ID] => L002
[RESULT_LOCATION] => Array
(
[L001] => Array
(
[TARIF_20] => 1.500.000,00
[TARIF_40] => 1.750.000,00
[TARIF_45] => 5.500.000,00
[TARIF_4H] => 3.500.000,00
)
[L002] => Array
(
[TARIF_20] => 500.000,00
)
)
)
i wanna unset value of RESULT_LOCATION if value in RESULT_LOCATION different with value of TO_LOCATION_ID.
What should i do?
i try to remove with code like this :
foreach ($hasil_jakarta as $key => $value) {
foreach ($value['RESULT_LOCATION'] as $key1 => $value1) {
if ($value['TO_LOCATION_ID'] != $value['RESULT_LOCATION'][$key1]) {
unset($hasil_jakarta[$key]['RESULT_LOCATION'][$key1]);
}
}
}
but all value of RESULT_LOCATION deleted. Whats wrong with my code?
You don't have to test the value but the key. Then, unset() arrays with different keys.
Here is an example :
<?php
$a = array(
'COMPANY_SERVICE_ID' => 'CS01',
'COMPANY_NAME' => 'HANOMAN SAKTI PRATAMA, PT - JAKARTA',
'TARIFF_CURRENCY' => 'IDR',
'SELLING_SERVICE_ID' => 'SS01',
'CONTAINER_TYPE_ID' => 'DC',
'SERVICE_NAME' => 'CONTAINER TRUCKING SERVICE',
'FROM_QTY' => 1,
'TO_QTY' => 100,
'FROM_NAME' => 'TANJUNG PRIOK',
'FROM_LOCATION_ID' => 'L096',
'TO_NAME' => 'BALARAJA',
'TO_LOCATION_ID' => 'L002',
'RESULT_LOCATION' => array(
'L001' => array(
'TARIF_20' => '1.500.000,00',
'TARIF_40' => '1.750.000,00',
'TARIF_45' => '5.500.000,00',
'TARIF_4H' => '3.500.000,00'
),
'L002' => array(
'TARIF_20' => '500.000,00'
)
)
);
foreach ($a['RESULT_LOCATION'] as $key => $value) {
if ($key != $a['TO_LOCATION_ID']) {
unset($a['RESULT_LOCATION'][$key]);
}
}
The result will be :
php > print_r($a);
Array
(
[COMPANY_SERVICE_ID] => CS01
[COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
[TARIFF_CURRENCY] => IDR
[SELLING_SERVICE_ID] => SS01
[CONTAINER_TYPE_ID] => DC
[SERVICE_NAME] => CONTAINER TRUCKING SERVICE
[FROM_QTY] => 1
[TO_QTY] => 100
[FROM_NAME] => TANJUNG PRIOK
[FROM_LOCATION_ID] => L096
[TO_NAME] => BALARAJA
[TO_LOCATION_ID] => L002
[RESULT_LOCATION] => Array
(
[L002] => Array
(
[TARIF_20] => 500.000,00
)
)
)
Hope it helps.
EDIT
I saw your snippet and adapt the loop for your needs :
foreach ($hasil_jakarta as $h_j_key => $h_j_value) {
foreach ($h_j_value['RESULT_LOCATION'] as $key => $value) {
if ($key != $h_j_value['TO_LOCATION_ID']) {
unset($hasil_jakarta[$h_j_key]['RESULT_LOCATION'][$key]);
}
}
}
Your code was almost good but the unset line was not. In that case, you need to unset a variable from the $hasil_jakarta array.
Good luck !
I need to merge a new array of alternative information into the loop if they have the alternative information in their profile.
Here's my loop:
foreach ($doctor->getVars() as $k => $v)
{
$data['doctor_'. $k] = $v;
}
foreach ($patient->get_data() as $k=>$v)
{
if (is_string($v) || is_numeric($v))
$data["patient_" . $k] = strtoupper($v);
}
Here's the $data var_dump:
Array
(
[employee] => person
[date] => 05/08/2013
[datetime] => 05/08/2013 9:41:15 AM
[department] => stuff
[employee_ext] => 7457
[employee_email] =>
[barcode] => *NZS01*
[doctor_df_code] => 09HQ
[doctor_npi] => 1111111111
[doctor_dea] => B4574
[doctor_upin] =>
[doctor_license] =>
[doctor_phone] => (111)111-1111
[doctor_fax] => (000)000-0000
[doctor_fname] => UNDEFINED
[doctor_lname] => UNDEFINED
[doctor_title] =>
[doctor_intake_rx_caller_id] =>
[doctor_costco_rx_caller_id] =>
[doctor_reorder_rx_caller_id] =>
[doctor_address1] => 24 CABELL st
[doctor_address2] => SUITE 10
[doctor_city] => places
[doctor_state] => CA
[doctor_zip] => 91111
[doctor_active_events] =>
[doctor_dont_call] => 0
[doctor_dont_fax] => 1
)
I need to merge the below array into the above array.
Here's the print var for the function addr($dfcode):
Array
(
[0] => Array
(
[CODE_] => 09HQ
[doctor_address1] => alternate addy
[doctor_address2] => 45854
[doctor_city] => different city
[doctor_state] => CA
[doctor_zip] => 963545
[doctor_phone] => (619)111-2548
[doctor_fax] => (157)123-4569
)
)
I'm new to array merge and I'm assuming right after the $data['doctor_'. $k] = $v i could list out the new function and the fields i want to merge in particular?
syntax is what i'm not sure on:
$data['doctor_'. $k] . array_merge(addr($dfcode))['doctor_address1'] = $v;
Any help would be greatly appreciated, thank you.
The general formula for merging two arrays is as follows (merging $array_m into $array_o):
foreach($array_m as $key=>$value){
$array_o[$key] = $value;
}
$array_o would now contain all of the elements of $array_m
EDIT: I just noticed in your post that you seem to want to use the array_merge function. You could also do the following:
$array_o = array_merge($array_o, array_m);
i have a (dynamic) array, which in this example contains 4 sets of data (5 fields per set), but it could be only one set or up to 25 sets.
Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
which i'd like to turn into something like
Array ( Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => ),
Array ( [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => ),
Array ( [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => ),
Array ( [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
)
the original array is created this way:
$light = Array();
foreach( $_POST as $key => $val )
{
//field names that start with light to go in this array
if (strpos($key, 'light') === 0) {
$light[$key] = $val;
}
}
the field name digit is already added with JS before form submission, and not by php script.
any hint much appreciated.
This is not an exacty answer to you question, but...
You can use arrays in POST variables like so:
<input name="light[1][wattage]" />
<input name="light[1][voltage]" />
<input name="light[2][wattage]" />
<input name="light[2][voltage]" />
will give you:
$_POST['light'] == array(
1 => array(
'wattage' => '...',
'voltage' => '...',
),
2 => array(
'wattage' => '...',
'voltage' => '...',
),
)
Try this:
$prefixes = array();
$postfixes = array();
foreach($original_array as $key=>$value)
{
preg_match('/^([^\d]*)(\d+)$/',$key,$matches);
if(count($matches)>1)
{
if(!in_array($matches[1], $prefixes)) $prefixes[] = $matches[1];
if(!in_array($matches[2], $postfixes)) $postfixes[] = $matches[2];
}
}
$new_array = array();
foreach($postfixes as $postfix)
{
$new_element = array();
foreach($prefixes as $prefix)
{
if(isset($original_array[$prefix.$postfix])) $new_element[$prefix.$postfix] = $original_array[$prefix.$postfix];
}
$new_array[] = $new_element;
}
given an $original_array equal to described, will produce $new_array:
Array
(
[0] => Array
(
[lightwattage1] => 50
[lightvoltage1] => 12
[lightquantity1] => 2
[lightusage1] => 4
[lightcomment1] =>
)
[1] => Array
(
[lightwattage2] => 60
[lightvoltage2] => 24
[lightquantity2] => 4
[lightusage2] => 5
[lightcomment2] =>
)
[2] => Array
(
[lightwattage3] => 30
[lightvoltage3] => 240
[lightquantity3] => 4
[lightusage3] => 2
[lightcomment3] =>
)
[3] => Array
(
[lightwattage4] => 25
[lightvoltage4] => 12
[lightquantity4] => 2
[lightusage4] => 6
[lightcomment4] =>
)
)
I was uncertain about how much you knew about the elements or their order, so this code basically takes any collection of elements that end in numbers and rearranges them in groups that have the same ending number.
Try this:
$outarr = array()
$subarr = array()
$i=0;
foreach($_POST as $key => $val)
{
//only include keys starting with light:
if (strpos("light", $key)==0)
{
//create a new subarray each time we find a key that starts with "lightwattage":
if ($i>0 && strpos("lightwattage", $key)==0)
{
$outarr[] = $subarr;
}
$subarr[$key] = $val;
$i++;
}
}
$outarr[] = $subarr;
//$outarr contains what you want here
I have this array in php returned from db
Array
(
[inv_templates] => Array
(
[0] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 1
[inven_template_name] => CopperWires6G
[constrained] => 0
[value_constraints] =>
[accept_range] => 2 - 16
[information] => Measured Manual
)
[1] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 2
[inven_template_name] => CopperWires2G
[constrained] => 0
[value_constraints] =>
[accept_range] => 1 - 7
[information] => Measured by Automated Calipers
)
)
)
I need to output this kind of multidimensional stuff
Array
(
[Wires] => Array
(
[inv_group_name] => Wires
[inv_subgroups] => Array
(
[CopperWires] => Array
(
[inv_subgroup_id] => 1
[inv_subgroup_name] => CopperWires
[inv_templates] => Array
(
[CopperWires6G] => Array
(
[inv_name] => CopperWires6G
[inv_id] => 1
)
[CopperWires2G] => Array
(
[inv_name] => CopperWires2G
[inv_id] => 2
)
)
)
)
)
)
I currently do this stuff
foreach ($data['inv_templates'] as $key => $value) {
$processeddata[$value['inven_group']]['inv_group_name'] = $value['inven_group'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_name'] = $value['inven_subgroup'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_name'] = $value['inven_template_name'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
EDIT : A var_export
array (
'inv_templates' =>
array (
0 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '1',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '2 - 16',
'information' => 'Measured Manual',
),
1 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '2',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '1 - 7',
'information' => 'Measured by Automated Calipers',
),
),
)
The foreach is almost unreadable. There must be a simpler way
$processeddata = array();
foreach($data['inv_templates'] as $key => $value) {
$group = $value['inven_group'];
$processeddata[$group]['inv_group_name'] = $group;
$subgroup = &$processeddata[$group]['inv_subgroups'][$value['inven_subgroup']];
$subgroup['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$subgroup['inv_subgroup_name'] = $value['inven_subgroup'];
$template = $value['inven_template_name'];
$subgroup['inv_templates'][$template]['inv_name'] = $template;
$subgroup['inv_templates'][$template]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
Untested code. This structures the array in a multidimensional way, and then uses array_merge_recursive to merge them with the already processed data.
if (!isset($processeddata[$value['inven_group']]))
$processeddata[$value['inven_group']] = array();
$processeddata[$value['inven_group']] = array_merge_recursive(
$processeddata[$value['inven_group']],
array(
'inv_group_name' => $value['inven_group'],
'inv_subgroups' => array(
$value['inven_subgroup'] => array(
'inv_subgroup_id' => $value['inven_subgroup_template_id'],
'inv_subgroup_name' => $value['inven_subgroup'],
'inv_templates' => array(
$value['inven_template_name'] => array(
'inv_name' => $value['inven_template_name'],
'inv_id' => $value['inven_template_id'],
),
),
),
),
)
);
I find this format usually works for me. You could do it more efficient, I've just never cared :D
I started traversing at $yourArray['inv_templates'] though.
function groupToStructure(array $rows, array $keys) {
$tree = array();
$keys = array_reverse($keys);
foreach ($rows as $row) {
$subTree = array($row);
foreach ($keys as $key) {
$subTree = array($row[$key] => $subTree);
}
$tree = array_merge_recursive($tree, $subTree);
}
return $tree;
}
print_r(groupToStructure($rows, array('inven_group', 'inven_subgroup', 'inven_template_name')));