Home > Mobile >  Putting PHP array values into new array
Putting PHP array values into new array

Time:11-06

I have this array structure:

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
            [0] => 3005874934
            [1] => 849804558
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
            [0] => 1146384946984079360
            [1] => 1006718040
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
            [0] => 322672692
            [1] => 590166675
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

Array
(
    [ids] => Array
        (
        )

    [next_cursor] => 0
    [next_cursor_str] => 0
    [previous_cursor] => 0
    [previous_cursor_str] => 0
    [total_count] => 
)

I'd like to take all of the values within the [ids] nested arrays and put them in a single separate array, so it looks like this:

Array
(
    [0] => 3005874934
    [1] => 849804558
    [2] => 1146384946984079360
    [3] => 1006718040
    [4] => 322672692
    [5] => 590166675
)

Here's my current attempt:

$retweeter_ids = array(); // create new empty array

foreach ($retweeters as $row) { // N.B. $retweeters contains the data in the first snippet
    if (!empty($row)) {
        $retweeter_ids[] = $row;
    }
}

However, this outputs in three separate arrays:

Array
(
    [0] => Array
        (
            [0] => 3005874934
            [1] => 849804558
        )

    [1] => Array
        (
            [0] => 1146384946984079360
            [1] => 1006718040
        )

    [2] => Array
        (
            [0] => 322672692
            [1] => 590166675
        )

)

Where am I going wrong?

CodePudding user response:

array_column will get everything from the ids columns, then just merge them (if they are empty it won't matter):

$retweeter_ids = array_merge(...array_column($retweeters, 'ids'));

To use your loop, just merge them:

$retweeter_ids = [];
foreach ($retweeters as $row) {
    $retweeter_ids = array_merge($retweeter_ids, $row['ids']);
}

CodePudding user response:

Not tested but $row is an array, so you might use

foreach ($retweeters as $row) { // N.B. $retweeters contains the data in the first snippet
    if (!empty($row)) {
        foreach ($row as $item)
            $retweeter_ids[] = $item;
    }
}

CodePudding user response:

In case you are using PHP 7.4 , you could solve it with a mix of array_merge and spread operator on array

https://www.php.net/manual/ro/function.array-merge.php https://wiki.php.net/rfc/spread_operator_for_array

Here is a sandbox example

$retweeters = [
    [
        'ids' => [
            3005874934,
            849804558
        ],
        'next_cursor' => 0,
        'next_cursor_str' => 0,
        'previous_cursor' => 0,
        'previous_cursor_str' => 0,
        'total_count' => 0
    ],
    [
        'ids' => [
            1146384946984079360,
            1006718040
        ],
        'next_cursor' => 0,
        'next_cursor_str' => 0,
        'previous_cursor' => 0,
        'previous_cursor_str' => 0,
        'total_count' => 0
    ]
];

$ids = array_merge(...array_map(function($set) {
    return $set['ids'] ?? [];
}, $retweeters));

print_r($ids);

This will result in:

Array
(
    [0] => 3005874934
    [1] => 849804558
    [2] => 1146384946984079360
    [3] => 1006718040
)
  • Related