This is my code, it's for have an array :
$zi = '1';
for ($zi = 1; $zi <= $v['Store']['stock']; $zi++) {
$options_array[$zi]= $zi;
}
var_dump($options_array);
Array
(
[0] => 0
[1] => 1
[2] => 2
)
why i have the zero in my result ?
i put $zi at 1, so why ?
As my comment turned out to be correct: The issue is that you already have a property 0 in the $options_array before you even get to the presented code block. You can start with a fresh array using $options_array = [] or $options_array = array() (for older php versions). You can also just remove property 0 with unset($options_array[0]).
In php there are two kinds of arrays, numerically indexed arrays, and associative arrays. Your output seems a little suspect but I can tell you that if all the keys of a PHP array are numeric then the array will be zero based.
I see how you have $zi = '1' above, which is along the lines of what you would have to do to create a one based array, but it would be associative, and you won't be able to simply use the ++ operator. I believe even if you use a string that is a number PHP will still convert to a numerically indexed array. I recommend against trying to implement a one based array, it's insanity.
Hope this page helps http://us2.php.net/manual/en/language.types.array.php
You start setting $options_array at index 1, so index 0 is whatever the default value of the underlying type is. In this case, it's an integer, so the default is 0.
Related
Im currently having issue's with an algorithm im creating in php using 2 separate 2 dimensional arrays.
I want to Find how different 1 array is from the other and return the value.
For now my issue is that with the algorithm im using 'should' return the same value for the 2 arrays im comparing...but currently it does not.
The values in the arrays are as follows:
$array1 contains ['index','index','index','index','index']
$array2 contains ['index','java','index','none']
When i run my algorithm :
function arrayDifference($array1,$array2)
{
if (is_array($array1)&&is_array($array2)){
$result = array_diff($array1, $array2);
$value=max(count($array1),count($array2));
$result=$value-count($result);
return $result;
}
}
I get these results:
When $array1 is passed in first the result is : 2.
When $array2 is passed in first the result is : 4.
The issue is that since im using the same arrays, should the resulting difference not be the same when both are passed in irrelevant of the parameter order?
Any help would be appreciated, thanks.
Update/Note ---------------------------------------
After printing out array_diff the first values returned from $array1 being passed in first is :
'( [1] => java [3] => none )'
and for array2 passed in first:
'Array ( ) 1'
See how array_diff() works.
In 1st case of $array1(first array in array_diff), there is only "index" value which is present in $array2 and therefore count(array_diff) is 0.
In 2nd case of $array2(first array in array_diff), there are 2 values (index,java) which are not present in $array1 therefore count(array_diff) is 2.
Thats why in first case, you get 5-0 = 5
Thats why in second case, you get 5-2 = 3
in php 4.4.4 having:
$hbc=pack("LL",82603088,82602992);
list($v1,$v2)=unpack("L2",$hbc);
echo("v1=".$v1." v2=".$v2);
result is:
v1= v2=82603088
I already have some hours wrapping my head aroud this...
thanks
The error is not withing the pack or unpack, but inside the list().
Turn on notices! Your code will complain like this:
Notice: Undefined offset: 0 in ...
That should make you wonder. If you dump the array that is returned by unpack, it is correct:
array(2) {
[1] =>
int(82603088)
[2] =>
int(82602992)
}
And then there is this tiny little note on the docs page of list():
list() only works on numerical arrays and assumes the numerical indices start at 0.
Your array does not start at index 0, and the notice likes to tell you that.
The quick fix is:
list(,$v1,$v2)=unpack("L2",$hbc); // add a comma to skip index 0
A better approach might be to use unpack to create named array indices:
$unpacked = unpack("L2v/",$hbc);
Result:
array(2) {
'v1' =>
int(82603088)
'v2' =>
int(82602992)
}
From the php.net comments.
$int_list = array_merge(unpack("s*", $some_binary_data));
This will essentially reset your index keys starting at 0.
Why am I getting different outputs through print_r in the following two cases!!? This is a bug in php? Is php unable to execute complex hierarchical functions called inside functions?
CASE 1 :
$aa='2,3,4,5,5,5,';
$aa=array_unique(explode(',',$aa));
array_pop($aa);
print_r($aa);
CASE 2 :
$aa='2,3,4,5,5,5,';
array_pop(array_unique(explode(',',$aa)));
print_r($aa)
In the first case, the output is an exploded array :
Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )
In the second case, the output is string :
2,3,4,5,5,5,
This is because array_pop alters its input, and you're passing it a temporary variable (not $aa).
Note the signature in the documentation: array_pop ( array &$array ) - the & means it takes a parameter by reference, and it alters that input variable.
Compare with the other two functions:
array explode ( string $delimiter , string $string , int $limit )
and
array array_unique ( array $array , int $sort_flags = SORT_STRING )
In the first case you update $aa with the output of array_unique(), and then pass that to array_pop to be altered.
In the second case the output of array_unique() will be the same, but this temporary value isn't assigned to a variable & therefore it's forgotten after array_pop is called.
It's worth noting that in that in PHP (unlike say, C++), passing by reference is actually slower than passing by value and therefore is only ever used to modify the input parameter of a function.
In the first case you alter variable as in line 2 bs assigning a new value with the assignment operator =
I've been given a datafile where the original creator used alphabetical rather than numeric values to show order.
For example, if there's ten items, they'd be named:
12342313A
12342313B
12342313C
12342313D
12342313E
...
I need to import these values into a mySQL table that has order as a required int column, and I need to convert the letter to a number.
Is there a function in PHP to get a numeric value for a letter? Or will I need to do a substr to grab the trailing letter, and create an indexed array of letters and just do a lookup against that array?
I'm hesitant to do the simple way above, since I don't know how many objects could potentially exist, and I could need to write an array from A-AAAA or something.
Try converting it from base 36 to base 10 using base_convert(), I.e. base_convert($str, 36, 10). You might need to strtolower it first, and it'll only work if its not case sensitive.
PHP has a simple way to create that array, so you could write a function to figure all that out for you and do something like:
function str_to_num($letters, $max = 'ZZZZZZ') {
$count = 0;
for ($i = 'A'; $i < $max; $i++) {
$count++;
if ($letters == $i)
return $count;
}
}
Then you could do the substr, find the letters at the end, and then pass it into the function:
str_to_num('A'); // returns 1
str_to_num('AB'); // returns 28
str_to_num('AC'); // returns 29
str_to_num('ABC'); // returns 731
Something like that, anyway.
Good luck.
Assuming this is a one-time problem that you've got to correct and won't encounter moving forward, I suggest you use sort to... erm, sort out the problem. Let's say you have all those alpha-numeric order fields in an array, like so:
$vals = array (
'12342313A',
'12342313D',
'12342313E',
'12342313B',
'12342313C'
);
Those are all mixed up, not in order. But, you can call the function sort (docs) on that array and PHP does a decent job of making sense out of it:
print '<pre>Unsorted: ';
print_r($vals);
print '</pre>';
sort($vals);
print '<pre>Sorted: ';
print_r($vals);
print '</pre>';
/*
Unsorted: Array
(
[0] => 12342313A
[1] => 12342313D
[2] => 12342313E
[3] => 12342313B
[4] => 12342313C
)
Sorted: Array
(
[0] => 12342313A
[1] => 12342313B
[2] => 12342313C
[3] => 12342313D
[4] => 12342313E
)
*/
So far, so good. Now, you've got them ordered, and as a bonus you can use the index of the array as your new field in the database. Alter the table and add a field to hold the new value; we'll call this field numeric_order, and in my sample I've called the field that currently holds the alpha-numeric sort data string_order. Loop your sorted array and update the database (for example):
foreach ($vals as $x=>$v) {
$sql = 'UPDATE myTable SET numeric_order = '.($x+1).' WHERE string_order = "'.$v.'"';
}
I add 1 to x in the loop based on the assumption that you don't want anything to have 0 for the order - if that isn't a concern, then you can just use x. This is also predicated on the assumption that no two rows have the same alpha-numeric sort value.
If they do, then all is not lost! Start with your array looking like this:
$vals = array (
3=>'12342313A',
15=>'12342313D',
66=>'12342313E',
101=>'12342313B',
200=>'12342313C'
);
... the numeric keys would represent the unique/primary key of the corresponding row. Instead of sort, which does not preserve keys, use asort (which does preserve keys - docs), and then your loop looks like this:
$ord = 1
foreach ($vals as $x=>$v) {
$sql = 'UPDATE myTable SET numeric_order = '.$ord.' WHERE id = "'.$x.'"';
$ord++;
}
If my base assumption is wrong, and you'll continue to deal with this method of ordering rows, then in my humble view you ought to re-consider your data design.
use ord() with substr and subtract 64. This will set A to 1, B to 2, etc...
From what you have above, it seems like your values (last digit, at least) can be thought as being hex numbers. You can then transform them into decimal numbers through the hexdec function.
http://php.net/manual/en/function.hexdec.php
PHP.
$a['0']=1;
$a[0]=2;
Which is proper form?
In the first example you use a string to index the array which will be a hashtable "under the hood" which is slower. To access the value a "number" is computed from the string to locate the value you stored. This calculation takes time.
The second example is an array based on numbers which is faster. Arrays that use numbers will index the array according to that number. 0 is index 0; 1 is index 1. That is a very efficient way of accessing an array. No complex calculations are needed. The index is just an offset from the start of the array to access the value.
If you only use numbers, then you should use numbers, not strings. It's not a question of form, it's a question of how PHP will optimize your code. Numbers are faster.
However the speed differences are negligible when dealing with small sizes (arrays storing less than <10,000 elements; Thanks Paolo ;)
In the first you would have an array item:
Key: 0
Index: 0
In the second example, you have only an index set.
Index: 0
$arr = array();
$arr['Hello'] = 'World';
$arr['YoYo'] = 'Whazzap';
$arr[2] = 'No key'; // Index 2
The "funny" thing is, you will get exactly the same result.
PHP (for whatever reason) tests whether a string used as array index contains only digits. If it does the string is converted to int or double.
<?php
$x=array(); $x['0'] = 'foo';
var_dump($x);
$x=array(); $x[0] = 'foo';
var_dump($x);
For both arrays you get [0] => foo, not ["0"] => foo.
Or another test:<?php
$x = array();
$x[0] = 'a';
$x['1'] = 'b';
$x['01'] = 'c';
$x['foo'] = 'd';
foreach( $x as $k=>$v ) {
echo $k, ' ', gettype($k), "\n";
}0 integer
1 integer
01 string
foo string
If you still don't believe it take a look at #define HANDLE_NUMERIC(key, length, func) in zend_hash.h and when and where it is used.
You think that's weird? Pick a number and get in line...
If you plan to increment your keys use the second option. The first one is an associative array which contains the string "0" as the key.
They are both "proper" but have the different side effects as noted by others.
One other thing I'd point out, if you are just pushing items on to an array, you might prefer this syntax:
$a = array();
$a[] = 1;
$a[] = 2;
// now $a[0] is 1 and $a[1] is 2.
they are both good, they will both work.
the difference is that on the first, you set the value 1 to a key called '0'
on the second example, you set the value 2 on the first element in the array.
do not mix them up accidentally ;)