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.
Related
I have a section of an array defined:
$arrData['categories'] = array();
array_push($arrData['categories'],array("category" => array(array("label"=>"Beef"),array("label"=>"Chicken"))));
array_push($arrDAta['categories'][0]['category'],array("label"=>"pork"));
The arrays are nested so that it encodes into JSON properly with all the {[{[{[]}]}]} formatted properly.
However, I need to create these types of meats dynamically, not statically. When I try to add onto the array in the third line of code to simulate dynamic creation, it throws an error:
Warning: array_push() expects parameter 1 to be an array, null given
Well, $arrData['categories'][0]['category] IS an array, so why does it shoot me down?
To show myself that I'm not crazy, I var_dump $arrData['categories'][0]['category'] and get an array of size 1:
array (size=1)
'category' =>
array (size=2)
0 =>
array (size=1)
'label' => string 'Beef' (length=4)
1 =>
array (size=1)
'label' => string 'Chicken' (length=7)
Its the capitalization. In the third line the variable $arrDAta is capitalized differently than the others ($arrData)
I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.
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
}
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).