如何在 php 中获取动态生成的数组的值


How to get values for a dynamically generated array in php

我正在用PHP生成一个字典,其中键和值被添加到字典中。

但是我无法取回这些值。我使用以下代码:

    //some code here
    while (($line = fgets($fileServices)) !== false) {
            //echo $line.'....................';
            if (strpos($line,'header') == false){
                    $serviceName=explode(",", $line)[0];
                    $RestartData=explode(",", $line)[1];
                    $StatusData=explode(",", $line)[2];
                    $serviceRestartMappingdict[$totalServices]= $serviceName.':'.$RestartData;
                    $serviceStatusMappingdict[$totalServices]= $serviceName.'_'.$StatusData;
                    $totalServices = $totalServices+1;
            }
    }
    $counter=0;
    //echo $serviceStatusMappingdict[0];
    fclose($fileServices);
    $counter=0;
    for ($i = 0; $i < count($serviceStatusMappingdict); ++$i){
            echo '<<<<<<<<<<<<<<<<<<<<<<<'.$serviceStatusMappingdict[$i].'>>>>>>>>>>>>>>>>>>>>>>>>>>>';
    }

如果我做像echo $serviceStatusMappingdict[0];这样的回显,我会得到值,但是当我使用循环访问数据时,我没有得到任何值。

[编辑] 由于"<"字符,问题来了。摆脱它们,它会立即工作

为了回答以下出现的注释,html中组合的字符"<"和">"是指标签的打开和关闭。 例如:<div>

问题来了,因为浏览器试图将其作为未知元素进行内化,并且不知道如何处理它。如果您检查页面的 html 代码,您将能够看到信息确实存在,只是没有正确呈现。

[编辑] 在 Umpert parial 答案之后,我尝试了这个,它做了预期的行为。我们能否提供有关为什么它在您的情况下不起作用的更多信息:

<?php
 $array = array( '0' => "kakfa_ps -ef | grep kafka | grep server.properties",
            '1' => "zookeeper_ps -ef | grep zookeeper | grep zookeeper.properties",
            '2' => "schemaregistry_ps -ef | grep schema-registry | grep java",
            '3' => "influx_/sbin/service influxdb status | grep active | grep running",
            '4' => "mysql_/sbin/service mysql status | grep active | grep running",
            '5' => "cassandra_/sbin/service cassandra status | grep active | grep exited",
            '6' => "aerospike_/sbin/service aerospike status | grep active | grep running");
for($i=0;$i<count($array);++$i){
  echo '<<<<<<<<<<<<<<<<<<<<<<<'.$array[$i].'>>>>>>>>>>>>>>>>>>>>>>>>>>>';
}
 ?>

OP中的"<"和">"数量相同。只是为了确保。