doulin6761 2017-10-16 12:37
浏览 21
已采纳

创建PHP数组匹配键有一些价值

This is the first post from a PHP noob. My question relates to an HTML/PHP form which posts the following three types of data generated by a MySQL query:

ID (always posted via a hidden input field) Mileage (only some entries may be completed, others might be left blank) Vehicle Type (always posted via a hidden input field)

This data is all for one of three companies that are selected on the page preceding the data-posting page ie.

Step One: Choose company and year. Press submit.

Step Two: See results from MySQL query and enter in mileages where necessary. Press submit.

Step Three: (that's why I'm here)

I've stuffed this data into what I (probably mistakenly) believe is a multidimensional array called $idmilearray by using the following code:

if(isset($_POST['mileage'])) {
    $mileagenumber = $_POST['mileage'];
}

if(isset($_POST['idnos'])) {
    $idnumber = $_POST['idnos'];
}

if(isset($_POST['vehicle'])) { 
    $vehicletype = $_POST['vehicle'];
}

$idmilearray = array(
'ids' => $idnumber,
'mileage' => $mileagenumber,
'vtype' => $vehicletype
);


foreach($idmilearray as $inputs) {

$inputs = $idmilearray['ids'];
$inputs = $idmilearray['mileage'];
$inputs = $idmilearray['vtype'];

}

If I execute a print_r on the $idmilearray I get the following results:

 Array
 (
  [ids] => Array
    (
        [0] => 35
        [1] => 22
        [2] => 32
        [3] => 38
        [4] => 36
        [5] => 39
        [6] => 16
        [7] => 20
        [8] => 48
        [9] => 46
    )

[mileage] => Array
    (
        [0] => 334
        [1] => 56
        [2] => 
        [3] => 43
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 11
        [9] => 5
    )

[vtype] => Array
    (
        [0] => 10T
        [1] => 10T
        [2] => 10T
        [3] => Artic
        [4] => 10T
        [5] => Artic
        [6] => Artic
        [7] => 10T
        [8] => Artic
        [9] => 10T
    )

What I would like to do is to create another array (the values of which I can use in later SQL queries) but only where the keys contain something in [ids][0], [mileage][0] and [vtype][0] and so on and so forth. I'd like to use [mileage] as the 'reference array'.

I've been playing around with array_intersect_key and array_diff_key but I've hit a big and rather a nasty wall. Can anyone help or give some pointers?

Thanks very much for your time.

Regards,

External.

  • 写回答

2条回答 默认 最新

  • doulu1968 2017-10-16 12:50
    关注

    You can use what you have to insert directly into mysql as follows:

    foreach ( $idmilearray['mileage'] as $KEY => $VAL ) {
        if ( empty($VAL) ) continue;
        $query = "INSERT INTO `tableName` (id, mileage, vtype) VALUES ('{$idmilearray['ids'][$KEY]}', '$VAL', '{$idmilearray['vtype'][$KEY]}')";
    }
    

    Or if you really want to merge everything for further processing or whatever reason you can do it like so:

    $NewArray = [];
    foreach ( $idmilearray['mileage'] as $KEY => $VAL ) {
        if ( empty($VAL) ) continue;
        $NewArray[] = array('id' => $idmilearray['ids'][$KEY], 'mileage' => $VAL, 'vtype' => $idmilearray['vtype'][$KEY]);
    }
    

    Ofcourse there are many other possible ways to do this, but these are the easiest given what you currently have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来