如何获取数组中的值与PHP的多阵列? [英] How to get values in an array with many arrays in php?

查看:68
本文介绍了如何获取数组中的值与PHP的多阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑与数组列表中。我有一个包含我需要插入到数据库中值的文本文件。我已经爆炸了它,这样我可以得到这些分隔值。

  REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;

我的code就是这样的:

  $读= FILEREAD($ TMP);
$ perline =爆炸(;,$读);为($ J = 0; $ J<计数($ perline); $ J ++)
        {
            $ percomma =爆炸(,$ perline [$ J]);
            的print_r($ percomma);
        }

和的结果是这样的:

 阵列([0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822)阵列([0] =&GT 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] =→3 [5] => 54 [6] =大于1 [7] => 2 [8] =→4 [9] =→5 [10] => 12 [11] => 23 [12] => 46)阵列([0] => 0214 [1] = > 22.00 [2] => 32 [3] =→4 [4] => 11 [5] =→25 [6] =→4 [7] => 12 [8] =&GT 23 [9] =→5 [10] =→2 [11] =→2 [12] => 44)阵列([0] => 0313 [1] => 25.00 [2] =大于5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] =→5 [8] =→6 [9] =大于7 [10] => 12 [11] =→3 [12] => 33)阵列([0] =&GT)

我应该怎么做才能从第2个数组的数组4日的价值?我应该把它放在另一个数组内部,将其多维?或者还有其他的方法来解决这个?

修改

我的问题是如何能够从的print_r($ percomma)得到的值时,也有很多在结果阵列。结果数组是在那里,说的 键,结果是这样的

编辑2

由于制作阵列是多方面的,我得到这个结果:

 阵列([0] =>数组([0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] =→3 [5] => 54 [6] =大于1 [7] =→2 [8] =→4 [9] =→5 [10] => 12 [ 11] => 23 [12] => 46))阵列([0] =>阵列([0] => 0214 [1] => 22.00 [2] => 32 [3] = →4 [4] => 11 [5] =→25 [6] =→4 [7] => 12 [8] => 23 [9] =→5 [10] =&GT ; 2 [11] =→2 [12] => 44))阵列([0] =>阵列([0] => 0313 [1] => 25.00 [2] =大于5 [ 3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] =→5 [8] =→6 [9] =大于7 [10 ] => 12 [11] =→3 [12] => 33))阵列([0] =>阵列([0] =&GT))

这表明,他们都是各个阵列。我的code是这样的:

  $读= FILEREAD($ TMP);
$ perline =爆炸(;,$读);为($ J = 0; $ J<计数($ perline); $ J ++)
        {
            $ percomma =爆炸(,$ perline [$ J]);
            $项=阵列($ percomma);
            的print_r($条);
        }

编辑3

subas_poudel 的答案,我得到这样的结果:

 阵列
    (
    )
    排列
    (
        [0] =>排列
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] =>五
                [10] => 12
                [11] => 23
                [12] => 46
            )    )
    排列
    (
        [0] =>排列
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] =>五
                [10] => 12
                [11] => 23
                [12] => 46
            )        [1] =>排列
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] =>五
                [10] => 2
                [11] => 2
                [12] => 44
            )    )
    排列
    (
        [0] =>排列
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] =>五
                [10] => 12
                [11] => 23
                [12] => 46
            )        [1] =>排列
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] =>五
                [10] => 2
                [11] => 2
                [12] => 44
            )        [2] =>排列
            (
                [0] => 0313
                [1] => 25.00
                [2] =>五
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] =>五
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )    )
    排列
    (
        [0] =>排列
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] =>五
                [10] => 12
                [11] => 23
                [12] => 46
            )        [1] =>排列
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] =>五
                [10] => 2
                [11] => 2
                [12] => 44
            )        [2] =>排列
            (
                [0] => 0313
                [1] => 25.00
                [2] =>五
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] =>五
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )    )

最后一组阵列的正是我需要的,但我怎样才能得到这些值?

修改4
用我简单的错误我把 array_slice 我的内循环,这就是为什么我得到了太多的阵列。现在,这是@ subas_poudel的回答的结果。

 阵列
    (
        [0] =>排列
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] =>五
                [10] => 12
                [11] => 23
                [12] => 46
            )        [1] =>排列
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] =>五
                [10] => 2
                [11] => 2
                [12] => 44
            )        [2] =>排列
            (
                [0] => 0313
                [1] => 25.00
                [2] =>五
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] =>五
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )    )


