-1

I have this array. I want to get the array values to a same array,how can I achieve that?

Array
(
    [0] => Array
        (
            [referrer_id] => usr157
        )

    [1] => Array
        (
            [referrer_id] => usr42
        )

)

I want this array to be

array("usr157", "usr42")
  • 1
    I suggest [array_column](https://www.php.net/manual/en/function.array-column.php). But isn't this question a duplicate of https://stackoverflow.com/questions/5103640/how-to-extract-particular-fields-from-an-array? – bdecaf May 05 '22 at 05:34

3 Answers3

3

use array_walk_recursive to achieve the result as follows

<?php

$main = [];
$ref = 
    [
        [
            "referrer_id" => "usr157"
        ],
        [
            "referrer_id" => "usr42"
        ]
    ];
 
array_walk_recursive($ref, function ($item, $key) use(&$main) {
    $main[] = $item;
} );

print_r($main);

You can check that out here

Sandeep Dhakal
  • 268
  • 2
  • 10
  • 1
    A recursive technique is an oversized weapon for a task that PHP already has deliberately created a native function for. `array_column()` is all that is required here. – mickmackusa May 18 '22 at 06:18
2

You can just access the array components like this:

// The next line just recreates your example array into a variable called $x:
$x = array(array('referrer_id' => 'usr157'), array('referrer_id' => 'usr42'));

$result = array($x[0]['referrer_id'], $x[1]['referrer_id']);

print_r($result); //print the result for correctness checking

$result will be the output array you wanted.

Using $x[0], you refer the first element of your input array (and hence, $x[1] the second one, ...). Adding ['referrer_id'] will access its referrer_id key. The surrounding array(...) puts the values into an own array.

You can "automate" the whole thing in case you have a bigger input array using a loop.

ahuemmer
  • 1,020
  • 16
  • 21
0

You may use array_column to achieve that

$flatten = array_column($array, 'referrer_id');

You can also use array_map and array_values together.

$array = [
    [
        "referrer_id" => "usr157"
    ],
    [
        "referrer_id" => "usr42"
    ]
];

$flatten = array_map(function($item) {
    return array_values($item)[0];
}, $array);

var_dump($flatten);

Also you can use the one-liner if you're using latest version of php that support arrow function

$flatten = array_map(fn($item) => array_values($item)[0], $array);

Or without array_values, you may specify the key

$flatten = array_map(fn($item) => $item['referrer_id'], $array);

You can see the demo here

Jerson
  • 1,630
  • 2
  • 7
  • 14
  • If you aren't going to call native function `array_column()`, then iterated calls of `current()` will be more brief than `array_values()[0]`. https://3v4l.org/ScEiH Most importantly: [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa May 18 '22 at 06:22