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 =
Related
I am getting an array in through form .I am using explode method to separate my values
$getData =$_POST['jasonHandle'];
print_r($getData); // output incoetp,11,12,13,#,101,11,12,#
here # is used for separation
$arr=explode(",#,",$getData);
print_r($arr); // Array ( [0] => incoetp,11,12,13 [1] => 101,11,12,# )
The above array may contain any number of elements(in this case there are tow arrays) .
I need to delete this # element which is appearing at last of last array
I tried unset() and array_pop () ,but its deleting entire last array .
Also COUNT() function is not working for inner array
echo count($arr[1]); // Parameter must be an array or an object that implements Countable
I have been using count function for 2 d array and never had any issue .why is this happening?
you can use chop() function
chop()--> The chop() function removes whitespaces or other predefined characters from the right end of a string
$arr[1]=chop($arr[1],",#");
Delete it by using rtrim
$arr[1] = rtrim($arr[1], '#');
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.
An array which is populated it's value from database. when I print_r the array . It shows like below...
Array
(
[term] => These are following selection a) new b) old
)
Here the array is ending with character ) inside the term element " ..selection a) new ..", while it's only a character in the array. So How I would avoid the ending of array with charcters present inside the array ?
What you're viewing is the output of an array with print_r.
Defining an array in PHP is a little different:
$variable = array('inside First a) new, b) old');
print_r($variable);
var_dump($variable); // similar to print_r
There is also another way of doing that:
$variable = array();
$variable[0] = 'inside First a) new, b) old';
So you need to quote your value to get a string.
The array value in your declaration should be a string:
$myArray = array(
0 => 'inside First a) new , b) old';
);
In PHP you need to quote string values:
$array = array(
0 => 'inside First a) new , b) old'
);
See the manual. To be honest this is pretty basic stuff. If you don't know this, I'd suggest going away and getting a basic introduction to PHP book and reading through it...
I have array in following format. I want to convert it into another format like below.
Array
(
[b2] => 2
[b3] => 8
[b4] => 2
[b5] => 4
)
Is it possible key is converted into PHP variables and its value automatically assigned to variables?Is it possible?
$b = 2;
$b3 = 8;
$b4 = 2
$b5 = 4
thanks
looks like you want extract()
You're looking for the extract() function. You pass in the array you want to extract into the current scope, and that's it. Mind though that you should be very careful when you use this function; for safety, pass EXTR_SKIP in as the second argument to prevent overwriting existing variables, after all you wouldn't want the likes of $_SESSION or $_SERVER throttled by an array with malicious data...
All,
I have an array with hyphens in the key name. How do I extract the value of it in PHP? It returns a 0 to me, if I access like this:
print $testarray->test-key;
This is how the array looks like
testarray[] = {["test-key"]=2,["hotlink"]=1}
Thanks
You have problems:
testarray[] = {["test-key"]=2,["hotlink"]=1}
1 2
You are missing $ used to create variables in php
It is not a valid array format
.
print $testarray->test-key;
1
The => operator is used for objects, not arrays, use [] instead.
Here is how your code should be like:
$testarray = array("test-key" => 2, "hotlink" => 1);
print $testarray['test-key'];
Finally,
See PHP Array Manual
print $testarray["test-key"];
The PHP manual has a good page explaining arrays and how to do things with them:
http://www.php.net/manual/en/language.types.array.php