I am going to try to explain what I am trying to accomplish before sharing the code so you will understand. I am trying to add multiple values to a multidimensional array. So I have one multidimensional array and want to add more than one value to each array in the multidimensional array. Currently, I can add one value by creating another array, and adding the array's together. I have looked up multiple answers, but they don't work with my loops and I don't understand how to make them work in my application, and some of them only explain it with adding one value, not multiple.
Code(PHP):
foreach ($csv as $row) {
$purchasePrice = str_replace(',', '.', $row[1]);
$object = new Product($purchasePrice, 10, 21);
$calculatedValues[]= $object->margePercentage;
}
foreach($calculatedValues as $key => $val){ //Adds the 2 array's together
$csv[$key][] = $val;
}
The output of this code is:
Array
(
[0] => Array
(
[0] => id
[1] => eur
[2] => title
[3] => marge
)
[1] => Array
(
[0] => 25200
[1] => 30,74
[2] => Humming teddy bear, verbetert kwaliteit van slaap
[3] => 19.261617724033
)
[2] => Array
(
[0] => 25201
[1] => 30,74
[2] => Humming teddy bear, verbetert kwaliteit van slaap, circa 45cm
[3] => 19.261617724033
)
[3] => Array
(
[0] => 25202
[1] => 26,06
[2] => Humming teddy bear, verbetert kwaliteit van slaap, circa 28x28cm
[3] => 18.567221241214
)
As you can see, it works fine with just 1 value. The foreach creates a new object for each $row[1]
(price) and calculates a percentage which is not relevant but happens in the product object. Then I add the value to $calculatedValues[]
.
Now I want to add multiple values and not just one($object->margePercentage
). I would like to have an output like this:
Array
(
[0] => Array
(
[0] => id
[1] => eur
[2] => title
[3] => -2.6572
[4] => value2
)
[1] => Array
(
[0] => 25200
[1] => 30,74
[2] => Humming teddy bear, verbetert kwaliteit van slaap
[3] => 19.261617724033
[4] => value2
)
[2] => Array
(
[0] => 25201
[1] => 30,74
[2] => Humming teddy bear, verbetert kwaliteit van slaap, circa 45cm
[3] => 19.261617724033
[4] => value2
)
[3] => Array
(
[0] => 25202
[1] => 26,06
[2] => Humming teddy bear, verbetert kwaliteit van slaap, circa 28x28cm
[3] => 18.567221241214
[4] => value2
)
value2
in this instance is just to show where I would like to have the value be in the array, value2 will be another calculation which is made in the product object.
I hope I can find a solution to this since I am stuck quite a while now, in the meantime I am going to study more about arrays.
If there are any more questions regarding my code or explanation, please feel free to ask me.
Thanks again for reading my question.
Cheers Cody