I have an array. But trying to separate items. For example:
$array1 = ["banana","car","carrot"];
trying to push car into another array which is $array2
$push = array_push($array1, "car") = $array2;
I try to find array_push usage for this but the documentation is all about. sending new item to array. Not array to array. Is this possible with array_push or need to use something else?
I need to search for the value car in $array1, push it into $array2 and remove it from $array1.
Here is a flexible solution that allows you to search by "car", then IF it exists, it will be pushed into the second array and omitted from the first.
Code: (Demo)
$array1 = ["banana","car","carrot"];
$needle = "car";
if (($index = array_search($needle, $array1)) !== false) {
$array2[] = $array1[$index];
unset($array1[$index]);
}
var_export($array1);
echo "\n---\n";
var_export($array2);
Output:
array (
0 => 'banana',
2 => 'carrot',
)
---
array (
0 => 'car',
)
You can push $array1 item car into $array2 as like below
$array2 = array();
$array1 = ["banana","car","carrot"];
array_push($array2, $array1[1]);
print_r($array2); /* OutPut Array ( [0] => car ) */
As you know $array1[1] has value is car so it will push it into $array2 by suing php built-in function array_push()
Hope this will help you.
$array1 = ["banana","car","carrot"];
$array2 = array_slice($array1, 1, 1);
unset($array1[1]);
Related
I have two array. I want to remove if 2nd array exists in 1st array. For example
array1 = array ("apple","banana","papaya","watermelon","avocado");
array2 = array ("apple","avocado");
I want the output should be
Array ( [1] => banana [2] => papaya [3] => watermelon)
Here are some code that I'd tried.
foreach($array2 as $key){
$keyToDelete = array_search($key, $array1);
unset($array1[$keyToDelete]);
}
print_r($array1);
but the output is
Array ( [1] => banana [2] => papaya [3] => watermelon [4] =>avocado )
It only remove first element.
i also tried to do something like this
$result = array_diff($array1,$array2);
print_r($result);
but the output is it print all element in array1
Noted: I want the result need to be outside foreach loop
array_diff should be work.
<?php
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$array_diff = array_diff($array1, $array2);
print_r($array_diff);
?>
DEMO
output will be.
Array ( [1] => banana [2] => papaya [3] => watermelon)
You can also try below solution. result will be same.. using in_array Check if first array value not in the second tester that value in the new array 'final_result' for results.
in_array support (PHP 4, PHP 5, PHP 7)
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$final_result = array();
foreach($array1 as $value){
if(!in_array($value, $array2)){
$final_result[] = $value;
}
}
print_r($final_result);
?>
DEMO
With the help of array_filter() we can do it easily. It filters elements of an array using a callback function.
array_filter() iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
Here we have used use($array2) clause to access the external variable inside callback function. $array2 is needed to filter out $array1.
$array1 = array("apple","banana","papaya","watermelon","avocado");
$array2 = array("apple","avocado");
$array1 = array_filter($array1, function($item) use($array2) { return !in_array($item, $array2); });
print '<pre>';
print_r($array1);
Demo
The fastest way to do this is to create a set(associative array) of elements in $array2 and iterate over $array1 and check if element in $array1 exists in our set or not using isset(). We take advantage of the method/algorithm technique called hashing.
<?php
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$set = [];
foreach($array2 as $element){
$set[$element] = true;
}
$result = [];
foreach($array1 as $element){
if(!isset($set[$element])){
$result[] = $element;
}
}
print_r($result);
Demo: https://3v4l.org/PcS45
Here is an example array I want to split:
(1428,217,1428)
How do I split it in 2 array like this?
(1428,1428)
(217)
I have tried following way but it's only return 1428 array.
$counts = array_count_values($array);
$filtered = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
One way to solve this for your example data is to sort the array and use array_shift to get the first element of the array and store that in an array.
$a = [1428,217,1428];
sort($a);
$b = [array_shift($a)];
print_r($a);
print_r($b);
Result
Array
(
[0] => 1428
[1] => 1428
)
Array
(
[0] => 217
)
You can try this.
$array = array(1428,217,1428);
$array1 = array_slice($array, 0, 2);
$array2 = array_slice($array, 2, 3);
print_r($array1);
print_r($array2);
And the output will like this:-
Array
(
[0] => 1428
[1] => 217
)
Array
(
[0] => 1428
)
In your case it will only return 1428 since array_count_values returns an array with values as keys and their frequency as array value therefore $counts will be equal to array('1428' => 2, '217' => 1);
If I understood your question well you should do something like this:
$array1 = [1428, 217, 1428];
$result = [];
foreach($array1 as $value){
$result[$value][] = $value;
}
This will not create an array for each different value but will create a new element for each unique value in $result. The final value of $result will be array('1428' => [1428, 1428], '217' => [217]) . Which can be easily manipulated as if they were 2 different arrays.
Let me know if this works for you, if not I will try to update my answer according to your specification.
I am using the Flot jQuery plugin to create a graph on how many visitors there have been per platform. I would like to create a 4th line with total visitors, calculated by previously retrieved data.
I need to combine several multi-dimensional Indexed arrays, but not simply merging them recursively. I.E:
$arr1 = [[2016/05/04,2],[2016/05/03,4],[2016/05/02,6]];
$arr2 = [[2016/05/04,1],[2016/05/03,3],[2016/05/02,2]];
$arr3 = [[2016/05/04,6],[2016/05/03,7],[2016/05/02,8]];
The output should be:
$arrTotal = [[2016/05/04,9],[2016/05/03,14],[2016/05/02,16]];
How do I accomplish this in a (fairly) simple way?
First of all, you cannot declare your dates the way you did:
$arr1 = [[2016/05/04,2],[2016/05/03,4],[2016/05/02,6]];
Because it's going to take 2016, divide it by 5 then divide it by 4. You need to put them into quotes.
$arr1 = [['2016/05/04',2],['2016/05/03',4],['2016/05/02',6]];
But to create an associative array, you should do it this way:
$arr1 = array('2016/05/04' => 2, '2016/05/03' => 4, '2016/05/02' => 6);
$arr2 = array('2016/05/04' => 1, '2016/05/03' => 3, '2016/05/02' => 2);
$arr3 = array('2016/05/04' => 6, '2016/05/03' => 7, '2016/05/02' => 8);
Now all you want to do, is loop through each array and sum them up.
$merge = array();
function mergeArray(Array &$merge, Array $array){
// Loop through each key and value
foreach($array as $key => $value)
// Make sure the value is numeric
if(is_numeric($value)){
if(!isset($merge[$key]))
$merge[$key] = $value;
else
$merge[$key] += $value;
}
}
mergeArray($merge, $arr1);
mergeArray($merge, $arr2);
mergeArray($merge, $arr3);
And now if you dump the $merge:
array(3) {
["2016/05/04"]=>
int(9)
["2016/05/03"]=>
int(14)
["2016/05/02"]=>
int(16)
}
Build a method that will sum the values by respecting the keys of existing values.
$arr1 = array('2016/05/04'=>2,'2016/05/03'=>4,'2016/05/02'=>6);
$arr2 = array('2016/05/04'=>1,'2016/05/03'=>3,'2016/05/02'=>2);
$arr3 = array('2016/05/04'=>2,'2016/05/03'=>7,'2016/05/02'=>8);
function array_sum(&$new_arr,$arr) {
foreach ($arr as $date_key => $num_value) {
// initialize date in new array with 0, if not done previously
if (! isset($new_arr[$date_key])) { $new_arr[$date_key] = 0; }
// add number for indexed element of array
$new_arr[$date_key] += $num_value;
}
}
$new_arr = array();
array_sum($new_array,$arr1);
array_sum($new_array,$arr2);
array_sum($new_array,$arr3);
You are trying to sum up every second value from each nested array relatively to their position in the parent array.There's a short and simple solution using array_map, array_sum and array_column functions:
$groupped = array_map(null, $arr1,$arr2,$arr3);
$result = array_map(function($v){
return [$v[0][0], array_sum(array_column($v, 1))];
}, $groupped);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => 2016/05/04
[1] => 9
)
[1] => Array
(
[0] => 2016/05/03
[1] => 14
)
[2] => Array
(
[0] => 2016/05/02
[1] => 16
)
)
I have two arrays,
$arr_1 = array(01=>5, 02=>3, 03=>2);
$arr_2 = array(01=>3, 02=>4, 03=>0);
what I want to achieve is to have a single array where the final form after adding the two arrays would be,
$arr_3 = array(01=>8, 02=>7, 03=>2);
I tried array_merge but it wasn't the solution.How would I attain the final form?
Try array_map. From the PHP Manual
array_map() returns an array containing all the elements of arr1
after applying the callback function to each one. The number of
parameters that the callback function accepts should match the number
of arrays passed to the array_map()
$arr_1 = array(01=>5, 02=>3, 03=>2);
$arr_2 = array(01=>3, 02=>4, 03=>0);
$arr_3 = array_map('add', $arr_1, $arr_2);
function add($ar1, $ar2){
return $ar1+$ar2;
}
print_r($arr_3);
OUTPUT:
Array ( [0] => 8 [1] => 7 [2] => 2 )
A for loop should handle this:
$max = count($arr_1);
$arr_3 = array();
for($i = 0; $i < $max; $i++){
$arr_3[$i] = intval($arr_1[$i]) + intval($arr_2[$i]);
}
I'm sure there are many other ways to do this, but this is the first one that came to mind. You could also do a foreach loop:
$arr_3 = array();
foreach($arr_1 as $k => $v){
$arr_3[$k] = intval($v) + intval($arr_2[$k]);
}
I'm just winging it here, the foreach is a little tricky to avoid the cartesian effects. Worth a shot though.
If you require adding elements matching by their key not by their position, you could try this:
$array1 = array(1=>5, 2=>3, 3=>2);
$array2 = array(3=>3, 2=>4, 1=>0); //unsorted array
$keys_matched = array_intersect_key ( $array1 , $array2);
foreach ($keys_matched as $key => $value) {
$result[$key] = $array1[$key] + $array2[$key];
}
print_r($result); //Displays: Array ( [1] => 5 [2] => 7 [3] => 5
You would look through both arrays and add each value of each array together then add that result to another array.
foreach($array1 as $val1) {
foreach($array2 as $val2) {
array_push($newArray, intval($val1)+ intval(val2));
}
}
Array is like this:
Array
(
[0] => 2011/10/05
)
Array
(
[0] => 2011/10/05
)
How can I get rid of the duplicate array?
php has a function
array_unique
http://php.net/manual/en/function.array-unique.php
EDIT
Option 1
<?php
$array1 = array("2011/10/05");
$array2 = array("2011/10/05");
$merged = $array1;
foreach($array2 as $v) array_push($merged,$v);
$unique = array_unique($merged);
?>
You could use array_merge, but the problem with array_merge is that it joins the same keys together which you probably don't want. The above code will add elements from array2 to array1 and then do array_unique (adding elements of array2 to array1 can also be done differently).
First merge and then unique
$a = array('2011/10/05');
$b = array('2011/10/05');
$c = array_merge($a,$b);
$d = array_unique($c);