简体   繁体   中英

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).

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. IE $mfg = $data[0] (mfg...the first field in my datafeed)

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. 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;

$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.

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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