在数组会话中存储值 - 仅存储最新值的数组

时间:2017-08-23 14:48:26

标签: php arrays

@Override
protected void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return true;
        }
    });
}

会发布类似

的内容
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view!!.setOnTouchListener { view, motionEvent -> return@setOnTouchListener true }
}

$user = hobaa; $usernames = array(); $usernames['name'] = $user; print_r($usernames['name']); 会发出Array ( [name] => hobaa )

如何保存多个值?

尝试

print_r($usernames);

但它只取最后一个值。

请帮忙。谢谢!

3 个答案:

答案 0 :(得分:1)

从这段代码:

foreach($users as $user) {
    $usernames = array();
    $usernames['name'][] = $user;
   }

从循环中删除此行:

$usernames = array();

并将其置于循环之上,如:

$usernames = array();
foreach($users as $user) {
    $usernames['name'][] = $user;
   }

然后再试一次。因为每次迭代都是re-initializing数组。

答案 1 :(得分:0)

类似的东西:

$users = array("hobaa","test");
// a new usernames array to use
$usernames = array();
// 
foreach($users as $user) {
    array_push($usernames, $user);
}

print_r($usernames);

会给:

Array ( [0] => hobaa [1] => test ) 

答案 2 :(得分:0)

在循环外定义$usernamse变量。使用以下代码:

$users = array("hobaa","test");
$usernames = array();
foreach($users as $user) {
    $usernames['name'][] = $user;
}
print_r($usernames['name']);