duanchuopi2298 2014-06-18 16:35
浏览 26

从db字段中提取部分数据并创建多个变量

I'm converting MySQL data from one system to another, and the database structure is not quite the same. One database field contains multiple parameters in a format like this:

option1=value1
option2=value2
option3=value3

Yes, it is a text field with line breaks. I need to get the data (using PHP) from the one field and make variables out of it. For example, I need "option1=value1" converted to a variable $option1 that contains the value "value1".

I have pulled the data of this field into an array and tried imploding that data into a string that I could make variables from... but have had no luck. Either I wasn't doing it right or that's not the right direction to go.

I was trying something like this:

$string = implode("&",$array);
echo $string;

...but I just get a bunch of "Notice: Array to string conversion" messages along with an echo of "&Array" over and over. Help appreciated!

UPDATE: For further explanation, I've realized that I need a two level array with the user ID (pulled from the same table as the option values noted above) in one array... then each of those IDs having a nested array containing the option values.

  • 写回答

1条回答 默认 最新

  • douqi1212 2014-06-18 16:45
    关注

    From your description

    option1=value1
    option2=value2
    option3=value3
    

    isn't an array, but a text field with line breaks. You should do

    $myarray=explode("
    ",$textfield);
    

    to get a proper array.

    From then on, your attempted solution seems to rely in converting that array into a query string you will pass with a GET request to another page. For that matter, you can implode the array using & as the glue.

    However you also said that you need to convert array items into variables. For that purpose, you can create a new array in the form

    $assocarray=[];
    foreach($myarray as $item) {
      $keyvalue=explode('=',$item);
      $assocarray[$keyvalue[0]]=$keyvalue[1];
    }
    

    which will result in $assocarray being

    Array [
        'option1' => value1,
        'option2' => value2,
        'option3' => value3,
    ];
    

    So you could perform

    extract($assocarray);
    

    to get 3 variables called $option1, $option2 and $option3 in the global namespace.

    Edit: I see that, for some reason, you have nested arrays. Something like

    $myarray = Array [
                      Array [ 
                         "access_optin=2", 
                         "", 
                         ""
                      ],
                      Array [
                         "show_bio=1",  
                         "show_url=1", 
                         "show_picture=1", 
                         "show_organization=1", 
                         "access_optin=2", 
                         "access_jobtitle=0",  
                         "access_email=0"
                      ]
                ];
    

    and the iteration should be

    $assocarray=[];
    foreach($myarray as $subarray) {
        foreach($subarray as $item) {
            $keyvalue=explode('=',$item);
            if(count($keyvalue)==2) $assocarray[$keyvalue[0]]=$keyvalue[1];
        }
    }
    

    Note that I'm validating the size of the $keyvalue array, because some not every value will have the name=value pattern.

    Second Edit:

    Since each $subarray represents a different user, you need to store the parsed key/value pair in a subarray too.

    $assocarray=[];
    foreach($myarray as $user => $subarray) {
        foreach($subarray as $item) {
            $keyvalue=explode('=',$item);
            if(count($keyvalue)==2) $assocarray[$user][$keyvalue[0]]=$keyvalue[1];
        }
    }
    

    This assumes your $user matches the user_id you need to set. Otherwise, you'll have to go a bit backwards to where you give form to the $myarray variable. I'm afraid you'll need to, because your example starts with zero and I bet your user_id doesn't.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计