0

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

Cody
  • 7
  • 3
  • Possible duplicate of [PHP add elements to multidimensional array with array\_push](https://stackoverflow.com/questions/16308252/php-add-elements-to-multidimensional-array-with-array-push) – dns_nx Jan 03 '19 at 13:57
  • https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values – Zeljka Jan 03 '19 at 13:57
  • Is there a specific reason you're not using objects instead? Multidimensional array is a little outdated – Joppe De Cuyper Jan 03 '19 at 13:57
  • @JoppeDeCuyper Because from a array I can generate a csv file – Cody Jan 03 '19 at 14:08
  • @dns_nx I have already checked that one out, but I don't seem to find it helpful same as regards to Zeljika – Cody Jan 03 '19 at 14:09
  • You should definitely check out objects, for example a Product object, in that object you can implement a `getCsvData` function, returning a serialized version for storage – Joppe De Cuyper Jan 03 '19 at 14:12
  • @Cody Did my post helped you? – dWinder Jan 10 '19 at 08:36
  • @DavidWinder Before I got your answer I reconsidered my approach to my application, I re-did a lot of it. I've found out that I should have been thinking in a different way regards building my application. So unfortunately I did not even try out your answer. But I do really appreciate your answer maybe someone in the future does have use of your answer, since my question still is valid. – Cody Jan 11 '19 at 14:20

1 Answers1

0

I may be missing something but I believe you can remove the second for loop in which you assign the extra value to your $csv elements and do it in the same loop - even for more values.

Consider the following:

foreach ($csv as $key => $row) {
  $purchasePrice = str_replace(',', '.', $row[1]);

  $object = new Product($purchasePrice, 10, 21);
  $csv[$key][] = $object->margePercentage;
  $csv[$key][] = $object->getValue2(); // or whatever function needed for get your value2
}

If you want you can also use & as foreach($csv as &$row) and then just add element to row as $row[].

Hope that helps!

dWinder
  • 11,597
  • 3
  • 24
  • 39