Array ( [horsepower] => 1 [horsepower_rpm] => 1 [torque] => 1 [torque_rpm] => 1 [fuel_capacity] => 1 [vehicle_length] => 1 [vehicle_width] => 1 [vehicle_height] => 1 [curb_weight] => 1 )
Array ( [horsepower] => 170.0 [horsepower_rpm] => 5800 [torque] => 174.0 [torque_rpm] => 4100 [fuel_capacity] => 64.0L [vehicle_length] => 179.5 in [vehicle_width] => 70.1 in [vehicle_height] => 66.9 in [curb_weight] => 3307.0 lbs )
I need to combine these 2 as json and insert into a DB column so it ends up looking like this:{"checks":["horsepower","horsepower_rpm","torque","torque_rpm","fuel_capacity","vehicle_length","vehicle_width","vehicle_height","curb_weight"],"values":{"horsepower":"170.0","horsepower_rpm":"5800","torque":"174.0","torque_rpm":"4100","fuel_capacity":"64.0L","vehicle_length":"179.5 in","vehicle_width":"70.1 in","vehicle_height":"66.9 in","curb_weight":"3307.0 lbs"}}
any idea how I can get there? I've tried a few dozen different combinations but not working out at all.array(2) { ["checks"]=> array(7) { [0]=> string(6) "torque" [1]=> string(10) "torque_rpm" [2]=> string(13) "fuel_capacity" [3]=> string(14) "vehicle_length" [4]=> string(13) "vehicle_width" [5]=> string(14) "vehicle_height" [6]=> string(11) "curb_weight" } ["values"]=> array(7) { ["torque"]=> string(5) "145.0" ["torque_rpm"]=> string(4) "4200" ["fuel_capacity"]=> string(5) "55.0L" ["vehicle_length"]=> string(8) "174.0 in" ["vehicle_width"]=> string(7) "68.5 in" ["vehicle_height"]=> string(7) "57.7 in" ["curb_weight"]=> string(10) "3109.0 lbs" } }
I am looking to remove the value if the checkbox isn't checkedThat should happen automatically; you shouldn't have to do anything special.
array(2) {
["checks"]=> array(7) {
[0]=> string(6) "torque"
[1]=> string(10) "torque_rpm"
[2]=> string(13) "fuel_capacity"
[3]=> string(14) "vehicle_length"
[4]=> string(13) "vehicle_width"
[5]=> string(14) "vehicle_height"
[6]=> string(11) "curb_weight"
}
["values"]=> array(7) {
["torque"]=> string(5) "145.0"
["torque_rpm"]=> string(4) "4200"
["fuel_capacity"]=> string(5) "55.0L"
["vehicle_length"]=> string(8) "174.0 in"
["vehicle_width"]=> string(7) "68.5 in"
["vehicle_height"]=> string(7) "57.7 in"
["curb_weight"]=> string(10) "3109.0 lbs"
}
}
Here's the link to the JSON reference.<?php // demo/temp_tjyoung.php
/**
* https://www.experts-exchange.com/questions/28962021/Saving-2-arrays-into-one-combined-json-value-for-DB-column.html
*/
// ORIGINAL DATA REPRESENTATION?
/*
array(2) {
["checks"]=> array(7) {
[0]=> string(6) "torque"
[1]=> string(10) "torque_rpm"
[2]=> string(13) "fuel_capacity"
[3]=> string(14) "vehicle_length"
[4]=> string(13) "vehicle_width"
[5]=> string(14) "vehicle_height"
[6]=> string(11) "curb_weight"
}
["values"]=> array(7) {
["torque"]=> string(5) "145.0"
["torque_rpm"]=> string(4) "4200"
["fuel_capacity"]=> string(5) "55.0L"
["vehicle_length"]=> string(8) "174.0 in"
["vehicle_width"]=> string(7) "68.5 in"
["vehicle_height"]=> string(7) "57.7 in"
["curb_weight"]=> string(10) "3109.0 lbs"
}
}
*/
// MORE CONCISE DATA REPRESENTATION
$values =
[ "torque" => "145.0"
, "torque_rpm" => "4200"
, "fuel_capacity" => "55.0L"
, "vehicle_length" => "174.0 in"
, "vehicle_width" => "68.5 in"
, "vehicle_height" => "57.7 in"
, "curb_weight" => "3109.0 lbs"
]
;
// RENDERED AS A JSON STRING
$jso = json_encode($values, JSON_PRETTY_PRINT);
// SHOW THE ORIGINAL AND THE WORK PRODUCT
echo '<pre>';
print_r($values);
echo PHP_EOL;
print_r($jso);
Outputs:
Array
(
[torque] => 145.0
[torque_rpm] => 4200
[fuel_capacity] => 55.0L
[vehicle_length] => 174.0 in
[vehicle_width] => 68.5 in
[vehicle_height] => 57.7 in
[curb_weight] => 3109.0 lbs
)
{
"torque": "145.0",
"torque_rpm": "4200",
"fuel_capacity": "55.0L",
"vehicle_length": "174.0 in",
"vehicle_width": "68.5 in",
"vehicle_height": "57.7 in",
"curb_weight": "3109.0 lbs"
}
<?php
$_POST['specs']['checks'] = array (
"horsepower" => 1,
"horsepower_rpm" => 1,
"torque" => 1,
"torque_rpm" => 1,
"fuel_capacity" => 1,
"vehicle_length" => 1,
"vehicle_width" => 1,
"vehicle_height" => 1,
"curb_weight" => 1
);
$_POST['specs']['values'] = array (
"horsepower" => "170.0",
"horsepower_rpm" => "5800",
"torque" => "174.0",
"torque_rpm" => "4100",
"fuel_capacity" => "64.0L",
"vehicle_length" => "179.5 in",
"vehicle_width" => "70.1 in",
"vehicle_height" => "66.9 in",
"curb_weight" => "3307.0 lbs"
);
// EXPECTED
echo '{"checks":["horsepower","horsepower_rpm","torque","torque_rpm","fuel_capacity","vehicle_length","vehicle_width","vehicle_height","curb_weight"],"values":{"horsepower":"170.0","horsepower_rpm":"5800","torque":"174.0","torque_rpm":"4100","fuel_capacity":"64.0L","vehicle_length":"179.5 in","vehicle_width":"70.1 in","vehicle_height":"66.9 in","curb_weight":"3307.0 lbs"}}';
echo "<br/>";
// CORRECTED KIM SOLUTION
echo json_encode(array('checks'=>array_keys($_POST['specs']['checks']),'values'=>$_POST['specs']['values']) );
$result = array (
'checks' => array(),
'values' => array()
);
foreach($_POST['specs']['checks'] as $k => $v) {
$result['checks'][] = $k;
$result['values'][$k] = isset($_POST['specs']['values'][$k]) ? $_POST['specs']['values'][$k] : '';
}
// SOLUTION THAT ONLY INCLUDES SELECTED
echo "<br/>";
echo json_encode($result);
{"checks":["horsepower","horsepower_rpm","torque","torque_rpm","fuel_capacity","vehicle_length","vehicle_width","vehicle_height","curb_weight"],"values":{"horsepower":"170.0","horsepower_rpm":"5800","torque":"174.0","torque_rpm":"4100","fuel_capacity":"64.0L","vehicle_length":"179.5 in","vehicle_width":"70.1 in","vehicle_height":"66.9 in","curb_weight":"3307.0 lbs"}}
{"checks":["horsepower","horsepower_rpm","torque","torque_rpm","fuel_capacity","vehicle_length","vehicle_width","vehicle_height","curb_weight"],"values":{"horsepower":"170.0","horsepower_rpm":"5800","torque":"174.0","torque_rpm":"4100","fuel_capacity":"64.0L","vehicle_length":"179.5 in","vehicle_width":"70.1 in","vehicle_height":"66.9 in","curb_weight":"3307.0 lbs"}}
{"checks":["horsepower","horsepower_rpm","torque","torque_rpm","fuel_capacity","vehicle_length","vehicle_width","vehicle_height","curb_weight"],"values":{"horsepower":"170.0","horsepower_rpm":"5800","torque":"174.0","torque_rpm":"4100","fuel_capacity":"64.0L","vehicle_length":"179.5 in","vehicle_width":"70.1 in","vehicle_height":"66.9 in","curb_weight":"3307.0 lbs"}}
Ray's suggestion above requires you duplicate the checkbox controls with a hidden control with the same name defaulting to 0 (I am assuming - I did not go too much into it). This will also work but it does mean you have to maintain a shadow set of controls - if you add a checkbox and then forget to add the shadow then you will not get the expected results.horsepower of 170.5 <input type="checkbox" name="ckSpecs[]" value="horsepower-170.5"><br/>
horsepower rpm of 5825 <input type="checkbox" name="ckSpecs[]" value="horsepower_rpm-5825"><br/>
torque of 175.0 <input type="checkbox" name="ckSpecs[]" value="torque-175.0"><br/>
torque rpm of 4100 <input type="checkbox" name="ckSpecs[]" value="torque_rpm-4100"><br/>
if(!empty($_POST['ckSpecs'])) {
$specs = array();
foreach($_POST['ckSpecs'] as $val){
$keyVal = explode("-", $val);
$specs[$keyVal[0] => $keyVal[1] ];
}
// and now in the $specs ARRAY you have ALL of the info you need as key value pairs
}
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html