I have an array like this :
array (size=3)
0 =>
array (size=2)
'score' => float 17.891873624039
1 =>
array (size=2)
'score' => float 17.883449353824
2 =>
array (size=2)
'score' => float 4.702030124427
and have a while loop like this:
$offset=1;
while($scoreList[$rank+$offset]!=NULL && $scoreList[$rank]["score"]==$scoreList[$rank+$offset]["score"])
{
//do something
$offset++;
}
I need to check if the next index in the array exists and at the same time check if it is equal to the current object but I get
Undefined offset:4
I cannot put the second condition in an if statement because offset should be increased if these two indexes are equal so it falls in an infinite loop if I put it in if clause.
Properly these two could be useful :)
isset(); - php.net
if(isset($value[0])){
//DO SOMETHING IF VALUE IS SET
}
if(!isset($value[0])){
//DO SOMETHING IF VALUE ISN'T SET
}
empty(); - php.net
if(empty($value[0])){
//DO SOMETHING IF VALUE / VARIABLE DOESN'T EXIST
}
if(!empty($value[0])){
//DO SOMETHING IF VALUE / VARIABLE DOES EXIST AND ISN'T EMPTY
}
Related
Can anyone explain why running mysql_fetch_array($result,MYSQL_BOTH) on a result sometimes means that key[0] = 0 whilst at other times it ends up with a string/value. See example array below:
Array
(
[0] => 0
[groupTitle] => TEST 5
)
In this instance I would expect 0 to equal 'TEST 5'
The reason was because I wanted Mysql to return '0' as one of the elements/fields. So my query read something like this:
SELECT field1,field2,'0',field3 FROM table
What PHP did was returned the first element in the array with a value '0' but then set the first string key with the correct first element. I believe that may be a bug.
I have the following define in my code
define('SERVICE', 1);
When I now initialize an array like this
$serviceLimit[SERVICE_PAGECHECK][0] = 0;
and now do a var_dump on $serviceLimit in my mind it should output it like this
array (size=1)
'0' =>
array (size=1)
0 => int 0
However, it currently looks like this
array (size=1)
'SERVICE' =>
array (size=1)
0 => int 0
How can this be? Why is the array index using the name of the variable rather than the value?
Your code seems strange, because of using 2 different constants: SERVICE and SERVICE_PAGECHECK. Just correct the constant name.
I have this array of arrays
array (size=2)
'login' =>
array (size=3)
20 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
5 =>
array (size=1)
0 =>
object(Closure)[5]
I'm trying to reorder the position of the keys in the second level arrays (where it says 20, 10, 5) so the result should be
array (size=2)
'login' =>
array (size=3)
5 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
20 =>
array (size=1)
0 =>
object(Closure)[5]
The problem is that I can't figure out how to do that so please help
You need to do
ksort($array['login']);
This will sort the array keys so they are ascending. If you wanted them descending you would do
krsort($array['login']);
PHP has lots of handy functions for sorting arrays
You could have a look at Array Sorting and find out, that 'ksort' is your choice ;-)
Try PHPs ksort function on your second level array: http://php.net/ksort
It will sort your array by key while maintaining the key-data-associations
PHP has a variety of array sorting functions.
In your case, the most appropriate would seem to be ksort(), which is summarised thus:
Sorts an array by key, maintaining key to data correlations.
As pointed out in the comments, your particular example is already in the reverse of the required order; if this can be guaranteed, you could also use array_reverse(), being sure to pass the optional $preserve_keys parameter as true.
Specifically, you want to re-order the sub-array with the key 'login'; assuming your overall array is called $data, you would want to write this:
ksort($data['login']);
// note no need for an assignment, as PHP's sort functions act in-place
or this:
$data['login'] = array_reverse($data['login'], true);
// assign back to the original variable, as array_reverse() returns a new array
I am new to php. Please let me know how do I count number of array entry in under array 80cb936e55e5225cd2af.
array
'status' => int 1
'msg' => string '2 out of 1 Transactions Fetched Successfully' (length=44)
'transaction_details' =>
array
'80cb936e55e5225cd2af' =>
array
0 =>
array
...
1 =>
array
...
Assuming your array variable is $arr : ...
$count = count($arr['transaction_details']['80cb936e55e5225cd2af']);
(But this will only count the indexes/integers of that sub-array, not the values inside them!)
You can use PHP's count() like this
$noOfEntries = count($array['transaction_details']['80cb936e55e5225cd2af']);
This will get you the number of arrays inside 80cb936e55e5225cd2af (not the overall amount of elements).
Hey all i am trying to get the data from a movie API. The format is this:
page => 1
results =>
0 =>
adult =>
backdrop_path => /gM3KKixSicG.jpg
id => 603
original_title => The Matrix
release_date => 1999-03-30
poster_path => /gynBNzwyaioNkjKgN.jpg
popularity => 10.55
title => The Matrix
vote_average => 9
vote_count => 328
1 =>
adult =>
backdrop_path => /o6XxGMvqKx0.jpg
id => 605
original_title => The Matrix Revolutions
release_date => 2003-10-26
poster_path => /sKogjhfs5q3aEG8.jpg
popularity => 5.11
title => The Matrix Revolutions
vote_average => 7.5
vote_count => 98
etc etc....
How can i get only the first element [0]'s data (as in backdrop_path, original_title, etc etc)? I'm new at PHP arrays :).
And of course this is what i used to output my array data:
print_r($theMovie)
Any help would be great!
Another solution:
$arr = reset($datas['results']);
Returns the value of the first array element, or FALSE if the array is empty.
OR
$arr = current($datas['results']);
The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE.
You can point to the array with this $theMovie['result'][0]['backdrop_path']; or you can loop through it like this,
foreach($theMovie['results'] as $movie){
echo $movie['backdrop_path'];
}
You can use array_shift to pop off the first element, then check that it is valid (array_shift will return null if there are no results or the item isn't an array).
$data = array_shift($theMovie['results']);
if (null !== $data) {
// process the first result
}
If you want to iterate though all the results, you can do a foreach loop while loop with array_shift.
foreach($theMovie['results'] as $result) {
echo $result['backdrop_path'];
}
while ($data = array_shift($theMovie['results'])) {
echo $data['backdrop_path'];
}
Or just use $theMovie['result'][0]['backdrop_path']; as already suggested, after checking that $theMovie['result'][0] is actually set.
Assuming all this code is stored in a variable $datas :
$results = $datas['results'];
$theMovie = $results[0];
Try
$yourArray['results'][0]
But keep in mind this produces errors when the result array is empty.