Anyone have any idea why shuffle() would only return 1 item?
when using:
$array2 = shuffle($array1);
with the following array($array1):
Array
(
[0] => 1
[1] => 5
[2] => 6
[3] => 7
[4] => 8
[5] => 10
[6] => 11
[7] => 12
[8] => 13
[9] => 14
)
The output of:
print_r($array2);
is simply: 1
Any idea as to why it would not only not shuffle the array, but knock off the remaining 9 items in the array?
thanks!
shuffle() shuffles the array in place, and returns true if it succeeded. If you want $array2 to be a shuffled version of $array1, first make it a copy of $array1 and then call shuffle($array2);
See the docs: shuffle
shuffle changes the original array. So in your case the shuffled array is $array1.
$array2 is simply a boolean value. The function returns true or false.
Please read a function description before use http://php.net/shuffle
it may work other than you expect.
$array2 = $array1;
shuffle($array2);
print_r($array2);
Related
I have an array with some values (numeric values):
$arr1 = [1, 3, 8, 12, 23]
and I have another associative array that a key (which matches to a value of $arr1) correspond to a value. This array may contain also keys that don't match with $arr1.
$arr2 = [1 => "foo", 2 => "foo98", 3 => "foo20", 8 => "foo02", 12 => "foo39", 15 => "foo44", 23 => "foo91", 34 => "foo77"]
I want as return the values of $arr2 specifying as key the values of $arr1:
["foo", "foo20", "foo02", "foo39", "foo91"]
If possible, all this, without loops, using just PHP array native functions (so in an elegant way), or at least with the minimum number of loops possible.
Minimal loop is simple - 1. as:
foreach($arr1 as $k) {
$res[] = $arr2[$k];
}
You can do that with array_walk but I think this simple way is more readable.
If you insist you can do with array_filter + array_values + in_array as:
$res = array_values(array_filter($arr2,
function ($key) use ($arr1) { return in_array($key, $arr1);},
ARRAY_FILTER_USE_KEY
));
You can see this for more about filtering keys
To do it purely with array functions, you could do it as...
print_r(array_intersect_key($arr2, array_flip($arr1) ));
So array_flip() turns the items you want form the array into the keys for $arr1 and then uses array_intersect_key() to match the keys with the main array and this newly created array.
Gives...
Array
(
[1] => foo
[3] => foo20
[8] => foo02
[12] => foo39
[23] => foo91
)
If you don't want the keys - add array_values() around the rest of the calls...
print_r(array_values(array_intersect_key($arr2, array_flip($arr1) )));
to get
Array
(
[0] => foo
[1] => foo20
[2] => foo02
[3] => foo39
[4] => foo91
)
Although as pointed out - sometimes a simple foreach() is just as good and sometimes better.
I have lot arrays and i want to only print number of 2 and first arrays
i use array_slice but there is still a problem
Arrays:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Array
(
[0] => 571
[1] => Roods
)
I need to like this:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Basically you seem to only need:
array_slice(array_unique(array_column($Myarrays, 'nidtitle')), 0, 2);
This should be done instead of the entire code you use to generate the arrays.
Short explanation:
array_column will get the element nidtitle from each "row" (array entry) in $Myarrays
After that we run that column through a unique function
Then we get the first 2 elements with an array_slice
This should do the job:
$finalarray = array_slice($Myarray, 0, 2);
print_r($finalarray);
You can use array_slice to get the items you want.
$arr = array(Array(441,"Awesome"),
Array(570,"Noons"),
Array(571,"Roods"));
$two = array_slice($arr, 0,2);
Var_dump($two);
Array_slice second parameter is where the slice should start.
Third parameter is count of values to slice.
https://3v4l.org/NPcJT
please view the following code with 2 arrays. i use multisort function with sort flags for ascending and numeric then display. as you can see in the output that array 2 starts with 100 when it should be last. please explain what is causing this and how to sort it correctly. thank you.
<?php
$array1 = array(1,7,10,6);
$array2 = array(100,20,25,10);
array_multisort($array1, SORT_ASC, SORT_NUMERIC, $array2);
print_r($array1);
echo "<br>";
print_r($array2);
?>
output:
Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 10 )
Array ( [0] => 100 [1] => 10 [2] => 20 [3] => 25 )
Ah, yes, array_multisort is a bit tricky to understand the first time round.
Basically the sort is lexicographical, a fancy word meaning that the first array is sorted and the second arrays elements are ordered according to the first array.
Look at your first (output) array and see the order and map it to the initial second array and you'll see whats happening.
So the second array you take the 1st, 4th , 2nd and 3rd elements.
If you just want plain sorting for multiple arrays then just do them one by one or over a loop.
I'm trying to get all array elements, where the value only occurs once in the array.
I tried to use:
array_unique($array);
But this does only remove the duplicates, which is not what I want.
As an example:
$array = 0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 2
6 => 3
7 => 4
8 => 5
Expected output:
array(
0=>1
)
As you can see only the value 1 occurs once in the array, all other values are more than once in the array. So I only want to keep that one element.
This should work for you:
First use array_count_values() to count how many times each value is in your array. This will return something like this:
Array (
[1] => 1
[2] => 2
[3] => 2
[4] => 2
[5] => 2
// ↑ ↑
// Value Amount
)
After that you can use array_filter() to only get the values, which occurs once in your array. Means:
Array (
[1] => 1
[2] => 2
[3] => 2
[4] => 2
[5] => 2
)
And at the end simply use array_keys() to get the value from the original array.
Code:
<?php
$arr = [1,2,3,4,5,2,3,4,5];
$result = array_keys(array_filter(array_count_values($arr), function($v){
return $v == 1;
}));
print_r($result);
?>
output:
Array (
[0] => 1
)
You can use array_count_values to get the number of times each value exists in the array. You can use this to get all the values that occur only once by looking at the value in the returned array.
If you already have an array and it is in the structure described above, you should be able to just array_search(1, $array) and it will give you the key of the array with the value of 1. Or if you expect to have multiple keys with the value of 1, you can use array_keys($array, 1) and it will return an array of keys that have the value of 1. Hope this helps.
I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php