Explode each values of Array element - php

I have an array like :
Array
(
[2] => 2,6
[3] => 1
[4] => 14
[5] => 10
[6] => 8
)
I want to explode each element of an array and return a new array using array_map, so that I can avoid using loop, and creating extra function to call back.
O/p should be like :
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)

You can use
$result = array_map(function($val) {
return explode(',', $val);
}, $input);
Which will result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
This will work on PHP >= 5.3 with support for anonymous functions.

You can also do
$result = array_map('str_getcsv', $input);
This will also result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)

Try following code
$newArr = array_map(function($val, $key){
return explode(",", $val);
}, $arr);

$data = array(2 => '2,6',3 => '1',4 => '14',5 => '10',6 => '8');
foreach($data as $key => $val) {
$new = explode(',',$val);
$data[$key] = $new;
}
$output = $data;
echo '<pre>';
print_r($output);

Related

Multidimensional array flattening technique still leaves empty arrays [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000043
[1] => 1000045
[2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000030
[1] => 1000032
[2] => 1000058
[3] => 1000064
) ...
But what I'm expecting is a truly flattened single array:
Array
[0] => 1000043
[1] => 1000045
[2] => 1000050
[3] => 1000030
[4] => 1000032
[5] => 1000058
[6] => 1000064
)
The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:
function array_flatten5(array $array)
{
$flat = array(); // initialize return array
$stack = array_values($array); // initialize stack
while($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
$stack = array_merge(array_values($value), $stack);
}
else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.
Here is the original array. It is 4-deep:
Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )
And here is the outcome using the array_walk_recursive suggestion ...
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000111
[1] => 1000113
[2] => 1000129
[3] => 1000134
)
Array
(
)
Array
(
)
Array
(
[0] => 1000012
[1] => 1000085
)
Array
(
) ...
You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.
$array = [
[1, 2, 3, 4],
[[5, 6], [7, 8]],
[[[9], [10]], [11]]
];
$result = [];
array_walk_recursive($array, function ($tempV) use (&$result) {
$result[] = $tempV;
});
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
)

How to get value of all same keys of multidimentional array in php

