简体   繁体   English

PHP如何将一个数组的值赋给另一个数组中的变量

[英]How to assign the values of one array to the variables in a different array in PHP

EDIT: Here is the working solution after getting help from Ricardo.编辑:这是从里卡多获得帮助后的工作解决方案。

$headers_as_variables = str_replace(TEXT_DELIMITER, ",", $headers);
$headers_as_variables = explode(',', $headers_as_variables); //split string into array seperated by ', '
$i = 0;
foreach($headers_as_variables as $column_header) {
    $varName = $headers_as_variables[$i];
    $$varName = $data[$i];
    $i = $i + 1;
}

I am trying to develop a program that imports datafeeds from various suppliers...using XAMPP on Windows 10 (1909).我正在尝试开发一个程序,该程序可以从各种供应商处导入数据馈送……在 Windows 10 (1909) 上使用 XAMPP。

Essentially, I'm trying to turn the first row of a datafeed into variable names, then assign values to each variable from the rows/records that follow the header row.本质上,我试图将数据馈送的第一行转换为变量名称,然后从标题行后面的行/记录中为每个变量赋值。

My existing code for one supplier works but table headers vary from supplier to supplier...thus my attempt to automate.我现有的一个供应商代码有效,但表头因供应商而异......因此我尝试自动化。

Here is my working code...based on one supplier.这是我的工作代码......基于一个供应商。 The variable names in "list()" are the same as the header row in the datafeed. “list()”中的变量名称与数据馈送中的标题行相同。 IE $mfg = $data[0] (mfg...the first field in my datafeed) IE $mfg = $data[0](mfg...我的数据馈送中的第一个字段)

list($mfg,$sku,$model,$name,$desc,$img,$weight,$cost,$map,$msrp,$shipping,$zip,$qty,$tags,$length,$height,$width,$pqty,$cat,$catpath,$added,$freq,$lead,$eta,$updated,$url) = $data;

//$mfg = $data[0];$sku = $data[1];$model = $data[2];$name = $data[3];$desc = $data[4];$img = $data[5];$weight = $data[6];$cost = $data[7];$map = $data[8];$msrp = $data[9];$shipping = $data[10];$zip = $data[11];$qty = $data[12];$tags = $data[13];$length = $data[14];$height = $data[15];$width = $data[16];$pqty = $data[17];$cat = $data[18];$catpath = $data[19];$added = $data[20];$freq = $data[21];$lead = $data[22];$eta = $data[23];$updated = $data[24];$url = $data[25];

If push comes to shove, I can make a table with row headers for each supplier, then grab the data according to whose datafeed I am downloading.如果迫在眉睫,我可以为每个供应商制作一个带有行标题的表格,然后根据我正在下载的数据源获取数据。 But I think there might a be a simpler solution using PHP...I just don't see it.但我认为使用 PHP 可能有一个更简单的解决方案......我只是没有看到它。 Besides, what happens if the supplier drops or adds a column...my code will be broken.此外,如果供应商删除或添加列会发生什么...我的代码将被破坏。

Here is what I have tried:这是我尝试过的:

echo $headers_as_variables;

$mfg, $sku, $model, $name, $desc, $img, $weight, $cost, $map, $msrp, $shipping, $zip, $qty, $tags, $length, $height, $width, $pqty, $cat, $catpath, $added, $freq, $lead, $eta, $updated, $url

I tried using this as an array with my working list() funuction;我尝试使用它作为我的工作 list() 函数的数组;

$variable_array = array($mfg,$sku,$model,$name,$desc,$img,$weight,$cost,$map,$msrp,$shipping,$zip,$qty,$tags,$length,$height,$width,$pqty,$cat,$catpath,$added,$freq,$lead,$eta,$updated,$url)

list($variable_array) = $data;

That resulted in a WHITE SCREEN OF DEATH.这导致了死机白屏。 LOL.哈哈。

Next, I converted the row into an array and set both arrays equal to each other.接下来,我将行转换为数组并将两个数组设置为彼此相等。

$headers_as_variables = explode(',', $headers_as_variables);

$var_dump($headers_as_variables);

 array(26) { [0]=> string(4) "$mfg" [1]=> string(5) " $sku" [2]=> string(7) " $model" [3]=> string(6) " $name" [4]=> string(6) " $desc" [5]=> string(5) " $img" [6]=> string(8) " $weight" [7]=> string(6) " $cost" [8]=> string(5) " $map" [9]=> string(6) " $msrp" [10]=> string(10) " $shipping" [11]=> string(5) " $zip" [12]=> string(5) " $qty" [13]=> string(6) " $tags" [14]=> string(8) " $length" [15]=> string(8) " $height" [16]=> string(7) " $width" [17]=> string(6) " $pqty" [18]=> string(5) " $cat" [19]=> string(9) " $catpath" [20]=> string(7) " $added" [21]=> string(6) " $freq" [22]=> string(6) " $lead" [23]=> string(5) " $eta" [24]=> string(9) " $updated" [25]=> string(6) " $url " }

