This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 5 years ago.
Here is my output: im using more foreach loop with different condition and values put into same array name $returnVal[]=
EX:
foreach($val1 as $vals)
{
$returnVal[]=$vals;
}
foreach($val2 as $vals)
{
$returnVal[]=$vals;
}
foreach($val3 as $vals)
{
$returnVal[]=$vals;
}
foreach($val4 as $vals)
{
$returnVal[]=$vals;
} `
Im getting Output:
Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => 57038--JEFFERSON--UNION--SD
)
[3] => 57049--NORTH SIOUX CITY--UNION--SD
)
My Question: remove duplicate values and merge into one level
Push element if it is not exists in the result array, this will avoid duplicate entries. Try the code below,
$returnVal = [];
foreach($val1 as $vals)
{
if (!in_array($vals, $returnVal)) {
$returnVal[]=$vals;
}
} ..etc and so on
Related
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have nested array with key & value pair. i want to convert it in single array.
/* This is current array */
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[qty] => Array
(
[0] => 1
[1] => 1
)
[price] => Array
(
[0] => 364.41
[1] => 300
)
[amount] => Array
(
[0] => 364.41
[1] => 300
)
)
/*Now, I want this type of array*/
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
[price] => 364.41
[amount] => 364.41
)
[1] => Array
(
[id] => 2
[qty] => 1
[price] => 300
[amount] => 300
)
)
I have tried array_walk and some other solutions but i could not find any proper solution
Thanks in advance
Main array is your nested array and Collection is the result you want
foreach($mainArray["id"] as $key => $value ){
$collection[$key] = ['id' => $value];
foreach(array_keys($mainArray) as $arrayKeysOfmainArray){
if(!array_key_exists($arrayKeysOfmainArray, $collection)){
$collection[$key][$arrayKeysOfmainArray] = $mainArray[$arrayKeysOfmainArray][$key];
}
}
}
print_r($collection);
$mainarray['id'] = Array(1,2,3);
$mainarray['qty'] = Array(1,1,4);
$mainarray['price'] = Array(364.41,300,500);
$mainarray['amount'] = Array(364.41,300,600);
$new = [];
foreach($mainarray as $key=> $singleArray){
if(count($singleArray) > 0){
foreach($singleArray as $childKey=> $value){
$new[$childKey][$key] = $value;
}
};
}`
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}
This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 years ago.
I have this array of arrays,
Array
(
[0] => Array
(
[id] => 1
[title] => AP-2 (1)
)
[1] => Array
(
[id] => 2
[title] => AC-1 (2)
)
[2] => Array
(
[id] => 3
[title] => AB-3 (1)
)
[3] => Array
(
[id] => 4
[title] => AD-2 (3)
)
[4] => Array
(
[id] => 5
[title] => AE-2 (1)
)
),
and I need to sort it in a way in which it will look like this,
Array
(
[0] => Array
(
[id] => 1
[title] => AB-3 (1)
)
[1] => Array
(
[id] => 2
[title] => AC-1 (2)
)
[2] => Array
(
[id] => 3
[title] => AD-2 (3)
)
[3] => Array
(
[id] => 4
[title] => AE-2 (1)
)
[4] => Array
(
[id] => 5
[title] => AP-2 (1)
)
)
What happened here is basically, sort the arrays using the title key alphabetically or maybe sort it using natsort() or natcasesort(). How would I actually do the sorting? Thanks in advance.
function sorter($key){
return function ($a, $b) use ($key) {
return strcmp($a[$key], $b[$key]);
};
}
usort($arr, sorter('title'));
var_dump($arr);
For versions of PHP prior to 5.3, use:
function sorter($arr, $index) {
foreach($arr as $key => $value) {
$arr2[$key] = strtolower($value[$index]);
}
asort($arr2);
foreach($arr2 as $key =>$value) {
$arr3[] = $arr[$key];
}
return $arr3;
}
var_dump(sorter($arr, 'title'));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to find the value from this array in php?
hello coders.I have an array like this
Array
(
[0] => Array
(
[0] => admin/login
[1] => LoginHandler
[2] => index
)
[1] => Array
(
[0] => post
[1] => PostHandler
[2] => index
)
[2] => Array
(
[0] => post/create
[1] => postHandler
[2] => create
)
[3] => Array
(
[0] => post/update
[1] => postHandler
[2] => update
)
[4] => Array
(
[0] => post/delete
[1] => postHandler
[2] => delete
)
)
I want only the values like admin/login, LoginHandler,index,
post,PostHandler,index,
post/create,PostHandler,create and so on.So how to do that?
You can loop through the array to perform an action on each element:
foreach ( $your_array as $element ) {
if ( is_array( $element ) ) {
foreach ( $element as $sub_element ) {
// You can store this value in a variable, or output it in your desired format.
echo $sub_element . "<br />";
}
}
else {
echo $element . "<br />";
}
}