如何迭代复杂的关联数组?

时间:2018-05-11 13:50:14

标签: php foreach

我正在使用以下数组。

Array
(
[device_id] => 432552343242
[host_name] => computer
[serial_number] => 32525243
[manufacturer] => HP
[model] => ProLiant BL460c Gen9
[locations] => Array
    (
        [0] => Array
            (
                [name] => New York
            )

    )

[cpu_count] => 1
[core_count] => 6
[is_virtual] => 
[operating_system] => Array
    (
        [version] => 6.6 Santiago
        [product] => Array
            (
                [vendor] => Varied
                [description] => ProductAdapter for handling various Unix or 
Linux based Operating Systems (HP-UX, AIX, Solaris, Ubuntu, etc...)
                [name] => Unix Operating Systems
            )

        [name] => Linux Server
    )

[users] => mydomain.com/users
[applications] => mydomain.com/applications
[qualified_name] => Array
    (
        [0] => Array
            (
                [name_type] => UnixName
                [name] => cbc.com
            )

        [1] => Array
            (
                [name_type] => DNSFQDN
                [name] => abc.com
            )

        [2] => Array
            (
                [name_type] => DNSDomain
                [name] => bbc.com
            )

        [3] => Array
            (
                [name_type] => OriginalHostname
                [name] => cnn.com
            )

    )
)

当我尝试循环遍历数组以将其插入数据库时​​,我得到13个条目。

这是我的代码。

$array = json_decode($result, true);
foreach($array as $key=>$value) {
$hostname = $array['locations'];

$stmt = $mysqli->prepare("INSERT INTO test (Hostname) VALUES (?)");
$stmt->bind_param("s", $hostname);
$stmt->execute();
$stmt->close();
}

我知道为什么要加入13支队伍。它循环遍历数组中的每个索引并为每个索引插入主机名。我怎样才能让它插入它?我是否必须创建for语句?或者是foreach中的foreach?任何帮助,将不胜感激?新手在这里。

1 个答案:

答案 0 :(得分:0)

您循环的次数与最大阵列中的索引一样多,您不使用$key$value。你的代码完全按照你对他说的去做:

  • 表示每个值 - >所以13循环
    • 获取$array['location']
    • 将其插入数据库
  • 你的foreach结束

如果您只想插入$array['locations'],为什么要使用foreach? 您只插入一个值,并且您知道它是索引,因此您实际上不需要使用foreach

使用foreach即可:

$array = json_decode($result, true);
foreach($array as $key=>$value) {
    if ($key === "location")
    {
        //$hostname = $array['locations'];
        $hostname = $value;

        $stmt = $mysqli->prepare("INSERT INTO test (Hostname) VALUES (?)");
        $stmt->bind_param("s", $hostname);
        $stmt->execute();
        $stmt->close();
    }
}

但是没有必要。

我会更喜欢这样写:

$array = json_decode($result, true);
$hostname = $array['location'];

$stmt = $mysqli->prepare("INSERT INTO test (Hostname) VALUES (?)");
$stmt->bind_param("s", $hostname);
$stmt->execute();
$stmt->close();

[编辑]

如果要循环插入数组的不同元素,则应使用$key$value变量。

$array = json_decode($result, true);
foreach($array as $key=>$value) {
    if ($key === "location")
    {
        $hostname = $value; //contains $array['location']

        $stmt = $mysqli->prepare("INSERT INTO test (Hostname) VALUES (?)");
        $stmt->bind_param("s", $hostname);
        $stmt->execute();
        $stmt->close();
    } else if ($key === 'device_id')
    {
        $devide_id= $value; //contains $array['device_id']

        $stmt = $mysqli->prepare("INSERT INTO test (DeviceId) VALUES (?)");
        $stmt->bind_param("s", $devide_id);
        $stmt->execute();
        $stmt->close();
    } else if ($key === 'device_id')
    {
        $devide_id= $value; //contains $array['device_id']

        $stmt = $mysqli->prepare("INSERT INTO test (DeviceId) VALUES (?)");
        $stmt->bind_param("s", $devide_id);
        $stmt->execute();
        $stmt->close();
    } else if ($key === 'device_id')
    {
        $devide_id= $value; //contains $array['device_id']

        $stmt = $mysqli->prepare("INSERT INTO test (DeviceId) VALUES (?)");
        $stmt->bind_param("s", $devide_id);
        $stmt->execute();
        $stmt->close();
    } else if ($key === 'device_id')
    {
        $devide_id= $value; //contains $array['device_id']

        $stmt = $mysqli->prepare("INSERT INTO test (DeviceId) VALUES (?)");
        $stmt->bind_param("s", $devide_id);
        $stmt->execute();
        $stmt->close();
    } else if ($key === '...')
    {
        //etc
    }
}

但如果您确切知道数组中值的键,我会更喜欢这样做:

$hostname  = $array['location'];
$devide_id = $array['device_id'];

$stmt = $mysqli->prepare("INSERT INTO test (Hostname) VALUES (?)");
$stmt->bind_param("s", $hostname);
$stmt->execute();

$stmt = $mysqli->prepare("INSERT INTO test (DeviceId) VALUES (?)");
$stmt->bind_param("s", $devide_id);
$stmt->execute();

$stmt->close();