【问题标题】:string to array return array in parameter字符串到数组返回参数中的数组
【发布时间】:2015-04-29 08:34:18
【问题描述】:

我得到一个类似这样的字符串,"voornaam, achternaam" 我把它交给一个函数。

当我使用字符串从数据库中获取数据时。

mysql_query("SELECT."$string". FROM ...")

一切顺利。

现在。我想归还我从数据库中选择的值。

我试过这样的。

<?php
function user_info($user_id, $fetch = "")
{
if (ctype_digit($user_id))
{
    if (empty($fetch))
    {
        $fetch = "*";
    }
    $query = mysql_query("SELECT ".$fetch." FROM accounts WHERE ID = '".$user_id."'");
    $data = mysql_fetch_assoc($query);
    $data['password'] = NULL;
}

if (empty($fetch))
{
    return $data;
}
else
{
    $fetch = explode(", ", $fetch);
    print_r($fetch);
    while($param = $fetch) {
        return $data[$param];
    }
}

//$item_result['created_by'] gives back a digit
user_info($item_result['created_by'], 'voornaam, achternaam')

我陷入了 while 循环。
我在循环中并不是那么好。
所以我尝试了一些东西,对我来说这似乎是合乎逻辑的。
但在某些方面它不会起作用。

【问题讨论】:

  • 请向我们展示您的完整真实代码,以便我们了解您如何使用哪个 API 等获取数据。你还卡在哪里?
  • @Rizier123 已更改
  • 你从哪里得到$param
  • 但我仍然没有真正看到你卡在哪里/什么没有按你的意愿工作?
  • @sgtBOSE 我想它是从天上掉下来的 :)

标签: php arrays string parameters


【解决方案1】:

尝试改变

  while($param = $fetch) {
    return $data[$param];
}

在 foreach 循环中:

$temp = array();
foreach($featch as $key){
  $temp[$key] = (empty($data[$key]) ? ''  :  $data[$key] );
}
return $temp;

更新

或者你可以使用 for 循环:

$temp = array();
for( $i =0; $i < count($fetch); $i++){
  $temp[$fetch[$i]] = (empty($data[$fetch[$i]]) ? ''  :  $data[$fetch[$i]] );
}
return $temp;

更新 2

OR while 循环

$temp = array();
while(count($fetch)){
  $key = array_shift($fetch);
  $temp[$key] = (empty($data[$key]) ? ''  :  $data[$key] );
}
return $temp;

【讨论】:

  • 我的老板不会使用 foreach。如果由我来决定我已经使用了 foreach
  • @Lenap 抱歉,但我不明白你想说什么。循环的可能性很多。
  • 我正在尝试这些事情之一;)
  • 使用了 foreach 循环。
  • 嗯..我先用了一个forloop然后foreach
【解决方案2】:

它正在工作!

我现在拥有的是:

$fetch = explode(", ", $fetch);
    $temp = array();
    for ($i = 0; $i < count($fetch); $i++) {
        $temp[$fetch[$i]] = (empty($data[$fetch[$i]]) ? '' : $data[$fetch[$i]]);
    }
    foreach ($temp as $data) {
        echo $data . ' ';
    }

【讨论】:

    【解决方案3】:

    问题在 -

    $fetch = explode(", ", $fetch);
    print_r($fetch);
    while($param = $fetch) {
        return $data[$param];
    }
    

    $fetch 现在是一个数组,$param 没有定义。

    你可以试试这个——

    $fetch = explode(", ", $fetch);
    $i = 0;
    $newData = array();
    while($i < count($fetch)) {
        $newData[$fetch] = $data[$fetch];
        $i++;
    }
    return $newData;
    

    我建议使用foreach 而不是while

    【讨论】:

    • return $data[$fetch]; - 将只返回一个值,而不是所有来自 $fetch 数组的值
    【解决方案4】:

    如果它必须处于 while 循环中,您也可以使用以下命令

    $data=array();
    while (list($var, $val) = each($fetch)) {
            $data[$var]=$val;
    }
    

    【讨论】:

    • 来自有问题的代码 = $data - 是来自 mysql 的数组,而 $fetch 是应该返回的键。所以这不是安静的正确代码
    • 这是另一种方法的示例。 SO 不是代码编写服务,您可以阅读并根据自己的代码调整解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多