get element N from array_count_values php - php

I have an array on which I did array_count_values, and then an arsort.
$a example: $a = array('ten','ten','ten','three','two',one','ten','four','four');
I want to get the first element, and tried $a[0] but this did not work.
What is the correct syntax to get the first element please?
EDIT - added array
EDIT2 - Also, underlying array not allowed to be changed because their is code following that uses the associative array...

depending on whether or not you want to alter the array, you can use array_slice() or array_shift()

you can use array_values as
$a = array_values($a);
var_dump($a[0])

Same as in your last question.
key($a); // or each() for the first key=>value pair
Or alternatively:
$k = array_keys($a);
print $k[0];

ok I used this:
$a = array('ten','ten','ten','three','two','one','ten','four','four');
$bb= array_count_values ($a);
arsort($bb);
echo reset($bb);
It only works for getting the first element...Thank you all.

These are the ways of getting the first element:
$next = $arr[0];
$next = array_shift($arr);
$next = current($arr); // if the internal cursor is at position 0

Related

How to export each subarrays as an array in PHP

is it possible that, get arrays to $value with key:
Example:
$array = Array("one"=>Array("field1"=>"value1","field2"=>"value2"),
"two"=>Array("field3"=>"value3","field4"=>"value4"));
Export arrays to value:
$first = any_main_php_function_name(0,$array);
$second = any_main_php_function_name(1,$array);
Result:
$first = Array("field1"=>"value1","field2"=>"value2");
$second = Array("field3"=>"value3","field4"=>"value4");
Basically, I wanna extract multiple array. If there is no such function (any_main_php_function_name) in PHP so How can i extract above $array.
You don't need any funtion to do that. Simply get subarrays like this:
$first = $array['one'];
$second = $array['two'];
Unless you don't know the one,two keys, then you can use array_shift to get first item (one subarray). Remember that this functions also removes returned value from root array.
array_slice does what you want
$first = array_slice($array, 0, 1);
$second = array_slice($array, 1, 1);
print_r($first);
print_r($second);
May be you can array_shift to get the element from array, Like this:
$first_element=array_shift($array);
Make sure it only removes the first element from the array and
return the value of removed element.
And if you don't want to remove the element or get the sub-array in any sequence, then you can create a function like this,
function myFunction($index,$array) {
$keys = array_keys($array);
$sub_array=$arr[$keys[$index]];
}
In above function we just get the keys in a array and then use the known index to get the sub-array using keys array.
Please check this
foreach($array["one"] as $key=>$val){
echo "key=>".$key." Values=>".$val;
}
foreach($array["true"] as $key=>$val){
echo "key=>".$key." Values=>".$val;
}

Remove all elements before x from an array php

if I give you:
$array = array(object1, object2, object3, object4);
and say, at position 2, remove all elements before this position so the end result is:
$array = array(object3, object4);
What would I do? I was looking at array_shift and array_splice to achieve what I wanted - how ever I am not sure which to use or how to use them to achieve the desired affect.
Use array_slice. For more detail check link http://php.net/manual/en/function.array-slice.php
$array = array(object1, object2, object3, object4);
$array = array_slice($array,2); // 2 is position
Or if you are looking into the values instead of the index, with a tiny adjustment in #Ashwani's answer you can have:
$array = array('object1', 'object2', 'object3', 'object4');
$slice = array_slice($array, array_search('object3',$array));
array_slice is one way to go about it, however, if you are wanting to remove all the elements in an any array before a specific value, without searching, then:
//assuming you've already verified the match is in the array
//make a copy of $array first if you don't want to break the original
while($array[0] !== $match) {
array_shift(&$array);
}
Alternatively, you could:
$index = array_search($match, array_values($array));
if($index !== false) $array = array_slice($array, $index);
This accomplished both the verification and slice. Note the array_values() is used to account for associative arrays.

PHP Single line array shuffle is not working

Very simple but I was wondering why this is not working.
I'm trying shuffle an array and output the results (in a single line structure)
this is my code :
echo shuffle(array("A","B","C"))[0];
Small tweak needed here ;)
Your basic logic is a little bit wrong. You're interested in only one value, I assume? To solve it with that logic in mind, you can do it like this:
echo array_rand(array_flip(['A', 'B', 'C']));
Try below code
$arr = array("A","B","C");
shuffle($arr);
echo $arr[0];
I know that this is not the best solution for you, but it works!
print_r( ( $b=array('A', 'B', 'C') ) && shuffle($b) ? next($b) : null );
How this works:
Assign the array to the variable $b
Shuffle the variable $b
If the shuffle() succeeded:
return the next element in the array
If the shuffle() failed:
return null
Some might think: "Why didn't he used the current() function?"
Well, it seems that the function shuffle simply changes the order of the keys, but the pointer is always pointing to the same element. This means that current() will always return 'A'.
Apparently, this behaviour changed on PHP 5.4 to set the pointer to the first element.
TRY THIS SIMPLE FUNCTION
$my_array = array("A","B","C","D","E");
shuffle($my_array);
print_r($my_array);
You will need to pass the array in a seperate variable. Also, shuffle() itself just returns a boolean value, so you need to return an array element instead of the output of the function.
$ar = array("A","B","C");
shuffle($ar);
echo $ar[0];

PHP: What is the fastest and easiest way to get the last item of an array?

What is the fastest and easiest way to get the last item of an array whether be indexed array , associative array or multi-dimensional array?
$myArray = array( 5, 4, 3, 2, 1 );
echo end($myArray);
prints "1"
array_pop()
It removes the element from the end of the array. If you need to keep the array in tact, you could use this and then append the value back to the end of the array. $array[] = $popped_val
try this:
$arrayname[count(arrayname)-1]
I would say array_pop In the documentation: array_pop
array_pop — Pop the element off the end of array
Lots of great answers. Consider writing a function if you're doing this more than once:
function array_top(&$array) {
$top = end($array);
reset($array); // Optional
return $top;
}
Alternatively, depending on your temper:
function array_top(&$array) {
$top = array_pop($array);
$array[] = $top; // Push top item back on top
return $top;
}
($array[] = ... is preferred to array_push(), cf. the docs.)
For an associative array:
$a= array('hi'=> 'there', 'ok'=> 'then');
list($k, $v) = array(end(array_keys($a)), end($a));
var_dump($k);
var_dump($v);
Edit: should also work for numeric index arrays

Referencing for element in a PHP array

Answer is not $array[0];
My array is setup as follows
$array = array();
$array[7] = 37;
$array[19] = 98;
$array[42] = 22;
$array[68] = 14;
I'm sorting the array and trying to get the highest possible match out after the sorting. So in this case $array[19] = 98;
I only need the value 98 back and it will always be in the first position of the array. I can't reference using $array[0] as the 0 key doesn't exist. Speed constraints mean I can't loop through the array to find the highest match.
There also has to be a better solution than
foreach ( $array as $value )
{
echo $value;
break;
}
$keys = array_keys($array);
echo $array[$keys[0]];
Or you could use the current() function:
reset($array);
$value = current($array);
You want the first key in the array, if I understood your question correctly:
$firstValue = reset($array);
$firstKey = key($array);
You can always do ;
$array = array_values($array);
And now $array[0] will be the correct answer.
If you want the first element you can use array_shift, this will not loop anything and return only the value.
In your example however, it is not the first element so there seems to be a discrepancy in your example/question, or an error in my understanding thereof.
If you're sorting it, you can specify your own sorting routine and have it pick out the highest value while you are sorting it.
$array = array_values($array);
echo $array[0];

Categories