[英]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的部分回答之后,我嘗試了此操作,並完成了預期的行為。 我們能否提供更多有關您的情況為何不起作用的信息:
<?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中的數量相同的'<'和'>'。 只想確認一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.