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.
Related
This question already has answers here:
Transpose a PHP multidimensional array with predefined keys
(3 answers)
How to transpose a multidimensional multi-file upload submission and maintain associative keys?
(4 answers)
Closed 1 year ago.
i have an array that i get from a form with alot of fields, i need to order those fields under an array and that array under the ''main'' array. Ill post what i have, and what i need to get.
WHAT I HAVE:
Array
(
[perfil_area1] => Array
(
[0] => a2
[1] => a3
)
[perfil_years] => Array
(
[0] => 2
[1] => 4
)
[perfil_function] => Array
(
[0] => f1
[1] => f4
)
[perfil_obs] => Array
(
[0] => teste1
[1] => teste2
)
[perfil_company] => Array
(
[0] => emp1
[1] => emp2
)
)
This is what i need to be so i can turn it into a query:
What i need
Array
(
[0] =>
(
[perfil_area1] => a2
[perfil_years] => 2
[perfil_function] => f1
[perfil_obs] => teste1
[perfil_company] => emp1
)
[1] =>
(
[perfil_area1] => emp2
[perfil_years] => 2
[perfil_function] => f4
[perfil_obs] => 4
[perfil_company] => a3
)
)
i have tried with 2 foreach but i didnt manage to get it done. I have read Create a multidimensional array in a loop , how to create multidimensional array using a foreach loop in php? , create multidimensional array using a foreach loop , Converting an array from one to multi-dimensional based on parent ID values and some more but i still cant do it. Any tips on how to create it?
You just need to loop over the input array and build the new array
$new = [];
foreach ($in as $key=>$arr) {
$new[0][$key] = $arr[0];
$new[1][$key] = $arr[1];
}
<?php
$result = [];
for ($i=0; $i<count(reset($array)); $i++) {
$result[] = array_combine(array_keys($array), array_column($array, $i));
}
Please read the manual for more details about array_combine(), array_keys() and array_column().
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
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 6 years ago.
How to convert array within array to string, that means I am having a one result set having value of country id but the result set will be in array within array.
Something like below code :
Array
(
[0] => Array
(
[country_id] => 7
)
[1] => Array
(
[country_id] => 8
)
[2] => Array
(
[country_id] => 9
)
[3] => Array
(
[country_id] => 10
)
[4] => Array
(
[country_id] => 1
)
[5] => Array
(
[country_id] => 2
)
[6] => Array
(
[country_id] => 3
)
[7] => Array
(
[country_id] => 4
)
[8] => Array
(
[country_id] => 5
)
)
I want that country list into one string like 7,8,9,10,1,2,3,4,5 but without looping..
Anyone have idea please let me know...?
You can use array_column() and implode() function to do this.
Here is how you can do it,
$values=array_column($array,"country_id");
$country_string=implode($values);
array_column() returns the values from a single column of the input,
identified by the column_key(country_id).
implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between
each element.
implode() can have two arguments, first as glue by which you want to join the elements and second as the array of elements. If first argument is not given then default glue is ",".
Use array_column and implode for (PHP 5 >= 5.5.0, PHP 7) as
$data = array_column($records, 'country_id');
echo implode(",",$data);
try this,
$newarray = array();
foreach ($array as $item) {
$newarray[] = $item['country_id'];
}
echo implode(",",$newarray);
i hope it will be helpful.
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;
}
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 months ago.
I'm using a query in Codeigniter to return the ids of all the rows that belong to a user.
$this->db->select('id');
$this->db->where('user_id', 99);
$query = $this->db->get('my_table');
return $query->result_array();
This returns
Array ( [0] => Array ( [id] => 8 ) [1] => Array ( [id] => 7 ) [2] => Array ( [id] => 6 ) )
Is it possible to return a flat array like
Array ( [0] => 8 [1] => 6 [2] => 7 )
?
The CodeIgniter documentation (most particularly, query results) doesn't list anything that will produce a flat array result (like the PDO::FETCH_COLUMN mode provided for PDOStatement::fetchAll). As such, array results will need to be flattened in your code, such as with a foreach loop:
$flat = array();
foreach($query->result_array() as $row) {
$flat[] = $row['id'];
}
or with the special purpose array_column:
array_column($query->result_array(), 'id');
or with the general purpose array_reduce:
array_reduce(
$results,
fn ($accum, $row) => array_merge($accum, [$row['id']]),
[]);
If you're to lazy to add few lines each time you select one column, you would need to tweak CodeIgniter. Like adding some option or returning flattened array when single column is selected. I would suggest adding an option
Following can be another way of dong it. I don't know what is the advantage of it over the accepted answer.
Let's say you are storing output of function in $result. Then you can do as following:
array_unshift($result, null);
$transposed = call_user_func_array("array_map", $result);
print_r($transposed);
It will print
Array( [0] => Array ( [0] => 8 [1] => 6 [2] => 7 ))
And you can access what you want by $transposed[0]
Array ( [0] => 8 [1] => 6 [2] => 7 )