var_dump($data);

array(26) { [0]=> string(3) "mfg" [1]=> string(3) "sku" [2]=> string(5) "model" [3]=> string(4) "name" [4]=> string(4) "desc" [5]=> string(3) "img" [6]=> string(6) "weight" [7]=> string(4) "cost" [8]=> string(3) "map" [9]=> string(4) "msrp" [10]=> string(8) "shipping" [11]=> string(3) "zip" [12]=> string(3) "qty" [13]=> string(4) "tags" [14]=> string(6) "length" [15]=> string(6) "height" [16]=> string(5) "width" [17]=> string(4) "pqty" [18]=> string(3) "cat" [19]=> string(7) "catpath" [20]=> string(5) "added" [21]=> string(4) "freq" [22]=> string(4) "lead" [23]=> string(3) "eta" [24]=> string(7) "updated" [25]=> string(3) "url" } 

These both look as they should with the exception of double quotes around variable names within the array.除了数组中变量名周围的双引号外,它们看起来都应该如此。 I have removed them with string replace and still can't get my field values from another array ($data) to attach to them.我已经用字符串替换删除了它们,但仍然无法从另一个数组 ($data) 中获取我的字段值以附加到它们。

So, I thought simply matching the two arrays by number identifiers would work...but it doesn't.所以,我认为简单地通过数字标识符匹配两个数组会起作用……但事实并非如此。

$i = 0;
foreach($headers_as_variables as $column_header) {
    //$headers_as_variables[$i] = str_replace("\"", "", $headers_as_variables[$i]); //Removed double-quotes

    $headers_as_variables[$i] = $data[$i]; // $mfg = mfg ... one would think?
    echo $headers_as_variables[$i] . '='. $data[$i] . '<br>';  // Output: $mfg = mfg
    echo 'MFG - ' . $mfg . '<br><br>'; // Result: MFG - 
    $i++;
}

So I thought variable variables would do the trick.所以我认为可变变量可以解决问题。

$i = 0;
foreach($headers_as_variables as $column_header) {
    $a = $headers_as_variables[$i];
    $$a = $data[$i];
    echo "$a ${$a}" . "<br>"; // Output: $mfg mfg
    echo 'MFG - ' . $mfg . '<br><br>'; // Output: MFG -
    $i = $i + 1;
}

One more try...combine the arrays and us key/values to get what I need.再试一次……组合数组和我们的键/值来获得我需要的东西。 Unfortunately, I'm quite the beginner when it comes to this one...even after reading for a couple of days.不幸的是,当涉及到这个问题时,我还是个初学者……即使在阅读了几天之后。

$a = array($headers_as_variables);
$b = array($data);
$c = array_combine($a, $b);

print_r($c);

//Array ( [Array] => Array ( [0] => mfg [1] => sku [2] => model [3] => name [4] => desc [5] => img [6] => weight [7] => cost [8] => map [9] => msrp [10] => shipping [11] => zip [12] => qty [13] => tags [14] => length [15] => height [16] => width [17] => pqty [18] => cat [19] => catpath [20] => added [21] => freq [22] => lead [23] => eta [24] => updated [25] => url ) ) 

That array looks very similar to var_dump($data) (minus all the string and text stuff).该数组看起来与 var_dump($data) 非常相似(减去所有字符串和文本内容)。 Now I feel like I'm going in circles.现在我觉得我在兜圈子。

You can use associative hash variables and arrays to push values:您可以使用关联哈希变量和数组来推送值:

$varName="msg";
$varName2="cat";

$List[$varName][] = "Value 1 of varName";
$List[$varName][] = "Value 2 of varName";
$List[$varName2][] = "Value 1 of varName2";
$List[$varName2][] = "Value 2 of varName2";

echo $List["msg"][0];  //Value 1 of varName
echo $List["cat"][1];  //Value 2 of varName2

Or use double $ to assign the variable value to make a new variable:或者使用双 $ 给变量赋值来创建一个新的变量:

$varName="msg";
$$varName="Hello";

echo $msg; //Hello

Or mix it up:或者混合起来:

$varName="msg";
$$varName[]="Hello";
$$varName[]="World";

echo $msg[0]." ".$msg[1]; //Hello World

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM