doude1917 2016-04-13 08:34
浏览 21
已采纳

从Checkbox进入索引数组,然后从索引数组进入复杂? 排列

I am a newbie with coding and I have problem in figuring out my current problem.

Let me explain my situation first.

In my website, users agree to register with their Facebook account. With that, I am able to retrieve all the permitted data, save it in my database and display it in my website's user profile page. Here, the user default profile information is based on users' Facebook. In that page, user able to edit any of their Facebook information and save it (save into my database but will not change the their information in Facebook's site). My point here is, I have created the retrieve codes for displaying the saved data from the database and I do not wish to change that.

Right now, lets get into it. I have my textarea and I want user able to list out their listings in that textarea. With each line marked as list. I created an for it, so I use explode. So automatically it is set as index array right? Now, I tried to assign/associate each value with one static key so that it will ease my next step (Retrieving them. Why? I have created the default calling for them in the display page).

So here is my textarea:-

<textarea name="languages" placeholder="Your Language of Expertise... 1 Language per Line">
</textarea>

After user submitted, their input, it will go to my function page:-

$languages = explode("
", $_POST['languages']);

and the array ofcourse will look like this for example:-

Array ( 
[0] => Mandarin Chinese
[1] => English 
[2] => French 
[3] => Hokkien 
[4] => Arab
) 

I tried to make the array looks like this:-

Array ( 
[0] => Array ( [name] => Mandarin Chinese ) 
[1] => Array ( [name] => English ) 
[2] => Array ( [name] => French ) 
[3] => Array ( [name] => Hokkien ) 
[4] => Array ( [name] => Arab ) 
)

So with that, I can preserve my display callings function which is like this for example:-

$user_lang = Array ( 
[0] => Array ( [name] => Mandarin Chinese )
[1] => Array ( [name] => English )
)

foreach ($user_lang as $lang){
    echo $lang['name'];
}

I have tried many method such as using methods such with array_walk, array_merge, array_combine, array_flip, array_fill_keys, array_keys and combination of those. I don't know, maybe for all of those, I used it wrongly. I hope you guys can help me out here. And forgive me for my lack of brain.

  • 写回答

1条回答 默认 最新

  • dosf40815 2016-04-13 08:42
    关注

    You can iterate over the languages and transform each of them into associative arrays like this:

    foreach ($languages as &$language) {
        $language = [
            'name' => $language
        ];
    }
    

    The ampersand before $language passes the variable by reference. This means that when you change $language inside the foreach, the original array value is changed instead of just your newly created $language variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上