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
0
votes
2 answers

Get max value from each column of a 2-dimensional array

I have an array within an array, for example: [ [0, 20, 5], [5, 0, 15], [5, 10, 0] ] I need to get the max number in each column. The max of [0 , 5 , 5] is 5, so that goes into the result array. The max of [20, 0, 10] is 20, so that…
Kamal
  • 363
  • 8
  • 25
-1
votes
1 answer

how to match and add column in the multi-dimensional array in PHP

we have to multi-dimensional array and we need to check some value of every element from array 1 to array two on a specific element key. we can use following code for handle this types of complex problems "Sonu",…
-1
votes
2 answers

Use array column to generate new array's keys and values

I have an array of associative arrays like this: $array = [ ["foobar" => "asd"], ["foobar" => "abvc"], ["foobar" => "test123"], ]; I would like to have the column values used as keys and values in the end result. [ 'asd' => 'asd', …
-1
votes
1 answer

Postgres db design Normalize tables or Use Array Columns

Newbie trying to figure out the best way to design a Postgres db for the following use case scenario. There is an Account table for the business customers and there is a contacts table with a column relationship. account.pk_id, …. contacts.pk_id,…
-1
votes
4 answers

How to save a single column from a multidimensional array as a .csv file?

The following code exports the whole array into the .csv file, but I only want to export a specific column.
-1
votes
4 answers

Count number of non-null values in each column of a 2d array

Let's say I have an array: [ {0:1, 1:5, 3:null}, {0:4, 1:null, 3:null}, {0:null, 1:5, 3:5} ] I want to count non-null values per associative key so that my expected output is: [{0:2, 1:2, 3:1}]
-2
votes
2 answers

php multidimensional array get prices in specific category

[0] => Array ( [id] => 004002718 [price] => 5.00 [category] => x ) [1] => Array ( [id] => 030285882 [price] => 8.99 [category] => y ) [2] => Array ( [id] => 040685111 …
-2
votes
3 answers

Calculate average of all values in a specific column of a 2d array

I need to find the average of all AdjClose values in my two dimensional array. I have an array stored in a variable $data. It looks like this: ["data"]=> array(22) { [0]=> object(stdClass)#234 (7) { ["Date"]=> string(10)…
Alan
  • 213
  • 1
  • 13
-2
votes
2 answers

Sum a column of data in a 2d array

How can I get the addition of the total_qty_ordered of all the arrays? This can be any number of arrays Array ( [0] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [1] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [2] => Array (…
Ricky Sharma
  • 1,839
  • 3
  • 24
  • 44
-3
votes
1 answer

Get column of data from third level of a multidimensional array

I have a multidimensional array with four levels of data. Here's a small sample: $data = [ [ [ 'Date' => '2021-03-15T00:00:00.0000000+01:00', 'Arena' => ['Id' => 181796, 'Name' => 'Motala bibliotek'], …
mathias5
  • 301
  • 3
  • 13
-3
votes
5 answers

Make associative array of unique values from column containing arrays

Array ( [0] => Array ( [id] => 21153 [genre] => ["History","Drama", "Thriller"] ) [1] => Array ( [id] => 21152 [genre] => ["ACTION"] ) [2] => Array ( [id] => 21151 …
-5
votes
3 answers

Print one value from qualifying row or else all values from a specific column

I am using a foreach loop to echo names from my multi-dimensional array. Sample Array: $readJson = [ [ 'id' => 78, 'name' => 'audi', 'sscat_id' => 59, 'date' => '0000-00-00 00:00:00' ], [ 'id' =>…
1 2 3 4 5 6
7