Basically i want to loop through a multidimensional associative array to get a simple indexed array
Here is my master array
Array
(
[0] => Array
(
[user_id] => 2
[children] => Array
(
[0] => Array
(
[user_id] => 5
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 6
[children] => Array
(
)
)
)
)
[1] => Array
(
[user_id] => 3
[children] => Array
(
[0] => Array
(
[user_id] => 7
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 8
[children] => Array
(
)
)
)
)
[2] => Array
(
[user_id] => 4
[children] => Array
(
[0] => Array
(
[user_id] => 9
[children] => Array
(
[0] => Array
(
[user_id] => 10
[children] => Array
(
[0] => Array
(
[user_id] => 11
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 12
[children] => Array
(
)
)
[2] => Array
(
[user_id] => 13
[children] => Array
(
)
)
)
)
)
)
)
)
)
Here is the result i want to achive
$userArray= array(2,3,4,5,6,7,8,9,10,11,12,13);
basically i just want all the user_id key value inside 1 single indexed array.
Till now i have tried this code
$keys = array_keys($masterArray);
for($i = 0; $i < count($masterArray); $i++) {
echo $keys[$i] . "{<br>";
foreach($masterArray[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
You can use array_walk_recursive() which will iterate over the leaf nodes in a multidimensional array, check for the key being user_id and if so, add it to a list of ids...
$ids = [];
array_walk_recursive($masterArray, function ( $value, $key) use (&$ids) {
if ( $key == "user_id" ) {
$ids[] = $value;
}
});
print_r($ids);
which with the sample data, gives...
Array
(
[0] => 2
[1] => 5
[2] => 6
[3] => 3
[4] => 7
[5] => 8
[6] => 4
[7] => 9
[8] => 10
[9] => 11
[10] => 12
[11] => 13
)
You just have to travel recursively to collect all user IDs as below:
<?php
function collectUserIDs($data,&$result){
foreach($data as $current_data){
$result[] = $current_data['user_id'];
collectUserIDs($current_data['children'],$result);
}
}
$result = [];
collectUserIDs($data,$result);
print_r($result);

how can i get count of each age group using php

please check this :
Array
(
[0] => 46-65
[1] => 7-12|31-45
[2] => 31-45
[3] => 31-45
[4] => 66+
[5] => 18-30
[6] => 46-65
[7] => 13-17|46-65
Here i converted string to array then i got below array :
Array
(
[0] => Array
(
[0] => 46-65
)
[1] => Array
(
[0] => 7-12
[1] => 31-45
)
[2] => Array
(
[0] => 31-45
)
[3] => Array
(
[0] => 31-45
)
[4] => Array
(
[0] => 66+
)
[5] => Array
(
[0] => 18-30
)
[6] => Array
(
[0] => 46-65
)
[7] => Array
(
[0] => 13-17
[1] => 46-65
)
[8] => Array
(
[0] => 31-45
)
[9] => Array
(
[0] => 31-45
)
[10] => Array
(
[0] => 31-45
)
[11] => Array
(
[0] => 18-30
)
[12] => Array
(
[0] =>
)
[13] => Array
(
[0] => 46-65
)
[14] => Array
(
[0] => 7-12
[1] => 31-45
)
[15] => Array
(
[0] => 18-30
)
[16] => Array
(
[0] => 18-30
[1] => 31-45
)
[17] => Array
(
[0] => 18-30
)
[18] => Array
(
[0] => 31-45
)
[19] => Array
(
[0] => 18-30
)
[20] => Array
(
[0] => 13-17
[1] => 18-30
[2] => 46-65
)
}
I stored age group in string format because each row can be have multiple age group. after that i converted into array.
How can i get count of each age group like 46-65 is 6 times 18-30 is 8 times.
HEre i want count of each age group
This code should work for that:
<?php
$selections = [/** your array here */];
$counted = [];
// Loop full list
foreach($selections as $selection) {
// Loop age elements in list
foreach ($selection as $age) {
// If not already counted initialise age range
if(!isset($counted[$age])) {
$counted[$age] = 0;
}
// Increment age range
$counted[$age]++;
}
}
var_dump($counted);
I'm not sure I fully understood tom problem. Try something like this.
This will return you a array with as key the groups and as value the number of times it has been met.
I used your second array or you already subdivided into sub array
$groups = [];
foreach($array as $subArray) {
foreach ($subArray as $group) {
if (isset($groups[$group])) {
$groups[$group] += 1;
} else {
$groups[$group] = 1;
}
}
}

How to convert multi dimensional array to single dimensional array in php ?

I want convert this multi dimensional array to single dimensions array
array([ca] => Array (
[0] => Array ( [userid] => 1 )
[1] => Array ( [userid] => 10 )
[2] => Array ( [userid] => 14 )
[3] => Array ( [userid] => 16 )
[4] => Array ( [userid] => 17 )
[5] => Array ( [userid] => 18 )
[6] => Array ( [userid] => 25 )
)
Convert to following array
array ([ca] =>
array(
[0] => 1
[1] => 10
[2] => 14
[3] => 16
[4] => 17
[5] => 18
[6] => 25
))
Thanks in advance
$res = [];
foreach ($array as $key => $val) {
$res['ca'][] = $val['userid'];
}
print_r($res);
And an example with array_walk:
array_walk(
$array['ca'],
function(&$item, $key) {
return $item = $item['userid'];
}
);

php - Regular Expression to match someting that starts with

I am terribly sorry but the array looks like this:
Array
(
[1] => Array
(
[0] => msie6.0
[1] => 7
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
[5] => Array
(
[0] => msie7.0
[1] => 915
)
and so on...
When i try to replace for example msie6.0and msie7.0 with InternetExplorer and add it occurence :
preg_match("/#^msie(.*)$#i/is", $results, $matches);
$test = $matches[0] ;
print_array($test);
$results["#^startText(.*)$#i"] = $results['InternetExplorer'];
print_array($results);
unset($results["/#^msie(.*)$#i/is]);
it not match the perfect as i want. any solution for that ?
in order to have :
Array
(
[1] => Array
(
[0] => InternetExplorer
[1] => 922
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
After your clarification in comment. I think you don't need regex, strpos is enough too that job.
$rows["InternetExplorer"] = 0;
foreach($rows as $key => $value){
if(strpos($key,"msie") !== false){
$rows["InternetExplorer"] += $value;
unset($rows[$key]);
}
}
DEMO.

Categories