动态生成的数组

时间:2017-04-06 11:12:55

标签: php mysql codeigniter

我有一个数组:

Array
(
    [0] => Array
        (
            [Dated] => 2017-04-01
            [Nadeem] => 1995
            [NadeemKaat] => 40
            [Ali] => 0
            [AliKaat] => 0
            [Usman] => 0
            [UsmanKaat] => 0
        )

    [1] => Array
        (
            [Dated] => 2017-04-06
            [Nadeem] => 0
            [NadeemKaat] => 0
            [Ali] => 4800
            [AliKaat] => 96
            [Usman] => 0
            [UsmanKaat] => 0
        )

    [2] => Array
        (
            [Dated] => 2017-04-20
            [Nadeem] => 0
            [NadeemKaat] => 0
            [Ali] => 0
            [AliKaat] => 0
            [Usman] => 2100
            [UsmanKaat] => 42
        )

)

我想打印出这样的数组值:

Date          | Nadeem | Ali    | Usman
2017-04-01    | 1995   |        |
2017-04-06    |        | 4800   |
2017-04-20    |        |        |2100

我在这里混淆如何处理这种阵列,请帮帮我 我正在尝试简单的foreach循环

foreach ($stock as $key => $value) 
{
 echo $key . $value;
}

7 个答案:

答案 0 :(得分:1)

试试这个:

if (!empty($stock)) {
echo '<table><tr>';
foreach ($stock[0] as $key => $value) 
{
    echo '<th>' . $key . '</th>';
}
echo '</tr>';

foreach ($stock as $value) 
{
    echo '<tr>';
    foreach ($value as $key2 => $field) {
        echo '<td>' . $field . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
}

答案 1 :(得分:1)

  

@Shafat Ahmad尝试下面的一个:

role

答案 2 :(得分:0)

尝试内循环

// print header
foreach ($stock[0] as $key => $value) {
    echo $key. ' | '
}

// print data
foreach ($stock as $item) 
{
    foreach ($item as $value) {
        echo $value . ' | '
    }
}

然后根据|<tr>改变<td>,等等,如果你想要合适的桌子那么做一些。

或者您可以使用

$header = array_keys($stock[0]);
echo '<table><tr><th>';
echo implode('</th><th>', $header);
echo '</th></tr>';

// print data
foreach ($stock as $item) {
    echo '<tr><td>';
    echo implode('</td><td>', $item);
    echo '</td></tr>';
}
echo '</table>';

答案 3 :(得分:0)

试试这个:

foreach($stock as $data) {
    echo $data['Dated'].'    |    '.($data['Nadeem'] ? $data['Nadeem'] : "") .'    |    '.$data['NadeemKaat'].'    |    '.$data['Ali'].'    |    '.$data['AliKaat'].'    |    '.$data['Usman'].'    |    '.$data['UsmanKaat'].'<br>';
}

答案 4 :(得分:0)

$keys = array_keys($some[0]);
$theader = implode("\t", $keys);
echo $theader;
foreach ($some as $one) {
    echo "\n";
    for ($i = 0; $i < count($keys); $i++) {
        echo $one[$keys[$i]] . "\t";
    }
}

假设您的数据结构没有变化且$some是您上面给出的数组

输出:

Dated       Nadeem  NadeemKaat  Ali AliKaat     Usman   UsmanKaat
2017-04-01  1995    40          0       0       0       0   
2017-04-06  0       0           4800    96      0       0   
2017-04-20  0       0           0       0       2100    42  

答案 5 :(得分:0)

以下是示例程序。也许它会帮助你。

// user data can be dynamic
$userData = array('Nadeem', 'Ali', 'Usman');

// tblRows data can also be dynamic
$tblRowsData = array(
        '2017-04-01' => array('Nadeem'=>1995, 'Ali'=>'', 'Usman'=>''),
        '2017-04-06' => array('Nadeem'=>'', 'Ali'=>4800, 'Usman'=>''),
        '2017-04-20' => array('Nadeem'=>'', 'Ali'=>'', 'Usman'=>2100)
    );

echo "<table border=1>";
    echo "<tr>";
        echo "<td>Date</td>";
        foreach ($userData as $key => $value) {
            echo "<td>$value</td>";
        }
    echo "</tr>";


    foreach ($tblRowsData as $key => $value) {
        echo "<tr>";
            echo "<td>$key</td>";

            foreach ($userData as $key1 => $value1) {
                echo "<td>".$value[$value1]."</td>";
            }
        echo "</tr>";
    }
echo "</table>";

答案 6 :(得分:0)

有点狗屎编码,快速编写解决方案)))

$data = [];
foreach($input as $value) {
    foreach($value as $person => $count) {
        if($person !== 'Dated') {
            $data[$value['Dated']][$person] = $count;
        }
    }
}

if(!$data) {
    die('empty');
}

$persons = array_keys(reset($data));


$html = '<table border="1"><thead>
  <th>Date</th>';

foreach($persons as $key) {
    $html .= "<th>{$key}</th>";
}
$html .= '</thead><tbody>';

while($temp = array_splice( $data, 0, 1 )) {
    $date = array_keys($temp)[0];
    $html .= "<tr><td>".$date."</td>";
    foreach($persons as $name) {
        $val = isset($temp[$date][$name]) ? $temp[$date][$name] : '-';
        $html .= "<td>".$val."</td>";
    }
    $html .= "</tr>";
}
$html .= '</tbody></table>';
die($html);

输入如

$input = [
    [
        'Dated' => '2017-04-01',
        'Nadeem' => 1995,
        'NadeemKaat' => 40,
        'Ali' => 0,
        'AliKaat' => 0,
        'Usman' => 0,
        'UsmanKaat' => 0
    ],
    [
        'Dated' => '2017-05-11',
        'Nadeem' => 123,
        'NadeemKaat' => 40,
        'Ali' => 3,
        'AliKaat' => 0,
        'Usman' => 0,
        'UsmanKaat' => 0
    ]

];

HTML输出: enter image description here