I am trying to add(string) array values into single array, I have three array values like below:
Array ( [0] => 15 [1] => 16 )
Array ( [0] => jan [1] => feb )
Array ( [0] => 2012 [1] => 2012 )
and now i want those array in single array like below:
Array ( [0] => 15-jan-2012 [1] => 16-feb-2012 )
So how can i do this in php.
You need this custom code: Too simple, just a loop to access each and every array with there index and store in an array.
$d = array(15, 16);
$m = array("jan", "feb");
$y = array (2012, 2012);
$final = array();
for($i = 0; $i < count($d); $i++){
$final[] = $d[$i]. "-" .$m[$i]. "-" .$y[$i];
}
print_r($final);
Output:
Array ( [0] => 15-jan-2012 [1] => 16-feb-2012 )
you can do it using, array_merge_recursive, but has some difficulties.
ONLINE DEMO
Quick and dirty way, I am supposing that your each array has same length
$a = array ('15','16' );
$b = array ('jan','feb');
$c = array ('2012','2012');
$newArray = array();
for($i=0;$i<=count($a);$i++){
$newArray = $a[$i]. "-" .$b[$i]. "-" .$c[$i];
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
You need a custom code like this one for example:
$arr = [];
$arr1 = [15, 16, 17, ...];
$arr2 = [jan, feb, ...];
$arr3 = [2012, 2012, ...];
for($i=0, $count = count($arr1); i<$count; i++)
{
$arr[] = "$arr1[$i]-arr2[$i]-arr3[$i]";
}
var_dump($arr);
Use array_column to get the required dates array, finally concat it using implode. Something like
$arr1 = array(15, 16);
$arr2 = array('jan', 'feb');
$arr3 = array(2012, 2012);
$date = array($arr1, $arr2, $arr3);
$dates = array(array_column($date, 0), array_column($date, 1));
$reqDate = array();
foreach ($dates as $dateVal) {
$reqDate[] = implode('-', $dateVal);
}
var_dump($reqDate); //your required output
You can write it all in one swoop like so:
$a = [15, 16];
$b = ['jan', 'feb'];
$c = [2012, 2012];
$dates = array_map(function($arr) {
return implode('-', $arr);
}, array_map(null, $a, $b, $c)));
Related
I have an array with date & time like below:
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
From each value, the T10:00 should be cut off, so that my new array looks like this:
$new_array = array('2021-05-04', '2021-05-05', '2021-05-06');
How can i do that?
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = [];
foreach($array as $a) {
$a = explode('T', $a)[0];
array_push($new_array, $a);
}
Iterate through the array by array_map with callback function take only first 10 chars, Which represent time.
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>substr($time, 0, 10), $array);
print_r($new_array);
Prints:
/*
Array
(
[0] => 2021-05-04
[1] => 2021-05-05
[2] => 2021-05-06
)
*/
the T10:00 should be cut off
If you have a constant time T10:00 and want to get rid of it just replace it with empty!
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>str_replace('T10:00', '', $time), $array);
print_r($new_array);
//Array ( [0] => 2021-05-04 [1] => 2021-05-05 [2] => 2021-05-06 )
I want to add value to array and then I want to use these arrays in array intersect. Codes are in bellow. Where am I doing mistake?
$array =['1,2,3,4','3,4,5','2,3'];
$arr2 = [];
$common = [];
for($i=0; $i<count($array); $i++)
{
$arr1 = [];
if($i==0)
{
array_push($arr1, $array[$i]);
array_push($arr2, $array[$i]);
$common = array_intersect($arr1,$arr2);
}
else
{
array_push($arr1, $array[$i]);
$common = array_intersect($arr1,$common);
}
print_r($common);
}
Output is :
Array (
[0] => 1,2,3,4
)
Array ( )
Array ( )
I want to be this :
Array (
[0] => 1,2,3,4
)
Array(
[0] => 3,4
)
Array(
[0] => 3
)
Thanks,
Try This
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = [];
for($i=0; $i<count($array); $i++)
{
$j='arr'.$i;
$j= [];
if($i==0){
array_push($j, $array[$i]);
}
else{
$a = explode(',',$array[$i-1]);
$b = explode(',',$array[$i]);
$c = array_intersect($a,$b);
$d= implode(',',$c);
array_push($j, $d);
}
echo "<pre>"; print_r($j);
}
You are misusing array_intersect. This method does works on values in an array not on a single value. To use it the way You want You should split your values by comma and insert them as separate values. For example:
value: '1,2,3,4' should be inserted as:
$array = ['1', '2', '3', '4'];
Solution (without loops etc):
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = array();
$arr2 = array();
$common = array();
$arr1 = explode(',', $array[0]);
$arr2 = explode(',', $array[1]);
$common =array_intersect($arr1, $arr2);
print_r($common);
$arr3 = explode(',', $array[2]);
$common2 = array_intersect($common, $arr3);
print_r($common2);
?>
I have an array containing multiple arrays like
$A = array();
$A[0] = array("1","2","3","4","5");
$A[1] = array("1","6","7","8");
$A[2] = array("0","1","3");
I want to sort the values in multiple arrays in the order of frequency and put them into another array let's say $B.
The values in $B is "1","1","1","3","3","0","2","4","5","6","7","8".
$A = array();
$A[0] = array("1","2","3","4","5");
$A[1] = array("1","6","7","8");
$A[2] = array("0","1","3");
//Merging above array in to one array
$merged = array_values(call_user_func_array('array_merge', $A));
//Getting occurrence count
$counts = array_count_values($merged);
//Sort by count
arsort($counts);
//Adding to required array
$B = array();
foreach ($counts as $k => $v)
for($i=1;$i<=$v;$i++)
$B[] = $k;
echo "<pre>";
print_r($B);
echo "</pre>";
Result
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 3
[4] => 3
[5] => 0
[6] => 8
[7] => 7
[8] => 5
[9] => 2
[10] => 4
[11] => 6
)
First merge all arrays
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$resultado = array_merge($array1, $array2);
see -> http://php.net/manual/es/function.array-merge.php
Second sort the big array
arsort($resultado );
see -> http://php.net/manual/es/array.sorting.php
Use a hash table to count the frequency of each number, and then store them in the decreasing order of frequency in array $B, like this:
$hash_table = array();
foreach($A as $array){
foreach($array as $value){
if(empty($hash_table[$value])){
$hash_table[$value] = 1;
}else{
$hash_table[$value] += 1;
}
}
}
arsort($hash_table);
$B = array();
foreach($hash_table as $key => $value){
for($i = 0; $i < $value; ++$i){
$B[] = $key;
}
}
var_dump($B); // to see the contents of array $B
If order of same occurance count items isn't important, you can use:
// Merge all arrays
$counts = array_count_values(call_user_func_array('array_merge', $A));
// Sort by occurance
arsort($counts);
// Add [value] to new array [occurance] times
$B = array();
array_walk($counts, function($occurances, $value) use (&$B){
for($i=0;$i<$occurances;$i++) $B[] = $value;
});
echo implode(',', $B);
Output
1,1,1,3,3,0,8,7,5,2,4,6
Array print in order of count and index:
$temp = array();
foreach($A as $b){
foreach($b as $c){
if(isset($tmep[$c])){
$tmep[$c]++;
}else{
$tmep[$c] = 1;
}
}
}
function SortArrayByKeyThanValue (&$pArray, $pSortMethodForKey = SORT_ASC, $pSortMethodForValue = SORT_DESC){
# check user input: sorting is not necessary
if (count($pArray) < 2)
return;
# define $k and $v as array_multisort() needs real variables, as user input is put by reference
$k = array_keys ($pArray);
$v = array_values($pArray);
array_multisort(
$v, $pSortMethodForValue,
$k, $pSortMethodForKey
);
$pArray = array_combine($k, $v);
}
SortArrayByKeyThanValue($tmep);
$B = array();
array_walk($tmep, function($occurances, $value) use (&$B){
for($i=0;$i<$occurances;$i++) $B[] = $value;
});
echo implode(',', $B);
For example I have like more than 3 different arrays, with element like below:
1st array
hello-1
hi-1
2nd array
ok-two
hi-2
22-two
hello
3rd array
hi-3rd
hello3
And so on...
I want to combine this array in the order one by one. For example the expected output for the 3 arrays above would be:
hello-1
ok-two
hi-3rd
hi-1
hi-2
hello3
22-two
hello
I tried array_merge(). But it appends the 2nd array after the complete 1st array, which is not what I'm looking for, so here I'm kinda stuck and don't know which functions I can use here. Any hints or ideas?
This should work for you:
First I get the first element of each array into a sub array, then the second value into the next sub array and so on, that you get this structure of array:
Array
(
[0] => Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
)
//...
)
After this you can just loop through each array value with array_walk_recursive() and get every value into your array.
<?php
$arr1 = [
"hello-1",
"hi-1",
];
$arr2 = [
"ok-two",
"hi-2",
"22-two",
"hello",
];
$arr3 = [
"hi-3rd",
"hello3",
];
$arr = call_user_func_array("array_map", [NULL, $arr1, $arr2, $arr3]);
$result = [];
array_walk_recursive($arr, function($v)use(&$result){
if(!is_null($v))
$result[] = $v;
});
print_r($result);
?>
output:
Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
[3] => hi-1
[4] => hi-2
[5] => hello3
[6] => 22-two
[7] => hello
)
I have another way to solve this issue
<?php
$arr1 = array(
"hello-1",
"hi-1");
$arr2 = array("ok-two",
"hi-2",
"22-two",
"hello");
$arr3 = array(
"hi-3rd",
"hello3");
$max = count($arr1);
$max = count($arr2) > $max ? count($arr2) : $max;
$max = count($arr3) > $max ? count($arr3) : $max;
$result = array();
for ($i = 0; $i < $max; $i++) {
if (isset($arr1[$i])) {
$result[] = $arr1[$i];
}
if (isset($arr2[$i])) {
$result[] = $arr2[$i];
}
if (isset($arr3[$i])) {
$result[] = $arr3[$i];
}
}
print_r($result);
I have two multidimensional arrays that I need to determine the delta for each value. I know the array_diff function only returns the difference in keys. Is there a functon that will determine the delta for each set of values assuming the two arrays contain the same set of keys?
Example:
array_1(test1 => Array([key1] => 100, [key2] => 200 ) )
array_2(test1 => Array([key1] => 105, [key2] => 195 ) )
I would expect something like:
array_3(test1 => Array([key1] => 5, [key2] => -5 ) )
Are there any PHP methods to do this or am I on my own?
Answers here suggested using foreach loop but I think creating anonymous function will be easier:
<?php
$count_delta = create_function('$a,$b', 'return $a - $b;');
$arr1 = array(100, 200);
$arr2 = array(20, 180);
$delta = array_map($count_delta, $arr1, $arr2);
var_dump($delta);
Output will be:
array
0 => int 80
1 => int 20
$delta = array();
foreach( $array1 as $k=>$v )
{
if( array_key_exists( $k, $array2 )
{
// preserve the key
$delta[$k] = $array1[$k] - $array2[$k];
// or don't
$delta[] = $array1[$k] - $array2[$k];
}
}
print_r($delta);
There is no built-in function for that, but you can use this.
function delta_array($a, $b) {
if (sizeof($a) != sizeof($b))
return false;
$arr = array();
for ($i=0; $i < $c = sizeof($a); $i++)
$arr[] = $b[$i] - $a[$i];
return $arr;
}
$arr1 = array(100,200);
$arr2 = array(105,195);
$delta = delta_array($arr1, $arr2);
print_r($delta);
The above will return
Array
(
[0] => -5
[1] => 5
)