I have array like below:
Array
(
[22] => Array
(
[0] => 60
[29] => Array
(
[0] => 6
)
[30] => Array
(
[0] => 5
[1] => 8
)
[31] => Array
(
[0] => 7
[1] => 9
[2] => 14
[3] => 26
)
)
[23] => 12
[35] =>10
[42] =>22
)
now i want to implode array like
60[6][5||8][7||9||14||26]|12|10|22
I have tried below code:
$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);
But it is not implode with required glue
How can i do it?
you can use this code
<?php
$a= Array(
22 => Array(
0 => 60,
29 => Array(
0 => 6
),
30 => Array
(
0 => 5,
1 => 8
),
31 => Array
(
0 => 7,
1 => 9,
2 => 14,
3 => 26
),
),
23 => 12,
35 =>10,
42 =>22,
);
$string='';
foreach($a as $arr){
if(is_array($arr)){
foreach($arr as $array){
if(is_array($array)){
$string .= '['.implode("||",$array).']';
}else{
if($string!==''){ $string .= '|';}
$string .= $array;
}
}
}else{
if($string!==''){ $string .= '|';}
$string .= $arr;
}
}
echo $string;die;
?>
Out put wil be
60[6][5||8][7||9||14||26]|12|10|22
Desired result without foreach.
$array = [
22 => [
0 => 60,
29 => [
0 => 6
],
30 => [
0 => 5,
1 => 8
],
31 => [
0 => 7,
1 => 9,
2 => 14,
3 => 26
]
],
23 => 12,
35 => 10,
42 => 22
];
$result = implode('|', array_map(function($item)
{
return is_array($item) // convert sub array into string
? implode('', array_map(function($inner_item)
{
return is_array($inner_item) // convert inner array into string
? '[' . implode('||', $inner_item) . ']'
: $inner_item;
}, $item))
: $item;
}, $array));
var_dump($result);
So, we have 3 types of delimiters: '|' - first level, ''(empty) - second level, '||' - third level.
You can use foreach and implode to achieve your pattern:
<?php
header('Content-Type: text/plain');
$arr = array();
$arr22 = array();
$arr22[0] = 60;
$arr22[29] = array(6);
$arr22[30] = array(5,8);
$arr22[31] = array(7,9,14,26);
$arr[22] = $arr22;
$arr[23] = 12;
$arr[35] = 10;
$arr[42] = 22;
$output = '';
foreach($arr as $entry) {
if(!is_array($entry)) {
$output .= $entry;
} else {
// array
foreach($entry as $inner) {
if(is_array($inner)) {
$output .= '[' . implode('||', $inner) . ']';
} else {
$output .= $inner;
}
}
}
$output .= '|';
}
echo substr($output, 0, strlen($output) - 1);
?>
which outputs:
60[6][5||8][7||9||14||26]|12|10|22
This should work for you:
Here the glue is configurable as you desired and this logic is built on the recursive call hence this will work for any level of multidimensional array:
<?php
$arrVal = array (
'22' => array(
'0' => 60,
'29' => array(
'0' => 6
),
'30' => array (
'0' => 5,
'1' => 8
),
'31' => array (
'0' => 7,
'1' => 9,
'2' => 14,
'3' => 26
)
),
'23' => 12,
'35' => 10,
'42' => 22
);
//60[6][5||8][7||9||14||26]|12|10|22
$constructedValue = "";
$glue = "||";
echo $constructedValue = implodeMultiArr($arrVal,$glue);
function implodeMultiArr($arrVal,$glue) {
$i = 0;
$constructedValue = "";
foreach ( $arrVal as $k=>$v) {
if ( is_array($v) ) {
$constructedValue .= !empty($constructedValue) ? "[".implodeMultiArr($v,$glue)."]" : implodeMultiArr($v,$glue)."]" ;
} else {
$constructedValue .= !empty($constructedValue) ? $glue.$v : $v ;
}
$i++;
}
return $constructedValue;
}
Related
The array is:
Array ( [0] => Array ( [dnu] => 121428 [d1] => 43 [d3] => 27 [d7] => 20 [d15] => 15 [d30] => 12 ) )
i want something like this:
[{"col":"dnu","value":121428},{"col":"d1","value":"43"},{"col":"d7","value":"20"}]
Try this.
$result = [];
foreach($array as $col => $value) {
$result[] = [
'col' => $col,
'value' => $value
];
}
$json = json_encode($result);
Try like this way with foreach() and json_encode()
<?php
$array = array ( array ( 'dnu' => 121428, 'd1' => 43, 'd3' => 27, 'd7' => 20, 'd15' => 15, 'd30' => 12 ) );
$result = [];
foreach($array[0] as $key=>$value){
$result[] = ['col'=>$key,'value'=>$value];
}
echo json_encode($result);
?>
DEMO: https://3v4l.org/vfG9k
Below is an example of a function that adds a constant number to the keys of an array -
function addConstantToArrayKeys($seed_array, $constant)
{
foreach($seed_array as $key => $value){
$a[$key + $constant] = $value;
}
return $a;
}
$basearray = [1 => 17, 2 => 24, 3 => 12];
$test = addConstantToArrayKeys($basearray, 19);
The result of this example is:
Array
(
[1] => 17
[2] => 24
[3] => 12
)
Array
(
[20] => 17
[21] => 24
[22] => 12
)
It works as desired but can this method be rewritten somehow using a functional programming approach that can achieve the same result?
I have tried this:
function addConstantToArrayKeys($seed_array, $constant)
{
return array_map(function($key, $element) use($constant)
{return $a[$key + $constant] = $element;},
array_keys($seed_array), array_values($seed_array));
}
But it does not handle the keys as desired. Here is the output -
Array
(
[1] => 17
[2] => 24
[3] => 12
)
Array
(
[0] => 17
[1] => 24
[2] => 12
)
Edit -
Thanks to Progrock's answer, this worked -
function addConstantToArrayKeys($seed_array, $constant)
{
return array_combine(array_map(
function($key) use($constant) {
return $key + $constant;
}, array_keys($seed_array)), $seed_array);
}
May be its too late but this should work for you as you wanted the functional approach.
function addConstantToArrayKeys($basearray, $constant){
$arrayReindexed = [];
array_walk($basearray,function($v,$k) use (&$arrayReindexed, $constant) {
$key = $k;
$k = $key + $constant;
$arrayReindexed[$k] = $v;
});
return $arrayReindexed;
}
$basearray = [1 => 17, 2 => 24, 3 => 12];
$test = addConstantToArrayKeys($basearray, 19);
print '<pre>';
print_r($test);
print '</pre>';
Output:
Array (
[20] => 17
[21] => 24
[22] => 12
)
You could use something unreadable like this. It does not mutate existing array keys.
<?php
$nums = [
1 => 17,
2 => 24,
3 => 12
];
$out = array_combine(array_map(
function($int) {
return $int + 10;
}, array_keys($nums)), $nums);
var_export($out);
Output:
array (
11 => 17,
12 => 24,
13 => 12,
)
Your first example is perfectly functional and readable, but for the naming.
<?php
$nums = [
1 => 17,
2 => 24,
3 => 12
];
$add_num_to_keys = function (array $in, $num) {
$out = [];
foreach($in as $k => $v)
$out[$k + $num] = $v;
return $out;
};
$out = $add_num_to_keys($nums, 10);
var_export($out);
Output:
array (
11 => 17,
12 => 24,
13 => 12,
)
You can't do:
foreach($in as &$k => $v)
$k += 10;
As the key element can't be a reference.
i have this data
[0] => 1#*#1-1
my requirement is explode from #*# and have to make generated array elements as key value pairs
Example
$data = explode("#*#",'1#*#1-1');
$data[0] =1;
$data[1] = 1-1;
now my requirement is make a dynamic associative array
array($data[1] => $data[0])
<?
$str = '1#*#1-1
3#*#1-2
5#*#1-3
7#*#1-4
9#*#1-5
11#*#1-6
13#*#1-7
15#*#1-8
17#*#1-9
19#*#1-10
2#*#2-1
4#*#2-2
6#*#2-3
8#*#2-4
10#*#2-5
12#*#2-6
14#*#2-7
16#*#2-8
18#*#2-9';
$ex = array_map('trim',explode("\n",$str));
$out = array();
foreach($ex as $e){
$ex2 = explode('#*#',$e);
$out[$ex2[1]] = $ex2[0];
}
print_r($out);
Array
(
[1-1] => 1
[1-2] => 3
[1-3] => 5
[1-4] => 7
[1-5] => 9
[1-6] => 11
[1-7] => 13
[1-8] => 15
[1-9] => 17
[1-10] => 19
[2-1] => 2
[2-2] => 4
[2-3] => 6
[2-4] => 8
[2-5] => 10
[2-6] => 12
[2-7] => 14
[2-8] => 16
[2-9] => 18
)
?>
<?php
$mydata = array();
$data = array('1#*#1-1');
list($key, $val) = explode('#*#', $data[0]);
$mydata[$val] = $key;
// Check work.
echo '<pre>' . print_r($mydata, TRUE) . '</pre>';
here is my $bob array :
Array
(
[n] => Array
(
[0] => 1
)
[m] => Array
(
[0] => 1
[1] => 2
)
[l] => Array
(
[0] => 1
[1] => 4
[2] => 64
)
[o] => Array
(
[0] => 1
[1] => 4
)
)
And i need to output in:
n-1
m-1 , m-2
l-1 , l-4 , l-64
o-1 , o-4
I tryed some
foreach ($bob as $value) {
foreach ($value as &res)
$value = $bob . "-" . $res;
}
}
I guess its pity but i'm php newbe..
All help will welcome,
Jess
You're miss curly bracket after foreach, and missprint with &res -> $res try use foreach with $key
Try this
<?php
$bob = [
'n' => [0 => 1],
'm' => [0 => 1, 1 => 2],
'l' => [0 => 1, 1 => 4, 2 => 64],
'o' => [1 => 1, 1 => 4],
];
foreach ($bob as $key => $value) {
foreach ($value as $res) {
echo $key . "-" . $res . PHP_EOL;
}
}
This output for me
php test.php
n-1
m-1
m-2
l-1
l-4
l-64
o-4
foreach ($bob as $key => $value) {
foreach ($value as $res){
echo $key . "-" . $res ." ";
}
}
The foreach ($bob as $key => $value) syntax gives you each key for each value. You can then loop over the $value array to get the numbers you need.
Try this:
it will give you exact output with , and new line
Live demo : https://eval.in/87738
$arr =array
(
'n' => array
(
0 => 1
),
'm' => array
(
0 => 1,
1 => 2
),
'l' => array
(
0 => 1
),
'0' => array
(
0 => 1,
1 => 2,
2 => 64,
3 => 120,
)
);
$output = '';
foreach($arr as $k1 =>$v1){
$out = ' ';
foreach($arr[$k1] as $k => $v){
$out .= $k1.'-'.$v.',';
}
$output .= rtrim($out,',').'<br/>';
}
echo $output;
Output:
n-1 m-1,m-2 l-1 0-1,0-2,0-64,0-120
try $res in place of &res in following line:
foreach ($value as &res)
Can you try this,
foreach ($bob as $key=>$value) {
foreach ($value as $res){
echo $value = $key . "-" . $res."<br/>";
}
}
I have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)