解决方案

您可以使用多维数组来保存所有值。那么你可以通过$ percomma使用array_slice [索引你想]或得到你想要的所有阵列。

  $读= FILEREAD($ TMP);
$ perline =爆炸(;,$读);为($ J = 0; $ J<计数($ perline); $ J ++)
{
    $ percomma [] =爆炸(,,$ perline [$ j]的);}
呼应'< pre>'的print_r(array_slice($ percomma,1,3),真)。'< / pre>';

I am confused with this list of array. I have a text file that contains the values that I will need to insert to the database. I have exploded it so that I can get those separated values.

REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;

My code is just like this:

$read = FileRead($tmp);
$perline = explode(";",$read);

for($j=0; $j<count($perline); $j++)
        {
            $percomma = explode(",", $perline[$j]);
            print_r ($percomma);
        }

And the result is this:

Array ( [0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822 ) Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) Array ( [0] => )

What should I do to get the value from the 2nd array to the 4th array? Should I put it inside another array to make it multidimensional? Or there are other ways to solve this?

EDIT

My question is how will I be able to get the values from the print_r($percomma) when there are a lot of arrays in the result. The result array is up there that says And the result is this

EDIT 2

As making the array to be multidimensional I get this as a result:

Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => ) )

It shows that they're all individual arrays. My code is this:

$read = FileRead($tmp);
$perline = explode(";",$read);

for($j=0; $j<count($perline); $j++)
        {
            $percomma = explode(",", $perline[$j]);
            $entries = array($percomma);
            print_r ($entries);
        }

EDIT 3

From subas_poudel's answer I get this result:

    Array
    (
    )
    Array
    (
        [0] => Array
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] => 5
                [10] => 12
                [11] => 23
                [12] => 46
            )

    )
    Array
    (
        [0] => Array
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] => 5
                [10] => 12
                [11] => 23
                [12] => 46
            )

        [1] => Array
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] => 5
                [10] => 2
                [11] => 2
                [12] => 44
            )

    )
    Array
    (
        [0] => Array
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] => 5
                [10] => 12
                [11] => 23
                [12] => 46
            )

        [1] => Array
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] => 5
                [10] => 2
                [11] => 2
                [12] => 44
            )

        [2] => Array
            (
                [0] => 0313
                [1] => 25.00
                [2] => 5
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] => 5
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )

    )
    Array
    (
        [0] => Array
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] => 5
                [10] => 12
                [11] => 23
                [12] => 46
            )

        [1] => Array
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] => 5
                [10] => 2
                [11] => 2
                [12] => 44
            )

        [2] => Array
            (
                [0] => 0313
                [1] => 25.00
                [2] => 5
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] => 5
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )

    )

The last set of array is what I needed but how can I get those values?

EDIT 4 With my simple mistake I put the array_slice inside my for loop that's why I get too many arrays. Now this is the result of @subas_poudel's answer.

    Array
    (
        [0] => Array
            (
                [0] => 0111
                [1] => 260.00
                [2] => 23
                [3] => 34
                [4] => 3
                [5] => 54
                [6] => 1
                [7] => 2
                [8] => 4
                [9] => 5
                [10] => 12
                [11] => 23
                [12] => 46
            )

        [1] => Array
            (
                [0] => 0214
                [1] => 22.00
                [2] => 32
                [3] => 4
                [4] => 11
                [5] => 25
                [6] => 4
                [7] => 12
                [8] => 23
                [9] => 5
                [10] => 2
                [11] => 2
                [12] => 44
            )

        [2] => Array
            (
                [0] => 0313
                [1] => 25.00
                [2] => 5
                [3] => 52
                [4] => 12
                [5] => 45
                [6] => 12
                [7] => 5
                [8] => 6
                [9] => 7
                [10] => 12
                [11] => 3
                [12] => 33
            )

    )

解决方案

you can use multidimensional array to hold all the value. then you can get all the array you want by either $percomma[index you want] or using array_slice.

$read = FileRead($tmp);
$perline = explode(";",$read);

for($j=0; $j<count($perline); $j++)
{
    $percomma[] = explode(",", $perline[$j]);          

}
echo '<pre>'.print_r (array_slice($percomma,1,3),true).'</pre>';

这篇关于如何获取数组中的值与PHP的多阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