I'm looking for something like this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1);
$j = array_push($a, $var2);
echo $i;
echo $j;
The expected output would be:
0
1
I want to know the index of the object I just inserted, to be able to find it quickly afterwards. I think array_push gives me the size of the resulting array, not the index for the recently inserted element
array_push return new number of elements in the array, so decrement the return value by 1
Try this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1) - 1;
$j = array_push($a, $var2) - 1;
echo $i;
echo $j;
function my_push_array(&$array, $value){
$array[] = $value;
end($array);
return key($array);
}
$a = ['h','e','l','l'];
echo my_push_array($a, 'o'); //returns 4
Related
I dont know if this is possible but can you create a variable loop?
I want to create many variables in an if condition
if($session[hello]){
$array1 = array("");
$array2 = array("");
$array3 = array("");
$array4 = array("");
$array5 = array("");
}
but since I have more I want to write it like this:
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array + $a = array("");
}
But its not working and I cant figure out how this might be possible.
is there an alternative?
thanks
You can try this -
if($session[hello]){
for($a = 1; $a <= 5; $a++){
${'array' . $a} = array("");
}
}
Don't why you want it this way but using an array would be better.
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array[$a] = array("");
}
}
And access it like $array[1] or $array[2] etc.
How do you count the occurrence of more than one value in an array?
I found below on stackoverflow,
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
I have used this, but I can't get it to work and there must be a more elegant way,
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Mary","Ben");
$counts = array_count_values($array);
$1 = $counts['Ben'];
$2 = $counts['Phil'];
$3 = $counts['Mary'];
echo $1+$2+$3;
// change your code to this
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Mary","Ben");
$counts = array_count_values($array);
$one = $counts['Ben']; // 3
$two = $counts['Phil']; // 1
$three = $counts['Mary']; // 2
echo $one + $two + $three; // 6
// because of this :)
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number <-- your case
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
$bar = &$foo; // This is a valid assignment.
$bar = &(24 * 7); // Invalid; references an unnamed expression.
More on PHP variables
if you want count occurance without using array_count_values() functions then use this code
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$len = sizeof($array);
$i = 0;
$occurance = 0;
while( $i < $len )
{
if($array[$i] == 'Ben')
{
$occurance++;
}
$i++;
}
echo 'number of occurance of Ben ='.$occurance;
I'm creating a function which passing parameter by reference. I just can't get return value with this code and I need your help.
function wow ($id, &$a, &$b)
{
$detail[0][0] = 1;
$detail[0][1] = 2;
$detail[0][2] = 3;
$detail[0][3] = 4;
$detail[1][0] = -1;
$detail[1][1] = -2;
$detail[1][2] = -3;
$detail[1][3] = -4;
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
else
{
$b = $detail[$i][0];
$b = $detail[$i][1];
$b = $detail[$i][2];
$b = $detail[$i][3];
}
}
This is the way I call the function.
$a = $b = null;
wow(1, $a, $b);
echo $a[0]." ".$a[1]." ".$a[2]." ".$a[3]." ".$a[4]." ".$b[0]." ".$b[1]." ".$b[2]." ".$b[3]." ".$b[4]." ";
I think you are missing how arrays work:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
Each of these statemenets is overwriting the previous one.
Try something like this:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i];
}
That was you are copying the entire array inside the first one into $a.
Additionally, you set both $a and $b to null, yet only set one or the other in your function - you will always return at least a warning telling you about a null variable you are trying to echo.
I'm a bit confused about variable variables.
What I like to do is print the value of $var1, $var2 and $var3:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo $var.$i;
$i++;
}
I know $var.$i; is not correct, but I think it shows what I would like to achieve; the while-loop should change it to $var1, $var2 and $var3;
I've tried the following:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
$current_var = 'var'.$i;
$current_var = $$current_var;
echo $current_var;
$i++;
}
But that doesn't work. I think because $var1, $var2 and $var3 are recreated in the while-loop instead of using the actual value. Not sure if that's correct, but that the only thing I can think of.
Try this instead:
echo ${"var".$i};
Curly braces can resolve to variable names without having to use the dollar-dollar approach.
See: Variable Variables in PHP
Try this one.
<?php
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo ${'var'.$i};
$i++;
}
?>
Are you trying to do something like this. Then use array
$my_data = array();
$my_data[1] = 'a';
$my_data[2] = 'b';
$my_data[3] = 'c';
// Method 1
$i = 1;
while ($i <= 3) {
echo $my_data[$i];
$i++;
}
// Method 2
foreach ($my_data as $data) {
echo $data;
}
// Output
abc
abc
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed last year.
I have an associative arrays and an array of keys.
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
How I build an associative array from all element of $A where the key is in $B?
For the example above, the answer should be
$C = array('a'=>'book', 'b'=>'pencil');
$keys = array_flip($B);
$C = array_intersect_key($A,$keys);
array_intersect_key($A,array_combine($B,$B))
or better: array_intersect_key($my_array, array_flip($allowed))
from the question: PHP: How to use array_filter() to filter array keys?
Here's a simple solution which checks that the key exists in $A before appending it to $C
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
var_dump($C);
// Prints:
array(2) {
["a"]=>
string(4) "book"
["b"]=>
string(6) "pencil"
}
$keys = array_keys($B);
$C = array();
foreach ($A as $key => $value)
{
if (in_array($key, $keys))
{
$C[$key] = $value;
}
}
To my immense surprise, the foreach loop method is faster.
The following quick benchmark script gives me results:
array_intersect_key: 0.76424908638
foreach loop: 0.6393928527832
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$start = microtime(true);
for ($i = 0 ; $i < 1000000; $i++) {
$c = array_intersect_key($A,array_flip($B));
}
$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
}
$t2 = microtime(true);
echo "array_intersect_key: " . ($t1 - $start), "\n";
echo "foreach loop: " . ($t2 - $t1), "\n";