PHP array map to anther array [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to map
Array
(
[0] => Array
(
[cid] => 1
[value] => red
)
[1] => Array
(
[cid] => 2
[value] => green
)
[2] => Array
(
[cid] => 3
[value] => pink
)
[3] => Array
(
[cid] => 4
[value] => yellow
)
)
To:
Array
(
[0] => 2
[1] => 3
[2] => 1
)
I need to map the second arrays value [0] => 2 to the cid in the first array. In other words I need 2 to map to value green.
Any help? THANK YOU.

I would first change the first array to something that is easier to check.
<?php
$temp = array();
foreach ($array1 as $val) {
$temp[$val['cid']] = $val['value'];
}
?>
Now you have an array:
$temp[1] = "red";
$temp[2] = "green";
$temp[3] = "pink";
$temp[4] = "yellow";
Then you can easily use that in the second array
<?php
$new= array();
foreach ($array2 as $key=>$val) {
$new[$key] = $temp[$val];
}
?>
Codepad example

PHP >= 5.5.0
$colors = array_column($first, 'value', 'cid');
foreach($second as $value) {
if(isset($colors[$value])) {
echo $colors[$value];
}
}
Where $first is your first array and $second obviously the second.

Related

Change keys to multidimensional array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Here is my array
Array
(
[0] => Array
(
[0] => 710715609
[1] => 3
[2] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[3] => 2
[4] => sports/Mtunes/Astro/D50
[5] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[0] => 119774100
[1] => 2
[2] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[3] => 1
[4] => sports/D50
[5] => sports-1/D50-1
)
)
I want to change these 5 keys in array to (msisdn, logCount, logins, transc, actVas) like this and I have tried by using array_fill_keys
Is there a way to do this ?
You can map and combine:
$array = array_map(function($v) {
return array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
}, $array);
Or walk and combine:
array_walk($array, function(&$v) {
$v = array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
});
You can do either one with a defined array and use() like:
$k = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
$array = array_map(function($v) use($k) { return array_combine($k, $v); }, $array);
//or
array_walk($array, function(&$v) use($k) { $v = array_combine($k, $v); });
Assuming you may have a very large array, this will just replace the existing arrays keys rather than making another copy of the array.
$pushed = [
[ 710715609, 3, '2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01', 2, 'sports/Mtunes/Astro/D50', 'sports-1/Mtunes-1/Astro-1/D50-2'],
[ 119774100, 2, '2020-02-22 00:01:01/2020-02-22 00:01:01', 1, 'sports/D50', 'sports-1/D50-1']
];
$names = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
foreach ($pushed as $x => $push){
foreach ($push as $i => $v) {
unset ($pushed[$x][$i]);
$pushed[$x][$names[$i]] = $v;
}
}
print_r($pushed);
RESULT
Array
(
[0] => Array
(
[msisdn] => 710715609
[logCount] => 3
[logins] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[transc] => 2
[actVas] => sports/Mtunes/Astro/D50
[vasCount] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[msisdn] => 119774100
[logCount] => 2
[logins] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[transc] => 1
[actVas] => sports/D50
[vasCount] => sports-1/D50-1
)
)

Get the logged username according to the loggedtime using php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
o/p:Here am getting the username,logged time and date
Array
( [cena] => Array
(
[CentaurOne] => Array
(
[0] => 29/12/17 01:02:15
[1] => 13/01/18 09:23:21
)
[count] => 2
)
[Bhumi] => Array
(
[CentaurOne] => Array
(
[0] => 29/12/17 01:21:30
[1] => 29/12/17 09:23:11
)
[count] => 2
)
[John] => Array
(
[CentaurOne] => Array
(
[0] => 16/01/18 09:24:01
[1] => 18/01/18 02:51:20
[2] => 18/01/18 01:02:04
[3] => 19/01/18 01:02:04
)
[count] => 3
)
)
Now i needed to get the username according to the users logged in time like given below:
cena 29/12/17 01:02:15
John 18/01/18 01:02:04 ,19/01/18 01:02:04
You could try something like this :
$time = ' 01:02'; // note the begining space
// or use : $time = '01:02:';
$matches = [] ;
foreach ($logs as $login => $data) {
foreach ($data as $name => $dates) {
if (! is_array($dates)) continue ; // avoid search in 'count'
foreach ($dates as $date) {
if (strpos($date, $time) !== false) {
$matches[$login][] = $date ;
}
}
}
}
var_dump($matches) ;

PHP Array Readable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an Array format.
Array
(
[0] => Array
(
[order_id] => 1
)
[1] => Array
(
[order_id] => 11
)
[2] => Array
(
[order_id] => 12
)
)
It should to be changed the array format to be like this format:
[1,11,12];
Please help. Thank you in advanced.
For (PHP 5 >= 5.5.0, PHP 7) you can use array_column and lower than that you can use array_map function (for PHP < 5.3) you need to define saperate function for array_map instead of anonymous function.
$array = array
(
'0' => array
(
'order_id' => 1
),
'1' => array
(
'order_id' => 11
),
'2' => array
(
'order_id' => 12
)
);
$new_array = array_column($array, 'order_id');
print_r($new_array);
$new_array = array_map(function($element) {
return $element['order_id'];
}, $array);
print_r($new_array);
output
Array
(
[0] => 1
[1] => 11
[2] => 12
)
How about this?
$arr = Array
(
0 => Array
(
'order_id' => 1
),
1 => Array
(
'order_id'=> 11
),
2 => Array
(
'order_id' => 12
),
);
$newArr = array();
foreach($arr as $x){
foreach($x as $y){
$newArr[] = $y;
}
}
You can try this:
$ar = array(array('order_id' => 1), array('order_id' => 11), array('order_id' => 12));
$temp = array();
foreach ($ar as $val) {
$temp[] = $val['order_id'];
}
print_r($temp);
Basically, you are looking to flatten the array. This SO answer has some tips on how to do that. Also, there's a golf version in this Github gist.
For the specific case in your question, this should do
function specificFlatten($arr) {
if (!is_array($arr)) return false;
$result = [];
foreach ($arr as $arr_1) {
result[] = $arr_1['order_id'];
}
return $result;
}

PHP Array Elements Sum [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Array
(
[Item 1] => Array
(
[0] => 8
[1] => 5
[2] => 4
)
[Item 2] => Array
(
[0] => 24
[1] => 4
)
[Item 3] => Array
(
[0] => 23
[1] => 4
)
}
How do I add the array elements together so they become
Array
(
[Item 1] => Array
(
[0] => 17
)
[Item 2] => Array
(
[0] => 28
)
[Item 3] => Array
(
[0] => 27
)
}
I previously use array_merge_recursive to merge 2 array of the same keys together, but now I wanna add their elements together so they only have element [0].
Thanks!
You can use array_map and array_sum to achieve your goal:
$array = array('Item1' => array(1, 3), 'Item2' => array(3, 4));
$array = array_map(function($item) {
return array_sum($item);
}, $array);
echo '<pre>';
var_dump($array);
echo '</pre>';
The result would be:
array(2) {
["Item1"]=>
int(4)
["Item2"]=>
int(7)
}
If you really need the result to be an array with the sum of the items then change the return to:
return array(array_sum($item));
Maybe you can do long way like this
foreach($array as $key => $element){
$sum = 0;
foreach($element as $value){
$sum += $value;
}
$result[$key][0] = $sum;
}
Basically like this you will browse thru every element of array and you will create sum of desired elements

PHP how to populate array from array by value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Thanks lets say i have array like this
$array = array('1','1','3','1','2','3','2','2','2','3');
i want to create arrays from the above array but of common elements like
$arr1 = ('1','1','3');
$arr2 = ('1','2','3');
$arr3 = ('2','2','2','3');
where conditions finding value 3 from array.
Is there such a PHP function, I had a browse of the manual but could not see what I was looking for.
I'm not sure what did you mean, but I guess this code will meet your needs
$result = array();
$needle = '3';
$haystack = array('1','1','3','1','2','3','2','2','2','3');
$tmp = array();
foreach ($haystack as $var){
$tmp[] = $var;
if ($var == $needle){
$result[] = $tmp;
$tmp = array();
}
}
if (!empty($tmp)){
$result[] = $tmp;
}
And the result is
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 3
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 3
)
)

Categories