循环通过php数组内容将内容返回到屏幕

时间:2017-07-10 16:26:11

标签: php html arrays

我有一个看起来像这样的数组($ myArray)

Array ( [0] => Array ( [0] => Array ( [ID] => 322 [Number] => 1 [Date] => 3117-01-41 [example] => Hello  ) 
[1] => Array ( [ID] => 123 [Number] => 49 [Date] => 1717-05-21 [example] => Hi )
[2] => Array ( [ID] => 007A [Number] => 42 [Date] => 2005-11-24 [example] => Some Text  )
[3] => Array ( [ID] => 999AAA [Number] => 492 [Date] => 3117-01-21 [example] => Text Test Text  )))

在我的页面中,我使用的函数返回内容($ content),并且该内容显示在Web浏览器上。没有回显或打印只返回不断被追加到的内容变量。

我真的想循环遍历我的数组并将某个字段的值打印到屏幕上,例如

while(//not sure what goes here){
  $content .= '<p>'.$someVariable["Number"].'</p>';
  $content .= '<p>'.$someVariable["example"].'</p>';
  $content .= '<p>'.$someVariable["date"].'</p>';
}

我不确定while循环是否是实现所需结果的最佳方法。另外使用fetch_array不是我可以使用的选项,因为它会破坏以前的代码。

3 个答案:

答案 0 :(得分:0)

<?php
$yourArray = array ( 
array (
    'ID' => 322,
    'Number' => 1,
    'Date' => '3117-01-41',
    'example' => 'Hello'
), 
array (
    'ID' => 123,
    'Number' => 49,
    'Date' => '1717-05-21',
    'example' => 'Hi'
),
array (
    'ID' => '007A',
    'Number' => 42, 
    'Date' => '2005-11-24',
    'example' => 'Some Text'
),
array (
    'ID' => '999AAA',
    'Number' => 492,
    'Date' => '3117-01-21',
    'example' => 'Text Test Text'
)

);
?>

打印整个数组:

<?php print_r($yourArray); ?>

打印某些级别的数组:

<?php print_r($yourArray[2]); ?>

打印数组的某些元素:

print_r($yourArray[2]['Date']);

逐级打印数组:

<?php
for($index=0; $index < count($yourArray); $index++){
    echo $yourArray[$index]['ID'].'<br>';
    echo $yourArray[$index]['Number'].'<br>';
    echo $yourArray[$index]['Date'].'<br>';
    echo $yourArray[$index]['example'].'<br>';
};
?>

答案 1 :(得分:0)

这有帮助吗?

import os 
import lxml.etree as et

dir = "C:\\Path\\To\\XML\\Files"

# LOOP THROUGH FILES IN DIRECTORY
for f in os.listdir(dir):
    if f.endswith('.xml'):

        # PARSE XML
        doc = et.parse(os.path.join(dir, f))

        for i in range(1,len(doc.xpath('//master'))+1):
            # PARSE XSLT (same xslstr as above -no need to loop it)
            xsl = et.fromstring(xslstr.format(i))

            # TRANFORM INPUT TO OUTPUT
            transform = et.XSLT(xsl)
            result = transform(doc)

            # SAVE OUTPUT
            outfile = doc.xpath('//master[{}]/@name'.format(i))[0] + '.xml'
            with open(os.path.join(dir, outfile), 'wb') as f:
                f.write(result)

答案 2 :(得分:0)

看来你正在寻求这个:

$yourArray = array ( 
array (
    'ID' => 322,
    'Number' => 1,
    'Date' => '3117-01-41',
    'example' => 'Hello'
), 
array (
    'ID' => 123,
    'Number' => 49,
    'Date' => '1717-05-21',
    'example' => 'Hi'
),
array (
    'ID' => '007A',
    'Number' => 42, 
    'Date' => '2005-11-24',
    'example' => 'Some Text'
),
array (
    'ID' => '999AAA',
    'Number' => 492,
    'Date' => '3117-01-21',
    'example' => 'Text Test Text'
)

);


$content = '';
foreach($yourArray as $key => $value):

    $content .= '<p>'.$value["Number"].'</p>';
    $content .= '<p>'.$value["example"].'</p>';
    $content .= '<p>'.$value["Date"].'</p>';

endforeach;

print $content;