Transformation multidimensional array into single array? [duplicate] - php

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 8 years ago.
My code looks like:
Array ( [0] => Array ( [uid] => 3456345345 [name] = test))
What to do to look like this:
Array ( [uid] => 3456345345 [name] = test)

<?php
function arrayflatten($array)
{
if(!is_array($array))
return false;
$result=array();
foreach($array as $key=>$value)
{
if(is_array($value))
$result=array_merge($result,arrayflatten($value));
else
$result[$key]=$value;
}
print_r ($result);
}
$a=array(array( "Volvo","22"));
arrayflatten($a);
?>

Related

How to convert a multidimensional array into a single line array in PHP? [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key

how to fetch data in single array instead of arrays of arrays [duplicate]

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()

Get array of user IDs from multi-dimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

Add to an array while in a loop with a key [duplicate]

This question already has answers here:
PHP - Append elements to array
(3 answers)
Closed 8 years ago.
I'm trying to populate all the links in my database into an array with a key something like this:
$link = array(99999 => "http://link1.com", 111111 => "http://link2.com");
Which returns:
Array ( [99999] => http://link1.com [111111] => http://link2.com )
How can i get the array to look like that when going through a loop, Here is my code:
while($row = mysql_fetch_array($query)) {
$link[] = "$row[r_id] => $row[r_link]";
}
It returns:
Array ( [0] => 24004 => http://link1.com [1] => 30554 =>
http://link2.com
But i don't wan't it to be like this, How can i get it the same as the first example while going through a loop ?
while($row = mysql_fetch_array($query)) {
$link[$row['r_id']] = $row['r_link'];
}
$link[$row['r_id']] = $row['r_link'];

count element in array? [duplicate]

This question already has answers here:
Working with arrays in php
(4 answers)
Closed 3 years ago.
I have an Array like this (when I print_r). How can I sum all elements in this Array?
In this example, the sum is 0+1=1
Array ( [0] => Array ( [0] => 0 )
[1] => Array ( [0] => 1 ) )
Take a look at the 'array_reduce' function: array_reduce: http://nl.php.net/manual/en/function.array-reduce.php
Taken from the documentation:
function rsum($v, $w)
{
$v += $w;
return $v;
}
$sum = array_reduce($a, "rsum");

Categories