在PHP中使用数组值中的空格?

时间:2015-11-22 08:07:07

标签: php arrays

我是一个自我教学的菜鸟,所以对这个问题的基本性质表示道歉,我想其他地方已经回答了!我确实试图找到答案。 :)

是否有可能以某种方式在数组值中使用空格?例如,我想指定数组中每个位置的朋友:

<?php
    $friends = array("John White", "Jane", "John Black" );
  ?>

有办法吗?或者我误解了阵列的重点?

1 个答案:

答案 0 :(得分:0)

是的,您可以在数组值中使用空格,如下所示:

public class ClassRecord extends AppCompatActivity {
    private ListView mylistView;
    String text;
    TextView inst_id, desc, sc, sched,hour, day, room, acad;
    FloatingActionButton fab;
    ArrayList<HashMap<String, String>> classrecordList = new ArrayList<HashMap<String, String>>();

    private static String url = "http://192.168.0.106/MobileClassRecord/getClassRecord.php";

    private static final String TAG_CR = "class_records";
    private static final String TAG_ID = "instructor_id";
    private static final String TAG_DESC = "description";
    private static final String TAG_SC = "subj_code";
    private static final String TAG_SCHED = "sched_type";
    private static final String TAG_HOUR = "hour";
    private static final String TAG_DAY = "day";
    private static final String TAG_RM = "room";
    private static final String TAG_ACAD = "acad";

    JSONArray classrecord = null;


    @Override
    public void onBackPressed() {
        Intent intent = new Intent(ClassRecord.this, MainActivity.class);
        startActivity(intent);
        super.onBackPressed();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_record);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent fabIntent = new Intent(ClassRecord.this, AddClassRecord.class);
                startActivity(fabIntent);

            }
        });
        new JSONParse().execute();
        classrecordList =new ArrayList<HashMap<String, String>>();

    }

    class JSONParse extends AsyncTask<String, String, JSONObject>{
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            inst_id = (TextView) findViewById(R.id.inst_id);
            desc = (TextView) findViewById(R.id.desc);
            sc = (TextView) findViewById(R.id.subj_code);
            sched= (TextView) findViewById(R.id.sched);
            hour=(TextView) findViewById(R.id.hour);
            day=(TextView) findViewById(R.id.day);
            room = (TextView) findViewById(R.id.room);
            acad = (TextView) findViewById(R.id.acad);
            pDialog = new ProgressDialog(ClassRecord.this);
            pDialog.setMessage("Getting Data from Database...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... params) {
            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            pDialog.dismiss();
            try {
                classrecord = jsonObject.getJSONArray(TAG_CR);
                for (int i = 0; i < classrecord.length(); i++){
                    JSONObject c = classrecord.getJSONObject(i);

                    final String Inst_id = c.getString(TAG_ID);
                    String Desc = c.getString(TAG_DESC);
                    String Sc = c.getString(TAG_SC);
                    String Sched = c.getString(TAG_SCHED);
                    String Hour = c.getString(TAG_HOUR);
                    String Day = c.getString(TAG_DAY);
                    String Rm = c.getString(TAG_RM);
                    String Acad = c.getString(TAG_ACAD);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_ID, Inst_id);
                    map.put(TAG_DESC, Desc);
                    map.put(TAG_SC, Sc);
                    map.put(TAG_SCHED, Sched);
                    map.put(TAG_HOUR, Hour);
                    map.put(TAG_DAY, Day);
                    map.put(TAG_RM, Rm);
                    map.put(TAG_ACAD, Acad);

                    classrecordList.add(map);

                    mylistView = (ListView) findViewById(R.id.list);
                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                    fab.attachToListView(mylistView);
                    ListAdapter adapter = new SimpleAdapter(ClassRecord.this, classrecordList,
                            R.layout.list, new String[]{TAG_ID, TAG_DESC, TAG_SC, TAG_SCHED, TAG_HOUR, TAG_DAY, TAG_RM, TAG_ACAD},
                            new int[]{R.id.inst_id, R.id.desc, R.id.subj_code, R.id.sched, R.id.hour, R.id.day, R.id.room, R.id.acad});

                    mylistView.setAdapter(adapter);
                    mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    String DESC = ((TextView) (view.findViewById(R.id.desc))).getText().toString();
                                    String CODE = ((TextView) (view.findViewById(R.id.subj_code))).getText().toString();
                                    SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
                                    preferences.edit().putString("desc", DESC).commit();
                                    preferences.edit().putString("code", CODE).commit();
                                    Intent intent = new Intent(ClassRecord.this, SpecificClassRecord.class);
                                    startActivity(intent);

                        }
                    });
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_class_record, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                Intent intent = new Intent(ClassRecord.this, MainActivity.class);
                startActivity(intent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

您可以搜索数组值文本以查找您的朋友姓名或家人:

$description=array("PHP is a programming language");

如果strpos找到你的搜索针返回位置,如果没有找到则返回false: 如果检查错误,您可以使用

$result=strpos($description[0],"PHP");

array refrence
strpos refrence