This question already has answers here:
Query with GROUP_CONCAT not working with PHP
(1 answer)
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 3 years ago.
How do i print array values from this query ?
I need something like 6475,7377,6367. (comma separated).
This is what i get when i do a print_r($myarray):
Array
(
[0] => Array
(
[gift_product] => 6475
)
[1] => Array
(
[gift_product] => 7377
)
[2] => Array
(
[gift_product] => 6367
)
)
Thanks alot!
You could have handled this on the MySQL side using GROUP_CONCAT, something like this:
SELECT GROUP_CONCAT(gift_product) AS products
FROM yourTable
GROUP BY id;
Your current output indicates that you are getting back gift_product values over several records, when in fact you wanted to retrieve a CSV string of products.
You can use implode function after you map your array using array_map:
echo implode(', ', array_map(function ($entry) {
return $entry['gift_product'];
}, $myarray))
Use array_column to fetch column wise data with implode,
echo implode(",", array_column($arr, "gift_product"));
Demo
Output
6475,7377,6367
Related
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
When I execute a print_r I get this result...
Array (
[0] => Array (
[A_program_id] => a0F36000008PIYF
[XC_program_logo] => louisville.jpg
)
[1] => Array (
[A_program_id] => a0F36000008qjSp
[XC_program_logo] => alabama.jpg
)
[2] => Array (
[A_program_id] => a0F36000008pRxL
[XC_program_logo] => trinity.jpg
)
)
How do I make a while loop or whatever to properly fetch needed to query something like this:
//zero based
echo"".$rows[0][0]."</td><td>"".$rows[1][0].""</td><td>"".$rows[2][0]."";
echo"".$rows[0][1]."</td><td>"".$rows[1][1].""</td><td>"".$rows[2][1]."";
to show
louisville.jpg alabama.jpg trinity.jpg
a0F36000008PIYF a0F36000008qjSp a0F36000008pRxL
please help thx
One possibility is to use array_column to extract each rows data from the source, and then implode to add the </td><td> between each value:
echo implode('</td><td>', array_column($rows, 'XC_program_logo'));
echo implode('</td><td>', array_column($rows, 'A_program_id'));
Demo on 3v4l.org
This will give the same output as the two echo statements in your question.
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
when I print_r $output I get the following
Array ( [0] => stdClass Object ( [id] => foo ) [1] => stdClass Object ( [id] => bar ) [2] => stdClass Object ( [id] => foobar ))
Im trying to do something which I though would be simple and convert the $output into
foo, bar, foobar
I have tried using for each statements such as below
foreach($array as $row){
$new_array[$row['nid']] = $row['nid'];
}
print_r($new_array);
but I just get invalid php.
I have tried ausing arraywalk and other examples here but not getting anything to work.
So how do I convert the following Array into a simple list of IDs?
Thanks for any help
Except for the comma separated list, what you have should work in theory, but the code doesn't at all match your array. You state $object not $array and the property is id not nid.
However, just extract the id column and implode. If you have PHP 7:
$list = implode(', ', array_column($output, 'id'));
For older versions:
$list = implode(', ', array_map(function($v) { return $v->id; }, $output));
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
A variable called $result
When print_r ($result);
It goes:
Array
(
[0] => iPhone
)
Array
(
[0] => iOS
)
Array
(
[0] => Safari
)
Seems like there are 3 Arrays in this $result, but I want to marge it to one and give theme order, I've tried array_merge(), and array_combine(), but none of theme get the result I want, what I really want is:
Array( 'iPhone', 'iOS', 'Safari' )
So I could output one of them using $results[0] or $results[1]...
How can I achieve that? Thanks alot.
Use array_merge with ... splat operator
$result = array_merge(...$array);
OR
$result = call_user_func_array('array_merge', array($a));
For example :- https://3v4l.org/38Q5h
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
Controller:
$student = $this->school_model->get_school_students($institute);
echo "<pre>";
print_r($student);
echo implode(',',$student);
exit;
Model:
function get_school_students($institute=''){
$query=$this->db->query("SELECT id FROM users as u where
u.current_educational_institution = '$institute' and u.deleted=0 AND u.role=2");
$result=$query->result_array();
if($query->num_rows()>0){
// $output=array();
$output=$result;
}
return $output;
}
Output:
Array
(
[0] => Array
(
[id] => 280
)
[1] => Array
(
[id] => 282
)
)
Array,Array ......
I want to echo 280,282 from implode but it is showing array array??? help me?
Reason:- Data that you are getting from function is multi-dimensional-array and implode() works on single-dimensional-array .That's why you are facing problem.
Solution:-
convert your multi-dimensional-array to single-dimensional-array using array_colum()like below:-
echo implode(',',array_column($student,'id'));
Output:-https://eval.in/829544
Reference:- array_column()
This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);