PHP Array Merge and remove duplicate [duplicate] - php

This question already has answers here:
Merge two arrays containing objects and remove duplicate values
(7 answers)
Closed 6 months ago.
the below are the array am getting out i want to mearge array and remove duplicats of it and pass too for loop not able too do i just want is remove duplicate and pass too for loop as a unique value
i tried with
print_r(array_merge($uniq_arr));
for($i = 0; $i < count($uniq_arr); $i++) {
$tag = $uniq_arr[$i];
}
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => water intake
[1] => hygiene
[2] => diet
)
Out put in tag shold be like each unique value

You could just use array_merge() indiscriminately then use array_unique() to remove any duplicate entries.

Here is how you could implement the array_merge, array_unique solution. I am making an assumption that you have 4 arrays like so:
$tag1Array = array('diet','exercise');
$tag2Array = array('diet','exercise');
$tag3Array = array('diet','exercise');
$tag4Array = array('water intake','diet','exercise');
$commontags = array_unique(array_merge($tag1Array,$tag2Array,$tag3Array,$tag4Array));
This should result in a single array of the unique values present in any of the 4 tag arrays.

Related

Get array of user IDs from multi-dimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

sort in explode variable [duplicate]

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

Remove the array that is already in another array [duplicate]

This question already has answers here:
difference between two arrays
(7 answers)
Closed 7 years ago.
How can i remove the elements of array that is from another array
Here is my Array Alpha ["1","2","4"]
And the Array Beta ["1"]
How can i remove the elements of Array Alpha that already contains in Array Beta
i.e., After removal it will remove the 1 as it is contained in Array Beta and returns only ["2","4"]
By using array_diff:
array_diff($alpha, $beta);
Working example: http://3v4l.org/fZRGD
$a = [1,2,4];
$b = [1];
$a = array_diff($a,$b);
print_r($a);
Yields:
Array ( [1] => 2 [2] => 4 )
And if you don't want to preserve original keys, you take only array of values:
$a = array_values(array_diff($a,$b));
print_r($a);
Then it gives:
Array ( [0] => 2 [1] => 4 )

Random selection from sub array [duplicate]

This question already has answers here:
Selecting a random element from a PHP associative array
(4 answers)
Closed 8 years ago.
I need to select a random item (img1, img2, etc.) from simple nested arrays. I'm sure this is easy but I am stumped. The array has this format:
Array
(
[0] => Array
(
[homepage_image] => img1
)
[1] => Array
(
[homepage_image] => img2
)
)
$fields is the name of the main array.
I've tried using:
$random = array_rand($fields);
But of course that just gives me 0 or 1. How do I randomly get img1, img2, etc?
Use array_rand() to find a random key of your array:
$image = $fields[array_rand($fields)]['homepage_image'];

php reassign array contents [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
any particular function or code to put this kind of array data
ori [0] => 43.45,33,0,35 [1] => 74,10,0,22 [2] => 0,15,0,45 [3] => 0,0,0,340 [4] => 12,5,0,0 [5] => 0,0,0,0
to
new [0] => 43.45,74,0,0,12,0 [1] => 33,10,15,0,5,0 [2] => 0,0,0,0,0,0, [3] => 35,22,45,340,0,0
As you can see, the first value from each ori are inserted into the new(0), the second value from ori are inserted into new(1) and so on
If $ori is an array of arrays, this should work:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$newArray = transpose($ori);
Note: from Transposing multidimensional arrays in PHP
If $ori is not an array of arrays, then you'll need to convert it first (or use the example by Peter Ajtai), like this:
// Note: PHP 5.3+ only
$ori = array_map(function($el) { return explode(",", $el); }, $ori);
If you are using an older version of PHP, you should probably just use the other method!
You essentially want to transpose - basically "turn" - an array. Your array elements are strings and not sub arrays, but those strings can be turned into sub arrays with explode() before transposing. Then after transposing, we can turn the sub arrays back into strings with implode() to preserve the formatting you want.
Basically we want to go through each of your five strings of comma separated numbers one by one. We take each string of numbers and turn it into an array. To transpose we have to take each of the numbers from a string one by one and add the number to a new array. So the heart of the code is the inner foreach(). Note how each number goes into a new sub array, since $i is increased by one between each number: $new[$i++][] =$op;
foreach($ori as $one) {
$parts=explode(',',$one);
$i = 0;
foreach($parts as $op) {
$new[$i++][] =$op;
}
}
$i = 0;
foreach($new as $one) {
$new[$i++] = implode(',',$one);
}
// print_r for $new is:
Array
(
[0] => 43.45,74,0,0,12,0
[1] => 33,10,15,0,5,0
[2] => 0,0,0,0,0,0
[3] => 35,22,45,340,0,0
)
Working example

Categories