individual value in loop [closed]

2020-05-09 02:17发布

问题:

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 7 years ago.

I am running a loop:

$array = $_POST['d'];

foreach($array as $a){
    print_r($a);
}

$array contains the array (the number of array varies) and i get this as a result. I just want to know how i can call these values individually in the loop. for example: $thing = value of methv in the loop

Array ( 
   [1173627548] => Array ( 
     [num] => 1173627548 
     [methv] => dont know 
     [q1] => - 
     [q2] => - 
     [q3] => U 
     [q4] => - 
     [comm] => 
    ) 
) 

Sorry if i am unclear

print_r($array) show this:

Array ( [0] => Array ( [1173627548] => Array ( [num] => 1173627548 [methv] => dont know [q1] => - [q2] => - [q3] => U [q4] => - [comm] => ) ) [1] => Array ( [1182868902] => Array ( [num] => 1182868902 [methv] => dont know [q1] => - [q2] => - [q3] => U [q4] => - [comm] => ) ) ) Array ( [0] => Array ( [1173627548] => Array ( [num] => 1173627548 [methv] => dont know [q1] => - [q2] => - [q3] => U [q4] => - [comm] => ) ) [1] => Array ( [1182868902] => Array ( [num] => 1182868902 [methv] => dont know [q1] => - [q2] => - [q3] => U [q4] => - [comm] => ) ) ) 

Note: I now have 2 arrays in this array

回答1:

I guess you want to loop on array $a, each one of whose elements are also arrays.

When you loop $array there is one item whose info is the one you said:

Array ( 
   [1173627548] => Array ( 
     [num] => 1173627548 
     [methv] => dont know 
     [q1] => - 
     [q2] => - 
     [q3] => U 
     [q4] => - 
     [comm] => 
    ) 
) 

If you want to access methv element you should do the following:

foreach($array as $a){
    echo $a["methv"]; // this would access $array[ 1173627548 ][ "methv" ]
}

While the foreach continues you will keep accessing to all $array[ ][ "methv" ] values Hope it helps.


Based on your comment indicating the print_r($array).

First of all, let me indent it so that we get a better overview of the array:

    Array ( 
    [0] => Array ( 
    [1173627548] => Array ( 
        [num] => 1173627548 
        [methv] => dont know 
        [q1] => - 
        [q2] => - 
        [q3] => U 
        [q4] => - 
        [comm] => 
        )
      )
    [1] => Array ( 
     [1182868902] => Array ( 
        [num] => 1182868902 
        [methv] => dont know 
        [q1] => - 
        [q2] => - 
        [q3] => U 
        [q4] => - 
        [comm] => )
        )
)
 Array (
    [0] => Array (
    [1173627548] => Array ( 
        [num] => 1173627548 
        [methv] => dont know 
        [q1] => - 
        [q2] => - 
        [q3] => U 
        [q4] => - [comm] => 
        )
     )
    [1] => Array (
     [1182868902] => Array (
        [num] => 1182868902 
        [methv] => dont know 
        [q1] => - 
        [q2] => - 
        [q3] => U 
        [q4] => -
        [comm] => 
        )
    )
) 

I guess you are printing it twice, because we can see two exactly arrays one after the other.

If you want to get [methv] item, what you need is to access:

$array[ 0 ][ 1173627548 ][ "methv" ]
$array[ 1 ][ 1182868902 ][ "methv" ]

So what you can do is to use foreach twice:

foreach ($array as $a) {
    foreach ($a as $v) {
        echo $v[ "methv" ];
    }
}


回答2:

For debugging, I would suggest some crude but readable debugging, like this:

function trace($a) { print "<pre>".var_export($a, 1)."</pre>"; }
// then you can do:
trace($array);

As for your question, you can access the methv key like this: $array[0][1173627548]['methv'] (or $array[1173627548]['methv'] if the above trace is the full $array variable).