Inserting multiple values from an array using array_unshift() method - php

array_unshift is using for inserting one or many values at the beginning of an array. Now I have an array-
$data = [1, 3, 4];
Another array is needed to insert at the beginning of the $data array.
$values = [5, 6];
Here I want to insert the values 5, 6 at the beginning of the $data array, and the resulting $data would be-
$data = [5, 6, 1, 3, 4];
Note: I know there is a way like array_unshift($data, ...$values); but this is working from php7.3.x AFAIK. I need to do it below php7.3.
Is there any way to do this without looping the $values array in the reverse order?

Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox

You should use array_merge instead of array_unshift.
$data = [1, 3, 4];
$values = [5, 6];
$result = array_merge($values, $data); // the sequence of the array inside array_merge will decide which array should be merged at the beginning.

Related

How to remove NaN from array in PHP?

How do I remove NaN values from an array in php?
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$desired_result = [1, 3, 5, 3, 4];
To remove any other element, I found this solution, but I can't get it to work for NaN.
https://stackoverflow.com/a/7225113/9606753
Context: I am reading data from an rrd Database. I want to calculate the mean across several data entries, however at least one of them is float(NaN).
Use array_filter to remove NaN with is_nan function.
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$filtered_array = array_filter($array, function ($element) {
return !is_nan($element);
});
Note: This will also remove numbers that are stored as strings, '21' for example would be removed.

How can I remove the current element in a foreach loop from the array, without changing the original array?

How can I get a new array with all elements except the current element passed into the foreach loop. See example below:
$numbers = [1, 2, 3, 4, 5];
foreach($numbers as $numer){
// Get a new array with 4 elements excluding the $numer
// For example for first loop I want a array [2, 3, 4, 5]
}
I tried doing:
foreach($numbers as $i=>$numer) {
unset($numbers[$i]);
echo '<pre>';
var_dump($numbers);
echo '</pre>';
}
It works but it alters original array. Is there any PHP function to get new array without affecting original one?
$numbers = array(1,2,3,4,5);
if (($key = array_search($numer, $numbers)) !== false) {
unset($numbers[$key]);
}
If you want to delete all instances of $numer, simply replace if with while.
Edit: I see you've changed your question to include not changing the original array. You can copy the array and manipulate the copied array in that case.
This should work for you:
Just use array_diff_key() to get the entire array, expect the current element, e.g.
<?php
$numbers = [1, 2, 3, 4, 5];
foreach($numbers as $key => $numer){
print_r(array_diff_key($numbers, array_flip([$key])));
}
?>
EDIT:
If you don't have the key, you can simply use array_keys() to get all keys, e.g.
$numbers = [1, 2, 3, 4, 5];
$value = 32;
print_r(array_diff_key($numbers, array_flip(array_keys($numbers, $value))));
This will work
<?php
$numbers = array(1,2,3);
foreach($numbers as $index => $value){
$temp = $numbers;
unset($temp[$index]);
$new_array = $temp;
echo '<pre>'.print_r($new_array, true).'</pre>';
}
?>

PHP merge 2 arrays with different number of elements, $keys from one, $values from another

I want to merge 2 arrays together that have a different number of elements, using the keys from one and the values from another where/if the keys match. The array that contains the desired values may have less elements in it although I would like to retain the resulting empty keys from the original array. For example:
//Array that contains keys I would like to retain
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use
$arr2 = array(01=>123, 03=>123, 05=>123, 07=>123, 09=>123, 11=>123);
//The desired result with some valueless elements
$results = array(01=>123, 02, 03=>123, 04, 05=>123, 06, 07=>123, 08, 09=>123, 10, 11=>123, 12);
As you can see the results array retains 12 elements but only applies values to where the keys from the 2 arrays match.
I have tried $results = array_intersect_key($arr1 + $arr2, $arr2); among other PHP functions as well as:
for ($i=1; $i < count($arr1); $i++) {
if (isset($arr2[$i])) {
$arr3[] = $arr2[$i];
} else {
$arr3[] = $arr1[$i];
}
}
print_r($arr3);
No luck as yet.
Thanks in advance!
For arrays like this
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
this should work.
foreach ($arr1 as $key) {
$results[$key] = isset($arr2[$key]) ? $arr2[$key] : null;
}
or using some other PHP functions:
$results = array_replace(array_fill_keys($arr1, null), $arr2);
//Array that contains keys I would like to retain
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
$result = array_fill_keys(
$arr1,
null
);
array_walk(
$result,
function(&$value, $key) use($arr2) {
$value = (isset($arr2[$key])) ? $arr2[$key] : null;
}
);
var_dump($result);
First set your $arr1's values to false to retain just the keys:
$arr1 = array_fill_keys(array_keys($arr1), false); Or if you're generating $arr1 yourself, define it as such to start with: $arr1 = Array(false, false, false, false /* etc. */);
Now use the array union operator:
$result = $arr2 + $arr1;
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays — from the docs: http://php.net/manual/en/language.operators.array.php)
Then if required sort the array by key: ksort($array);

PHP Array within an Array

$row[]; // Declare array. PRETEND ITS AN ARRAY
$row2[]; // Declare another
$row3[]; // Declare one more
$rowarray[];
$rowarray[0] = $row
$rowarray[1] = $row2
$rowarray[2] = $row3 // Store array in an array
My Questions:
1. Is this valid or even useful?
2. If I do this, how do I access $row[0] $row[1] etc.
The concept is valid, but the syntax is not -- arrays are not explicitly declared like that in PHP. A correct way to initialize this would be something like:
$row1 = array(1, 2, 3);
$row2 = array(4, 5, 6);
$row3 = array(7, 8, 9);
$rowarray = array($row1, $row2, $row3);
Or, equivalently and more succinctly:
$rowarray = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
$rowarray[1][2]. Indexes are in order, so, given the example data I used, this would be 6 (element 2 of the array which is element 1 of $rowarray).
What you're looking for is a Multi-dimensional array.
For example:
$a1 = array(
array(1, 2, 3, 4),
array(1, 2, 3, 4)
);

Finding the index of an object in an array when there are multiple possibilities

I have an array, and I want to find all of the indexes of a certain object in the array. When I use array_search, it only returns the first index in which the object can be found.
echo array_search(3, array(3, 3, 4));
This returns 0, but I want to know that both indexes 0 and 1 have the integer 3 as their object. Is there a way of doing this without using a for loop?
Try array_keys() method :
$array = array(3, 3, 4);
print_r(array_keys($array, "3"));
For reference:
array_keys() — Return all the keys or a subset of the keys of an array Info & usuage examples : http://php.net/manual/en/function.array-keys.php
As an alternative to array_keys, array_filter() retains associativity
$key = 3;
$array = array(1, 3, 3, 4, 3, 5);
$result = array_filter(
$array,
function ($item) use ($key) {
return ($item == $key);
}
);
var_dump($result);

Categories