0

I have a function (getSSOrders) that grabs data from an API. Since there is pagination, I am trying to get all of the order data from all pages. Right now my code adds an index to the array. Is there a way to just add the data to the $array_data without adding an index?

$page_counter = 1;
while ($page_counter <= $ss_pages) {
    $ss_data = getSSOrders($page_counter);
    $array_data[] = $ss_data['orders'];
    $page_counter++;
}

Edit: Here is how I want the output:

Array
(
    [0] => Array
        (
            [orderId] => 28058625
            [orderNumber] => GS50340
            [orderKey] => 92452700
        )

    [1] => Array
        (
            [orderId] => 28316205
            [orderNumber] => GS50392
            [orderKey] => 92511383
        )
)

But with the while loop, I am getting:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [orderId] => 28058625
                    [orderNumber] => GS50340
                    [orderKey] => 92452700
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [orderId] => 28316205
                    [orderNumber] => GS50392
                    [orderKey] => 92511383
                )
        )
)
8
  • 3
    What do you mean by "without adding an index"? Can you show a sample of the result you're getting and the desired result? Commented Mar 23, 2020 at 0:10
  • 1
    No, each Array element is indexed by a number (or String in Associative Arrays). If you want to empty that array while maintaining reference array_splice($array_data, 0);. Commented Mar 23, 2020 at 0:13
  • Arrays in PHP must have an index, either numeric or text. In your example, you will automatically get a numeric index with $array_data[] = $ss_data['orders'];. I am not sure why you would want an array without an index, maybe a little clarification in your question may help. Commented Mar 23, 2020 at 0:14
  • You mention associative array in the title only, can you add more details about that requirement. Commented Mar 23, 2020 at 0:15
  • How many elements can $ss_data['orders'] have? Commented Mar 23, 2020 at 0:21

1 Answer 1

1

You may use array_merge.

Init $array_data = [] before the loop and:

$array_data = array_merge($array_data, $ss_data['orders']);
Sign up to request clarification or add additional context in comments.

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.