使用不同的名称循环$ _POST变量

时间:2016-08-01 17:03:32

标签: php

如何使用不同的名称循环$ _POST变量,这是print_r的输出:

帖子:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MarkAsFav extends Activity {

    private DBHelper mydb;

    TextView header;

    int id_To_Update = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mark_fav_layout);
        header = (TextView) findViewById(R.id.editTextName);

        mydb = new DBHelper(this);
        mydb.getWritableDatabase();
        Button b = (Button) findViewById(R.id.button1);
        b.setVisibility(View.VISIBLE);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            int value = extras.getInt("id");

            if (value > 0) {
                //means this is the view part not the add contact part.
              /*  Cursor rs = mydb.getData(value);
                id_To_Update = value;
                rs.moveToFirst();

                String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));

                if (!rs.isClosed()) {
                    rs.close();
                }
                Button b = (Button) findViewById(R.id.button1);
                b.setVisibility(View.INVISIBLE);

                header.setText( nam);
                header.setFocusable(false);
                header.setClickable(false);*/

            }
        }
    }


    public void run(View view) {
      Bundle extras = getIntent().getExtras();
        if (extras != null) {

            //int value     =   extras.getInt("id");
            String headerValue =header.getText().toString();
            String value1 =   extras.getString("title");
            String value2 =   extras.getString("company");
            String value3 =   extras.getString("city");
            String value4 =   extras.getString("state");
            String value5 =   extras.getString("country");
            String value6 =   extras.getString("formattedLocation");
            String value7 =   extras.getString("source");
            String value8 =   extras.getString("date");
            String value9 =   extras.getString("snippet");
            String value10=   extras.getString("url");
            String value11=   extras.getString("onmousedown");
            String value12=   extras.getString("latitude");
            String value13=   extras.getString("longitude");
            String value14=   extras.getString("jobkey");
            String value15=   extras.getString("sponsored");
            String value16=   extras.getString("expired");
            String value17=   extras.getString("formattedLocationFull");
            String value18=   extras.getString("formattedRelativeTime");
           // String value19="0";


            Log.e("ERROR", "Inside run and checking Value and val");

              // if (mydb.insertContact("header", "name", "company","city","state","country","formattedLocation","source","date","snippet","url","onmousedown","latitude","longitude","jobkey","sponsored","expired","formattedLocationFull","formattedRelativeTime","checked")){
            if   (mydb.insertContact(headerValue, value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18)){
                    Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
                    //Log.e("ERROR", "insert contact errors");
                } else {
                    Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            //}
        }
    }
}

其中

$_POST['post_access_1'];
$_POST['post_access_2'];
$_POST['post_access_3'];
$_POST['post_access_4'];

Array
(
    [post_access_1] = Array
        (
            [0] = 1
            [1] = 6
        )

    [post_access_2] = Array
        (
            [0] = 1
            [1] = 4
        )

    [post_access_3] = Array
        (
            [0] = 2
            [1] = 5
            [2] = 6
        )

    [post_access_4] = Array
        (
            [0] = 2
            [1] = 5
        )
)

我有搜索,但没有不同的名字。

三江源

2 个答案:

答案 0 :(得分:1)

foreach

foreach ($array as $foo)
{
// do something with $foo
}

如果您还需要访问键/子阵列名称:

foreach ($array as $k => $v)
{
// do something with $k and or $v
}

答案 1 :(得分:1)

你可以像这样对POST变量进行foreach循环。

foreach ($_POST as $key => $value){
    if (is_array($value)){
        echo $key ." Is an array in post.";
        foreach ($value as $k => $v){
            echo $k." is the index and ".$v." is the value in the array.";
        }
    }
}