PHP变量,我的代码有什么问题?
有一个PHP脚本,它使用python脚本获取用户的新数据。 我能够PHP变量,我的代码有什么问题?,php,arrays,shell,variables,command,Php,Arrays,Shell,Variables,Command,有一个PHP脚本,它使用python脚本获取用户的新数据。 我能够foreach和循环并获取数组的键2和值2,但无法获取$address和$product变量以将其放入$shell命令中。我的变量有什么问题? 刚开始编写代码,如果有人教我这一点,我会很有帮助 foreach ($gotdata as $key1 => $value1) { foreach ($value1 as $key2 => $value2) { //print " key2: "
foreach
和循环并获取数组的键2和值2,但无法获取$address
和$product
变量以将其放入$shell命令中。我的变量有什么问题?
刚开始编写代码,如果有人教我这一点,我会很有帮助
foreach ($gotdata as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
//print " key2: " . $key2 . " value2: ". $value2;
$address = "";
$maker = "";
if($key2 == 'Address'){
$address = $value2;
print "address: ".$address; //able to get it on console
}
if($key2 == 'Product'){
$product = $value2;
print "product: ".$product; //able to get it on console
}
$shell = 'python /opt/getdata.py '.$address.' '.$product;
print "shell:" . $shell; //not able to get $address and $product
// $current = shell_exec($shell);
// $current = (int)$current;
// print "current:" . $current;
}
}
这就是多维数组的外观。每次运行脚本时,主数组的大小都不同。可能是10或14等。
这个 另一方面,子阵列始终具有相同的六项
array(2) {
[0]=>
array(6) {
["Id"]=>
string(1) "1"
["Name"]=>
string(15) "name01"
["Address"]=>
string(12) "1.1.1.1"
["Product"]=>
string(1) "1"
["Current"]=>
string(3) "111"
["DateTime"]=>
string(19) "2020-09-09 15:03:46"
}
[1]=>
array(6) {
["Id"]=>
string(1) "2"
["Name"]=>
string(15) "name02"
["Address"]=>
string(12) "1.1.1.2"
["Product"]=>
string(1) "1"
["Current"]=>
string(3) "111"
["DateTime"]=>
string(19) "2020-09-09 15:03:46"
}
我假设您可以直接从主循环访问所需的值 编辑:现在数组已经发布,我相信这会起作用。
不需要嵌套循环数组
嵌套foreach循环与访问子数组项相同。我不知道您想要的输出是什么。但如果你想要两个值。试试看
foreach ($gotdata as $key1 => $value1) {
$address = "";
$product = "";
foreach ($value1 as $key2 => $value2) {
//print " key2: " . $key2 . " value2: ". $value2;
if($key2 == 'Address'){
$address = $value2;
print "address: ".$address; //able to get it on console
}
elseif($key2 == 'Product'){
$product = $value2;
print "product: ".$product; //able to get it on console
}
if($address!="" && $product!=""){
$shell = 'python /opt/getdata.py '.$address.' '.$product;
print "shell:" . $shell; //not able to get $address and $product
}
// $current = shell_exec($shell);
// $current = (int)$current;
// print "current:" . $current;
}
}
另外,您还没有在代码中定义$product。$key2可以同时是adress和product,因此它必须循环通过,然后再次将值重置为“”。您希望IF语句后面的
$address
和$product
是什么?如果$key2
是“地址”,则将设置$Address
,但$product
永远不会设置。反之亦然,如果$key2
是“产品”,则将设置$Product
,但$address
永远不会设置。当您获得问题的所有部分时,帮助会容易得多。不仅仅是代码。还要共享您的数组。对于另一个条件或使用switch case使用else语句。@user7431522没问题!你知道你可以接受答案来向其他人表明问题已经解决了吗?已经转到也许你也可以在这方面提供帮助:)
foreach ($gotdata as $key1 => $value1) {
$address = "";
$product = "";
foreach ($value1 as $key2 => $value2) {
//print " key2: " . $key2 . " value2: ". $value2;
if($key2 == 'Address'){
$address = $value2;
print "address: ".$address; //able to get it on console
}
elseif($key2 == 'Product'){
$product = $value2;
print "product: ".$product; //able to get it on console
}
if($address!="" && $product!=""){
$shell = 'python /opt/getdata.py '.$address.' '.$product;
print "shell:" . $shell; //not able to get $address and $product
}
// $current = shell_exec($shell);
// $current = (int)$current;
// print "current:" . $current;
}
}