1

I have an array that is built as follows

foreach($details as $data)
{
    $loans[] = [
            'name' => 'Details',
            [
                'name' => 'id',
                'value' => 1
            ],
            [
                'name' => 'Date',
                'value' => '2015'
            ],
            [
                'name' => 'Purpose',
                [
                    'name' => 'Code',
                    'value' => 123
                ]
            ],
            getFees($data)
        ];
}

I need to use the fees from the getFees function and have those values set at the same level as the 'name' => 'Details' above.

private function getFees($data)
{
    foreach($data as $item){
        $values[] = [
            'name' => 'type',
            'value' => 'Interest'
        ];
    }
    return $values;
}

So the above produces the following:

Array
(
    [name] => Details
    [0] => Array
    (
        [name] => Details
        [0] => Array
            (
                [name] => id
                [value] => 1
            )

        [1] => Array
            (
                [name] => Date
                [value] => 2015
            )
         [2] => Array
            (
                [name] => Purpose
                [0] => Array
                 (
                      [name] => Code 
                      [value] => 123
                 )
            )
          [3] => Array
            (
                [0] => Array
                    (
                        [name] => Type
                        [value] => Interest

                    )
            )
      )
 )

But I don't want that [3] array to be another level deeper, I simply want:

 Array
(
    [name] => Details
    [0] => Array
    (
        [name] => Details
        [0] => Array
            (
                [name] => id
                [value] => 1
            )

        [1] => Array
            (
                [name] => Date
                [value] => 2015
            )
         [2] => Array
            (
                [name] => Purpose
                [0] => Array
                 (
                      [name] => Code 
                      [value] => 123
                 )
         [3] => Array
           (
                 [name] => Type
                 [value] => Interest
           )          
     )
 )

Clearly I'm missing something - if i try to do array_merge, outside the loop, then the fees array is outside the Details array.

If the getFees function only sets the array to one single element, then it works fine, but I need to allow for multiple elements, hence the $values[] assignment.

How do i produce the array as required above ?

1 Answer 1

1

The way you've currently written it, getFees() would have to return multiple values in order to achieve the desired array. Since a function can only return one thing, you'll need to rewrite the code.

I recommend you take the getFees() call out of your $loans array, and instead call it afterwards, merge the results with the $loans array.

foreach($datum as $data){
    $temp = array(
        // your data
    );

    $temp = array_merge($temp, getFees($data));

    $loans[] = $temp;
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but i forgot to add the code (i've edited now) to show that the initial assignment of loans is in a foreach loop, so i can't put it outside, otherwise the resultant array contains the fees data outside the specific array element it should be in .. hope that makes sense
The answer still stands. Since getFees() is returning an array, and you want that arrays elements to be at the end of the loans array, you simply merge the two.
Ahh... now I understand. You need a third variable (temp).

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.