how to simplify an array of arrays with single object attribute [duplicate] - php

This question already has answers here:
how get each single column data from php multidimensional array?
(2 answers)
Closed 2 years ago.
I have the following data structure:
$campaigns =
Array
(
[0] => Array
(
[subject] => cca-cpg
)
[1] => Array
(
[subject] => cleanup-cpg
)
[2] => Array
(
[subject] => gas-cpg
)
[3] => Array
(
[subject] => pollinators-cpg
)
)
what I would like to end up with is:
$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];
this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
array_push($newCampaigns,$campaigns[$i]['subject'];
}
but I was wondering if there's a better way to do this. The data is coming directly from a mysql database

There's array_column() function for you:
$newCampaigns = array_column($campaigns, 'subject');
Source: https://www.php.net/manual/en/function.array-column.php

Related

On array How can i select only column [duplicate]

This question already has answers here:
Remove unwanted elements from subarrays in multidimensional array
(3 answers)
Preserve elements in each row of a two-dimensional array based on another array
(7 answers)
Get multi column from array php, Alternate of array_column [duplicate]
(2 answers)
How can I access an array/object?
(6 answers)
Closed 7 months ago.
I have an array like the one below in laravel which I want to fetch the column.
Array
(
[0] => Array
(
[table] => form_identification
[column] => region
)
[1] => Array
(
[table] => form_identification
[column] => province
)
)
And I want the output like this
Array
(
[0] => Array
(
[column] => region
)
[1] => Array
(
[column] => province
)
)
Consider your first array as $datas variable here and we are using for each whole array
foreach($datas as $data) {
$result[] = array('column' => $data['column']);
}
print_r($result);

How to insert values in assosiative array with mysqli_fetch_array() [duplicate]

This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 2 years ago.
I want to make array with key 'barang' that contains ids of barang in my database.
Here my code
while ($globalBarang = mysqli_fetch_array($sqlBarang)) {
$arrayglobal["barang"] = array($globalBarang['id_brg']);
}
for now with print_r() it result
Array ( [barang] => Array ( [0] => SSB-001 ) )
What I want:
Array ( [barang] => Array ( [0] => BRS-001,
[1] => GLP-001,
[2] => SSB-001 ) )
Here my database if needed:
Database:
You just need to change the assignment, add each element with [] (and not as an array on the right)...
while ($globalBarang = mysqli_fetch_array($sqlBarang)) {
$arrayglobal["barang"][] = $globalBarang['id_brg'];
}

Reduce down level of multidimensional array in PHP [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 months ago.
I'm wondering what the most efficient way to reduce this array down by a level, ideally without loops in PHP. It's a result from mysqli_fetch_all().
Array
(
[0] => Array
(
[ID] => 648546
)
[1] => Array
(
[ID] => 648552
)
[2] => Array
(
[ID] => 650046
)
[3] => Array
(
[ID] => 652732
)
[4] => Array
(
[ID] => 652738
)
[5] => Array
(
[ID] => 652756
)
)
The result I would like is
array(648546,648552,650046,652732,...)
The simple query example it comes from is something as easy as:
SELECT mytable.ID FROM mytable WHERE status =1
This should do it for PHP 5.5+
$result = array_column($array, 'ID');
You can use array_map():
$new_array = array_map(function($v){return $v['ID'];}, $old_array);
You might try this:
SELECT GROUP_CONCAT(mytable.ID)
FROM mytable
WHERE status = 1
GROUP BY status
It should return 1 row with the ID as a comma separated list.

Loop through multiple arrays php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
Can anybody explain me how can i get the value of 'ubicacion' in those arrays, and then, store that value in a php variable.
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 02021002
)
)
)
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 034267211
)
)
)
This will give you the value of it..
$value = $array[0]['PC']['ubicacion'];

Easiest way to make DB records as array keys [duplicate]

This question already has answers here:
PHP - Make my query's array's key the ID
(2 answers)
Closed 8 years ago.
What is the easiest and fastest way to get or manipulate the MySQL result with id values as keys in PHP? I know we can simply loop through the entire result set and so on. But what is the fastest and also the simplest way of doing this?
That is to convert,
Array
(
[0] => Array
(
[id] => 1
[user_name] => user1
)
[1] => Array
(
[id] => 3
[user_name] => user3
)
)
to this.
Array
(
[1] => Array
(
[id] => 1
[user_name] => user1
)
[3] => Array
(
[id] => 3
[user_name] => user3
)
)
This question may face some downvotes. But still, i think any answer on this would be helpfull to many.
Use array_column()
$new_array = array_column($old_array, NULL, 'id');
print_r($new_array);
So construct the array by using the id as key.
$result = array();
while ($row = db_dummy_fetch_method($res)) {
$result[$row['id']] = $row;
}

Categories