Comma separated value from array [duplicate] - php

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)

assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);

Related

PHP : Sort an array by value based on another array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Sorting a php array of arrays by custom order
(8 answers)
Closed last month.
I have two array and I want to arrange one of them based on the other one.
$sortMe = Array
(
[0] => 100
[1] => 1
[2] => 10
[3] => 1000
)
$example = Array
(
[0] => 1
[1] => 1000
[2] => 100
[3] => 10
)
how can I sort the $sortMe array based on $example to:
$sortMe = Array
(
[1] => 1
[3] => 1000
[0] => 100
[2] => 10
)

How can I get multiple values from array within array? [duplicate]

This question already has answers here:
How to extract specific array keys and values to another array?
(2 answers)
Closed 2 years ago.
I have an array that looks like this:
[meta_data] => Array
[0] => Array
(
[id] => 100
[name] => John
)
[1] => Array
(
[id] => 200
[name] => Peter
)
[2] => Array
(
[id] => 300
[name] => Peter
)
What would be the simplest way to select all names from $meta_data?
Thanks.
see https://www.php.net/manual/en/function.array-column.php
var_dump(array_column($array['meta_data'], 'name'));

How to combine or merge multiple values inside the single array? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(26 answers)
Closed 3 years ago.
I have an array values. Inside the array some multiple data but how to combine or merge these multiple records.
some values inside the single array are multiples . How to merge these ?
Array
(
[0] => plxliupp
[1] => plxliupp
[2] => dg_w12
[3] => col4312
[4] => Iphone6s
[5] => Ndry_milk
[6] => smj7
[7] => ovn 02 o3
[8] => allwin6406f
)
After remove multiples values. array like this
Array
(
[0] => plxliupp
[1] => dg_w12
[2] => col4312
[3] => Iphone6s
[4] => Ndry_milk
[5] => smj7
[6] => ovn 02 o3
[7] => allwin6406f
)
thanks for answering
$array = array("a" => "moon", "star", "b" => "moon", "star", "sky");
// Deleting the duplicate items
$result = array_unique($array);
print_r($result);

merge sub array into main array [duplicate]

This question already has answers here:
Merge 2 nested arrays in one with multiple values
(2 answers)
Closed 5 years ago.
how do i merge sub array into main array.
Array
(
[0] => Array
(
[FirmId] => 1
[PartyName] => abc medicals
[PartyCode] => P001
[SalesmanName] =>
)
[1] => Array
(
[FirmId] => 2
[PartyName] => xyz medicals
[PartyCode] => P001
[SalesmanName] =>
)
)
Array
(
[0] => Array
(
[SalesmanName] => abc
)
[1] => Array
(
[SalesmanName] => xyz
)
)
Output should be :
Array
(
[0] => Array
(
[FirmId] => 1
[PartyName] => abc medicals
[PartyCode] => P001
[SalesmanName] => abc
)
[1] => Array
(
[FirmId] => 2
[PartyName] => xyz medicals
[PartyCode] => P001
[SalesmanName] => xyz
)
)
I tried array_merge($arr1,$arr2); but output was not as expected.
Merge 2 nested arrays in one with multiple values
This solution is too expensive, actually my first array has 30 columns but i mentioned only 4 to simplify my question.
Actually second array is a sliced part of main array need to merge it.
http://php.net/manual/en/function.array-merge-recursive.php
you want to use array_merge_recursive()

How to display the values of array in sequence in PHP? [duplicate]

This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 9 years ago.
I need to display array in sequence
my array
Array
(
[0] => test#yahoo.com
[1] => rans#yahoo.com
[2] => maria#gmail.com
[3] => lspap#gmail.com
[6] => sage#yahoo.com
[7] => rlope#hotmail.com
[13] => alyssa#gmail.com
)
I want output like following
Array
(
[0] => test#yahoo.com
[1] => rans#yahoo.com
[2] => maria#gmail.com
[3] => lspap#gmail.com
[4] => sage#yahoo.com
[5] => rlope#hotmail.com
[6] => alyssa#gmail.com
)
Now my question is how can display array like above sequence in PHP?
use array_values to reindex an array
$array = array_values($array);
To display the values in sequence use a foreach loop as so
foreach($array as $arr){
echo $arr."\n";
}
to store the values with new indexes or keys use
array_values($array);

Categories