如何使用php从键值对中的多维数组中回显值

时间:2018-11-13 05:45:38

标签: php arrays multidimensional-array

我有一个多维数组,其中包含数据库表中的值,但是值是键值格式,因此我尝试使用forloop和foreach循环进行打印,但无法获取输出,那么如何使用php forloop或foreach循环执行此操作?

数组中的值

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Email] => xyz@gmail.com
                    [mobile] => 123456
                    [address] => 'xyz xyz'
                    [maritalstatus] => 'married'

                    [Role] => 'employee'

                )

            [1] => Array
                (
                    [joiningdate] => 2012-11-01
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Email] => abc@gmail.com
                    [mobile] => 123456
                    [address] => 'xyz xyz'

                    [maritalstatus] => 'married'
                    [Role] => 'employee'


                )

            [1] => Array
                (
                    [joiningdate] => 2012-11-01
                )

        )

        )

使用forloop打印值

for ($x = 0; $x < count($rows); $x++) {

                            echo '<tr>';
                            foreach ($rows[$x][0] as $key => $value) {

                                $indexposition = array_search($key, array_keys($rows[$x][0]));


                            }
                            echo '</tr>';
                            }

2 个答案:

答案 0 :(得分:0)

如果您只想打印数组,请使用var_dump($ your_variable),否则,请说出您到底想要什么:)

答案 1 :(得分:0)

最后通过简单的方法得到了解决方案。 假设我们然后从$ rows获取所有数组

<script>
     function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#preview').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imagetr").change(function () {
        readURL(this);
    });
</script>