2

I have one dynamic array object which is store a value like this.

foreach($salesReturn as $salesReturns)
    {
        echo '<pre>';
        print_r($salesReturns);
    }

O/P :-

stdClass Object
(
[date] => 2
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 228.95
)
stdClass Object
(
[date] => 3
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 842.55
)
stdClass Object
(
[date] => 4
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 129.33<
)

But I want to store a value in array object like this.

stdClass Object
(
[date] => 1
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 2
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 228.95
)
stdClass Object
(
[date] => 3
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 842.55
)
stdClass Object
(
[date] => 4
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 129.33
)
stdClass Object
(
[date] => 5
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 6
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 7
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 8
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 9
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 10
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 11
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)
stdClass Object
(
[date] => 12
[tax1] => 0.00
[tax2] => 0.00
[totalReturn] => 0.00
)

6
  • [date] is in a months... Commented Apr 21, 2016 at 5:55
  • you mean to have the [date] property start from 1 instead of 2? Commented Apr 21, 2016 at 6:46
  • I return a [date]=2 and its [totalReturn]=228.95... In other [date] like 3 & 4 have a [totalReturn] but I want to store [totalReturn] in other [date] like 1,5,6,... by default 0.00 Commented Apr 21, 2016 at 6:51
  • upto [date]=12.... Commented Apr 21, 2016 at 6:52
  • you want the code that will create the wanted array of $salesReturn in a hard coded manner? Commented Apr 21, 2016 at 6:53

2 Answers 2

1

What you want is filling the array with "empty" default objects ?

$defaults = [];
foreach (range(1, 12) as $m) {
    // using non numerical index to avoid reindexing behaviours...
    $defaults['m' . $m] = new stdClass;
    ...
    // init default obj
    $defaults['m' . $m]->date = $m;
    $defaults['m' . $m]->otherFields = 0;
    //add all fields
}

Next index your current data array by month too with the same index format.

$indexedSalesReturn = [];
foreach ($salesReturn as $obj) {
     $indexedSalesReturn['m' . $obj->date] = $obj;
}

Then append default values to current data.

$indexedSalesReturn += $defaults;

This will NOT replace existing index but add non existing.

Optional : ksort the array and array_values

Edit : You should aim for this working array (pseudo code)

[
  'm1' => stdClass { date => 1, ...},
  'm2' => stdClass { date => 2, ...},
  ...
]
Sign up to request clarification or add additional context in comments.

4 Comments

Replace $myData by $salesReturn
You need to index your array with a string derived from the month as wxplain
You can copy and modify this array if you need :p
The first loop create an array filled with empty default objects just with the correct month
0

to shift the [date] property by 1 on all the objects:

foreach($salesReturn as $salesReturns)
    {
        echo '<pre>';
        $salesReturns->data = $salesReturns->data - 1;
        print_r($salesReturns);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.