简体   繁体   English

如何访问 php 中的数组元素

[英]How to access elements of array in php

Below is the code snippet:下面是代码片段:

$resultArray = explode("\n",$cmd);
print_r($resultArray);
$tempArray = array();
foreach($resultArray as $key){
        $tempArray = explode(" ",$key);
         print_r($tempArray);
}

first print gives output:-第一次打印给出 output:-

Array
(

 [0] => AA-BB-E3-1B-81-6A 10.10.10.2
 [1] => CC-DD-E3-1B-7E-5A 10.10.10.3
)

Second print gives output:-第二次打印给出 output:-

Array
(
    [0] => AA-BB-E3-1B-81-6A
    [1] => 10.10.10.2
)
Array
(
    [0] => CC-DD-E3-1B-7E-5A
    [1] => 10.10.10.3
)
Array
(
    [0] => 
)

Assuming there will be many entries of mac addresses corresponding mac addresses, I want to store them in separate variables for further use.假设会有很多mac地址对应的mac地址条目,我想将它们存储在单独的变量中以供进一步使用。

Please help.请帮忙。 I am new to PHP, learning on my own.我是 PHP 的新手,自己学习。 Any help is appreciated.任何帮助表示赞赏。

eidt 1: Expected output should be two arrays, one each for mac address and Ip Address from which I would be able to loop through and query database for each mac address. eidt 1:预期的 output 应该是两个 arrays,一个用于 mac 地址和 Ip 地址,我可以从中循环并查询每个 mac 地址的数据库。

Well I'm not sure what you want to do, but from the little I understood is to separate the mac addresses:好吧,我不确定您要做什么,但是据我所知,将mac地址分开:

$resultArray = explode("\n", $cmd);
print_r($resultArray);
$tempArray = array();
foreach($resultArray as $key){
    $dm = explode(" ", $key);
    $tempArray[] = $dm[0];
}
print_r($tempArray);

The result would be:结果将是:

Array
(
    [0] => AA-BB-E3-1B-81-6A
    [1] => CC-DD-E3-1B-7E-5A
     .
     .
     .
)

It would be much better if you put the expected result, in order to help you better.如果你把预期的结果放进去会更好,以便更好地帮助你。

You forget [] for tempArray:您忘记了 tempArray 的 []:

$resultArray = explode("\n",$cmd);
print_r($resultArray);
$tempArray = array();
foreach($resultArray as $key){
        $tempArray[] = explode(" ",$key);
         print_r($tempArray);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM