1

I am having a $_POST array look like this:

Array
(
    [veryfy_doc_type] => Array
            (
                [0] => 1
                [1] => 2
            )

    [id_number] => Array
            (
                [0] => 3242424
                [1] => 4456889
            )

    [document_issuer] => Array
            (
                    [0] => 1
                    [1] => 3
            )

    [verify_doc_expiry_date] => Array
            (
                    [0] => 2016-01-26
                    [1] => 2015-02-20
            )

    [doc_id] => Array
            (
                    [0] => 15
                    [1] => 16
            )

    [user_id] => Array
            (
                    [0] => 14
                    [1] => 14
            )
)

Using this array I need to get each values into php variables.

I tried it something like this, but it doesn't work for me.

foreach($_POST AS $k => $v) { 
    //print_r($v); 
    list($doc_type, $id_number, $issuer, $expiry_date, $doc_id, $user_id) = $v;
}

echo "Type = $doc_type";

Can anybody tell me how to figure this out. Thank you.

5
  • 2
    $_POST is php variable, so the values already are "in" one ;-) Commented Jan 8, 2016 at 1:58
  • $doc_type will be 1 or 2? it cant be both Commented Jan 8, 2016 at 1:58
  • @Dagon, $doc_type values can not be same. Commented Jan 8, 2016 at 2:01
  • @Terminus, I checked the question that you have linked. but not sure how it help for this scenario. Can you kindly show me an example. Thank you. Commented Jan 8, 2016 at 2:03
  • have you tried to use extract? Commented Jan 8, 2016 at 2:42

6 Answers 6

2

This might help you since you can also use extract in php to create variables.

<?php
$_POST = array(
    'veryfy_doc_type'=> array(1,2),
    'id_number' => array(3242424,4456889),
    'document_issuer'=> array(1,3),
    'verify_doc_expiry_date'=> array('2016-01-26','2015-02-20'),
    'doc_id' => array(15,16),
    'doc_id' => array(14,14)
);


extract($_POST);
print_r($veryfy_doc_type);
print_r($id_number);
Sign up to request clarification or add additional context in comments.

4 Comments

Dangerous thing to suggest when you're dealing with posted data.
A lot of things are dangerous. It's up to the developer to make that call.
@Robbiee I dont know how could it be dangerous if he will just use a certain index that you know.I think you are also saying that using $_POST['post_name'] variable is dangerous.. when in fact you know the post_name index.
I mean that if you use say $userId in your script then someone could post that variable and have it potentially overwrite yours. Use with caution is all I'm saying.
1

So you want to reference each of the sub-array values while looping the main array... maybe something like this?

// Loop one of the sub arrays - you need to know how many times to loop!
foreach ($_POST['veryfy_doc_type'] as $key => $value) {
    // Filter the main array and use the $key (0 or 1) for the callback
    $rowValues = array_map(function($row) use ($key) {
        // Return the sub-array value using the $key (0 or 1) for this level
        return $row[$key];
    }, $_POST);

    print_r($rowValues);
}

Example: https://eval.in/498895

This would get you structured arrays for each set of data.

From here I'd suggest you leave the arrays as they are rather than exporting to variables, but if you wanted to you you could use the list() as in your example.

Comments

1

You can use a MultipleIterator for that:

<?php
$post = array(
    'veryfy_doc_type' => array('1','2'),
    'id_number' => array('3242424','4456889'),
    'document_issuer' => array(1,3),
    'verify_doc_expiry_date' => array('2016-01-26','2015-02-20'),
    'doc_id' => array(15,16),
    'user_id' => array(14,14)
);


$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);
foreach($post as $k=>$v) {
    $mit->attachIterator( new ArrayIterator($v), $k);
}


foreach( $mit as $row ) {
    echo $row['doc_id'], ' ', $row['id_number'], ' ', $row['verify_doc_expiry_date'], "\r\n";
}

prints

15 3242424 2016-01-26
16 4456889 2015-02-20

If you have control over the client code you can change the names of the POST parameters in a way that php build this structure automagically. E.g.

<form method="POST" action="test.php">
    <input type="hidden" name="record[0][veryfy_doc_type]" value="1" />
    <input type="hidden" name="record[0][id_number]" value="3242424" />
    <input type="hidden" name="record[0][document_issuer]" value="1" />
    <input type="hidden" name="record[0][verify_doc_expiry_date]" value="2016-01-26" />
    <input type="hidden" name="record[0][doc_id]" value="15" />
    <input type="hidden" name="record[0][user_id]" value="14" />

    <input type="hidden" name="record[1][veryfy_doc_type]" value="2" />
    <input type="hidden" name="record[1][id_number]" value="4456889" />
    <input type="hidden" name="record[1][document_issuer]" value="3" />
    <input type="hidden" name="record[1][verify_doc_expiry_date]" value="2015-02-20" />
    <input type="hidden" name="record[1][doc_id]" value="16" />
    <input type="hidden" name="record[1][user_id]" value="14" />

    <input type="submit" />
</form>

would do/cause that.

7 Comments

Can you show me how to use list() within foreach loop. I tried it something like this list($doc_type, $id_number, $issuer, $expiry_date, $doc_id, $user_id) = $row;. But it have Undefined offset: 5 error.
Change MultipleIterator::MIT_KEYS_ASSOC to MultipleIterator::MIT_KEYS_NUMERIC. But why not simply use $row? There's no need for additional pollution of the variable namespace.
I need execute an update query. something like this : $stmt->bind_param('iissii', $doc_type,$id_number, $issuer, $expiry_date, $doc_id, $user_id); foreach ($mit as $row) { list($doc_type, $id_number, $issuer, $expiry_date, $doc_id, $user_id) = $row; $stmt->execute(); }
Is there other way to do it without using list();
In that special case I'd go with list() as well.
|
0

If you only want to get the value of the post then you can have a simple multidimentional loop. no need to make it broad

   foreach($_POST as $keyName => $row){
      // $keyName will be verify_doc_type, id_number, etc..
      foreach($row as $value){
      // do something
      echo $value;
     }
  }

Hope that helps.

Comments

0

This method will generate variables of same name as in POST array:

    foreach ($_POST as $k => $v )
    {
        $$k =  addslashes(trim($v ));
    
    }

Comments

0

I know this post is a bit old, but I was recently trying to achieve this very same thing.

Hopefully the answer will point others in the right direction.

As per the documentation on list():

Since PHP 7.1, keys can be specified

Example from the manual:

<?php
  $arr = ['locality' => 'Tunis', 'postal_code' => '1110'];

  list('postal_code' => $zipCode, 'locality' => $locality) = $arr;

  echo $zipCode; // will output 1110
  echo $locality; // will output Tunis
?>

https://www.php.net/manual/en/function.list.php#121094

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.