Questions tagged [array-column]

The array_column() return the values from a single column in the input array.

$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    )
);
$first_names = array_column($records, 'first_name');
print_r($first_names);

The above example will output:

Array
(
    [0] => John
    [1] => Sally
)
102 questions
87
votes
7 answers

Return single column from a multi-dimensional array

I'm a novice at PHP and I need a quick solution to the following problem but can't seem to come up with one: I have a multi-dimensional array like so Array ( [0] => Array ( [blogTags_id] => 1 [tag_name] => google …
CobaltBabyBear
  • 2,188
  • 6
  • 26
  • 47
78
votes
18 answers

How to check if a specific value exists at a specific key in any subarray of a multidimensional array?

I need to search a multidimensional array for a specific value in any of the indexed subarrays. In other words, I need to check a single column of the multidimensional array for a value. If the value exists anywhere in the multidimensional array, I…
Rob
  • 899
  • 1
  • 6
  • 8
35
votes
8 answers

Convert array of single-element arrays to a one-dimensional array

I have this kind of an array containing single-element arrays: $array = [[88868], [88867], [88869], [88870]]; I need to convert this to one dimensional array. Desired output: [88868, 88867, 88869, 88870] Is there any built-in/native PHP…
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
24
votes
15 answers

Create a comma-separated string from a single column of an array of objects

I'm using a foreach loop to echo out some values from my database, I need to strip the last comma from the last loop if that makes sense. My loop is just simple, as below foreach($results as $result){ echo $result->name.','; } Which echos…
Cecil
  • 1,609
  • 5
  • 21
  • 26
12
votes
3 answers

Implode a column of values from a two dimensional array

I've got an array like this: Array ( [0] => Array ( [name] => Something ) [1] => Array ( [name] => Something else ) [2] => Array ( [name] => Something…
qwerty
  • 575
  • 3
  • 6
  • 8
10
votes
4 answers

Get array values from specific column in a multidimensional array

The array looks like $arr = array( array('a', 'b'), array('c', 'd'), array('e', 'f'), ) And I want to get an array with values from the first column, like array('a', 'c', 'e') I know it can easily be done by iterating the array and store…
thelolcat
  • 10,995
  • 21
  • 60
  • 102
7
votes
5 answers

How to average columns of data from multiple, flat arrays?

Let's say I have 4 arrays with the same amount of values in each: $array1 = array(0, 7, 5, 0); $array2 = array(2, 6, 10, 0); $array3 = array(4, 8, 15, 10); $array4 = array(6, 7, 20, 10); I want to count the average for all 4 of these arrays for…
KarelRuzicka
  • 461
  • 2
  • 13
7
votes
5 answers

Concatenate two columns into one using array_column

I have an array of type $records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Cena', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', )); I want to display the output in the…
Bibek Aryal
  • 545
  • 9
  • 28
6
votes
4 answers

Replace first level array keys using values from a specific column

I have an array like this $users = array( [0] => array('Id' => 3, 'Name' => 'Bob'), [1] => array('Id' => 8, 'Name' => 'Alice'), ) and I want to pull the Ids 'up' one level so that the final array is: $usersById = array( [3] =>…
Harper
  • 1,285
  • 3
  • 15
  • 35
6
votes
3 answers

Access array column by integer instead of associative key name

How exactly can I apply array_column() to always get the first column of an array instead of getting the column by name? I am seeking something like this is: array_column($array,[0]) instead of: array_column($array,"key");
Fane
  • 1,978
  • 8
  • 30
  • 58
4
votes
4 answers

In php is there a function like array_column for multidimensional arrays

Is there function that works similar to array_column for multidimensional arrays? Is there a function that translates the first array below to the second: Array ( [0] => Array ( [foodType] => fruits [itemID] =>…
jcropp
  • 1,236
  • 2
  • 10
  • 29
3
votes
1 answer

array_column returning empty array in PHP 5.6

In a production environment that uses the Yii1 framework and PHP 5.6.40, the array_column function is returning an empty array. The array is a list of CActiveRecords from another CActiveRecord's HAS_MANY relation. On my local machine (PHP 7.1.23),…
hutch90
  • 341
  • 3
  • 15
3
votes
3 answers

Sorting by several array columns with usort

I am aware of the fact that I can usort an array by one column using something like this: function cmp($a, $b) { return $b['column'] - $a['column']; } usort($array, "cmp"); This simulates the ORDER BY column DESC. What if I want to simulate…
Keaire
  • 879
  • 1
  • 11
  • 30
3
votes
5 answers

Count unique values in a column of an array

I have an array like this: $arr = [ 1 => ['A' => '1', 'C' => 'TEMU3076746'], 2 => ['A' => '2', 'C' => 'FCIU5412720'], 3 => ['A' => '3', 'C' => 'TEMU3076746'], 4 => ['A' => '4', 'C' => 'TEMU3076746'], 5 => ['A' => '5', 'C' =>…
Fadly Dzil
  • 2,154
  • 3
  • 34
  • 85
3
votes
5 answers

Comparing two array columns in Scala Spark

I have a dataframe of format given below. movieId1 | genreList1 | genreList2 -------------------------------------------------- 1 |[Adventure,Comedy] |[Adventure] 2 |[Animation,Drama,War] |[War,Drama] 3 …
1
2 3 4 5 6 7