I have an array and I want to delete previous all elements from the current specified index
For example:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
I have an index like 3, so I want to delete previous all like
0 => "a", 1 => "b", 2 => "c"
and only have
3=>"d", 4=>"e"
in my new array.
Can anyone help me?
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$output = array_slice($array, 3);
output:
array(2) {
[0]=> string(1) "d"
[1]=> string(1) "e"
}
Another solution with saving index
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$output = array_slice($array, 3, null, true);
output:
array(2) {
[3]=> string(1) "d"
[4]=> string(1) "e"
}
https://www.php.net/manual/en/function.array-slice.php
You may to use array_slice()
In example :
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$startingPosition = 3;
// Preserve keys
// |
// Your array Delete from Delete to |
// | | (if null, |
// | | to the end) |
// | | | |
// v v v v
$array = array_slice($array, $startingPosition , null, true);
var_dump($array);
Output :
array(2) {
[3]=>
string(1) "d"
[4]=>
string(1) "e"
}
You can also use unset() to remove the elements. As shown below.
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$index = 3;
for($i = 0; $i<$index; $i++)
{ unset($array[$i]); }
echo "<pre>";print_r($array);
?>
You can use veriation of array-slice and so on (as array_slice($array, 3) ) but also simple for loop:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$copy = false;
foreach($array as $k => $v) {
$copy |= ($k == 3);
if ($copy)
$res[$k] = $v;
}
Try this
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$new_array = array_slice($array, 3); // 3 is your key to slice
print_r($new_array);
?>
You can use array_slice:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$newArray = array_slice($array, 3, NULL, TRUE);
echo '<pre>';
print_r($newArray);
echo '</pre>';
Output:
Array
(
[3] => d
[4] => e
)
Note that 4 th parameter: TRUE -> preserve_keys is very important.
If it is set to true, preserves the keys in the output array.
Your new array will now have all elements only after index 3
All elements before 3 are not returned here.
Related
I have 2 arrays:
$array1 = ["b" => "no", "c" => "no", "d" => ["y" => "no"]];
$array2 = ["a" => "yes", "b" => "yes", "c" => "yes", "d" => ["x" => "yes", "y" => "yes", "z" => "yes"]];
I want to return the members of $array2 whose keys are also present in $array1.
Desired output:
$array3 = ["b" => "yes", "c" => "yes", "d" => ["y" => "yes"]];
Basically I need the same functionality as array_intersect_key but I want to keep the values from $array2 instead of $array1.
Does such a function exist? Also needs to recurse into child arrays. Thanks!
*Edit*
As Andri suggested in his comment, I attempted to flip the arguments around array_intersect_key($array2, $array1) but in that case what I actually get is:
$array3 = ["b" => "yes", "c" => "yes", "d" => ["x" => "yes", "y" => "yes", "z" => "yes"]];
ie all the children of "d" just because "d" is mentioned in $array1.
There isn't as far as I'm aware a built in function, a method using recursion to process all layers should be easy enough...
$array1 = ["b" => "no", "c" => "no", "d" => ["y" => "no"]];
$array2 = ["a" => "yes", "b" => "yes", "c" => "yes", "d" => ["x" => "yes", "y" => "yes", "z" => "yes"]];
function intersect_key_2 ( array $a1, array $a2 ) {
foreach ( $a1 as $key1 => $value1) {
// If there is a matching element
if ( isset($a2[$key1]) ) {
// Update - if an array, use recursive call else just replace
$a1[$key1] = ( is_array($value1) ) ? intersect_key_2 ( $value1, $a2[$key1])
: $a2[$key1];
}
}
return $a1;
}
print_r(intersect_key_2($array1, $array2));
gives
Array
(
[b] => yes
[c] => yes
[d] => Array
(
[y] => yes
)
)
Ended up writing my own:
function array_intersect_key_values_2($array1, $array2) : array {
$intersection = [];
foreach ($array1 as $key => $value) {
if(isset($array2[$key]))
{
if(is_array($array1[$key]) && is_array($array2[$key]))
{
$intersection[$key] = array_intersect_key_values_2($array1[$key], $array2[$key]);
}
else
{
$intersection[$key] = $array2[$key];
}
}
}
return($intersection);
}
I'm working on a Quiz engine and am comparing answers.
I have two arrays
correct answers :
0 => "a"
1 => "a"
2 => "a"
3 => "c"
Chosen Answers...
0 => "c"
1 => "b"
2 => "a"
3 => "b"
So based on this, I know (from comparing myself) that I have 1 correct answer.
Is there a PHP function that can compare the keys and the values and increment a number of similar?
I've looked at array_intersect and array_difference but they don't seem to give me the desired answer.
Thanks
Short solution using array_intersect_uassoc function (on extended input arrays):
$correct = ["a", "a", "a", "c", "a", "c"];
$chosen = ["c", "b", "a", "b", "a", "b"];
$result = array_intersect_uassoc($correct, $chosen, 'strnatcmp');
print_r($result);
The output:
Array
(
[2] => a
[4] => a
)
I would write a function that would generate an array for each key and if the answer was correct (1, or 0 if wrong) that way you can not only quickly calculate the score but use the array to display the questionnaire result later. Like showing which questions were right and which ones were wrong.
<?php
$corrects = array(
0 => "a",
1 => "a",
2 => "a",
3 => "c"
);
$answers = array(
0 => "a",
1 => "a",
2 => "a",
3 => "c"
);
function verify($answers, $corrects) {
$results = array();
foreach($corrects as $question => $correct) {
$results[$question] = $correct == $answers[$question] ? 1 : 0;
}
return $results;
}
$results = verify($answers, $corrects);
$score = array_sum($results);
?>
It leaves room for more complex scoring or multi correct answer question etc.
A have a php array
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
and a binary number
$filter=101101
I want to filter the array and keep only the keys where the respective value on binary is 1
For this example I would have:
$arr = array(
1 => "a",
4 => "c",
8 => "d",
32 => "f"
);
Or for
$filter=110001
to get
$arr = array(
1 => "a",
2 => "b",
32 => "f"
);
Assuming that the length of $filter is always the same as the number of array elements:
$filter_arr = str_split($filter);
$new_arr = array();
$i = 0;
foreach ($arr as $key => $val) {
if ($filter_arr[$i] == 1) {
$new_arr[$key] = $val;
}
$i++;
}
Using your given array, and filter equal to 101101, $new_arr will equal:
Array
(
[1] => a
[4] => c
[8] => d
[32] => f
)
See demo
This should work for you:
<?php
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
$filter=110001;
$filerValue = str_split($filter);
$count = 0;
foreach($arr as $k => $v) {
if($filerValue[$count] == 0)
unset($arr[$k]);
$count++;
}
var_dump($arr);
?>
Output:
array(3) {
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[32]=>
string(1) "f"
}
I have 2 array
a=array(a=>1,b=>2,c=>2,d=>2,e=>2,f=>2)
and
b=array(a,b,d)
I want to make function compare_plus(array a, array b) like if array a have key== array b val then in increase value of array a at this key by 1.
Example with above array a and b:
c=compare_plus(a,b) =>> c=(a=>2,b=>3,c=>2,d=>3,f=>2)
$a = array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 2, 'e' => 2, 'f' => 2);
$b = array('a', 'b', 'd');
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
$arr[$key]++;
return $arr;
}
Codepad Demo
If you want to add only to existing keys and not create additional ones, you'll need something like this:
$a = array("a" => 1, "b" => 2, "c" => 2, "d" => 2, "e" => 2, "f" => 2);
$b = array("a", "b", "d", "g", "apple");
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
if (array_key_exists($key, $arr))
$arr[$key]++;
return $arr;
}
/* // Output:
Array
(
[a] => 2
[b] => 3
[c] => 2
[d] => 3
[e] => 2
[f] => 2
)
*/
To add the additional keys from $b to $c, simply remove if (array_key_exists($key, $arr)).
http://codepad.org/aquc5DKA
HI there,
Is there any PHP native function which returns the range of records from the array based on the start and end of the index?
i.e.:
array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
and now I would like to only return records between index 1 and 3 (b, c, d).
Any idea?
Couldn't you do that with e.g. array_slice?
$a = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
array_slice($a, 1, 3);
there is a task for array_slice
array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )
example:
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
By using array_intersect_key
$myArray = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
$arrayRange = array('1', '2', '3');
// this can also be used if you have integer only array values
// $arrayRange = range(1,3);
$newArray = array_intersect_key($myArray, array_flip($arrayRange));
print_r($newArray); // output: Array ( [1] => b [2] => c [3] => d )
$array1 = array(1,2,3,4,5,6,23,24,26,21,12);
foreach(range ($array1[0],$array1[5]) as $age){
echo "Age: {$age}<br />";
}
you should get the following output:
Age: 1
Age: 2
Age: 3
Age: 4
Age: 5
Age: 6