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
Related
I have print_r result like this :
Array
(
[0] => A, B, C, D
[1] => 15,20,24,19
)
how to make them like this :
Array
(
[A] => 15
[B] => 20
[C] => 24
[D] => 19
)
Great thanks for help :)
Try this:
$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();
foreach ($a as $index => $value) {
if (isset($b[$index])) {
$c[$value] = $b[$index];
}
}
var_dump($c);
You need explode() and array_combine(). Assuming your initial array, $arr:
$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));
See demo
Try to explode() your array index by comma and combine both array with keys and values using array_combine()
$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 )
array_combine is the way
<?php
$myArray = array(
array('A', 'B', 'C', 'D'),
array(15, 20, 24, 19)
);
$combinedArray = array_combine($myArray[0], $myArray[1]);
$a = array('x' => 1, 'y' => 2, 'z' => 3, 'a' => 4, 'b' => 4);
$b = array('x', 'z', 'a');
I am looking for a NATIVE PHP function that will take the 2 above arrays and return
array('x' => 1, 'z' => 3, 'a' => 4);
I do not want to use a loop.
Any help would be appreciated.
This can be solved using array_intersect_key (http://uk3.php.net/manual/en/function.array-intersect-key.php) and array_flip (http://uk3.php.net/manual/en/function.array-flip.php);
array_intersect_key($a, array_flip($b));
See this example; https://eval.in/158360
array_intersect_key($a, array_fill_keys($b, 1));
There is native function array_intersect_key which computes the intersection of several arrays using their keys as base. But your second array is a list of values which we need to be keys. So we can use function array_fill_keys which take first argument as list of keys and create an array filled with value, specified by second parameter ( we will take integer 1, for instance ):
$b = array('x', 'z', 'a');
$bValsToKeys = array_fill_keys($b, 1);
$bValsToKeys will be
Array ( [x] => 1 [z] => 1 [a] => 1 )
And then we can intersect it with $a:
$result = array_intersect_key($a, $bValsToKeys);
print_r($result);
Out:
Array ( [x] => 1 [z] => 3 [a] => 4 )
I have two arrays and want to find the first match for either of arrayTwos values in arrayOne.
arrayOne ( [0] = C [1] = A [2] = B [3] = D [4] = B [5] = C)
and
arrayTwo ( [0] = A [1] = B [2] = C )
With these values I would want to return the value "C" as it is the first value in arrayTwo to appear in arrayOne.
I'm thinking I could use for loops and if statements to run through but re there any functions in PHP I could use to simplify this?
Use array_search
$keys = array_search($second_array, $first_array);
Ref : http://in3.php.net/array_search
array_search
$valuekeys = array_search($secondarray, $arrayone);
use array_intersect
$arrayOne = array('C', 'A', 'B', 'D', 'B', 'C');
$arrayTwo = array('A', 'C');
$result = array_intersect($arrayOne , $arrayTwo);
echo $result[0];
Use array_intersect. This will do the job. http://www.php.net/manual/en/function.array-intersect.php Note the difference between using array_intersect($array1, $array2) and array_intersect($array2, $array1)
You can use array_intersect():
$arr1 = array( 0 => 'C', 1 => 'A', 2 => 'B', 3 => 'D', 4 => 'B', 5 => 'C');
$arr2 = array( 0 => 'A', 1 => 'B', '2' => 'C' );
$arr3 = array_intersect($arr1,$arr2);
var_dump($arr3[0]);
string(1) "C"
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
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
+ operator for array in PHP?
If $a and $b are both arrays, what is the result of $a + $b?
http://www.php.net/manual/en/language.operators.array.php
Union of $a and $b.
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.
<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);
$c = $a + $b;
print_r($c);
results in this for me:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
BUT:
<?php
$a = array('a' => 1, 'b' => 2, 'c' => 3);
$b = array('d' => 4, 'e' => 5, 'f' => 6);
$c = $a + $b;
print_r($c);
results in:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
)
So it would appear that the answer here depends on how your arrays are keyed.
My test
$ar1 = array('1', '2');
$ar2 = array('3', '4');
$test = $ar1 + $ar2;
print_r($test);
Array
(
[0] => 1
[1] => 2
)
Now try this experiment
$a = array( 0 => 1,
1 => 2,
4 => 3
);
$b = array( 2 => 4,
4 => 5,
6 => 6
);
$c = $a + $b;
var_dump($c);
If you do something like $result = $a + $b; then $result will be assigned to the first argument, in this case $a.