This question already has answers here:
Custom key-sort a flat associative based on another array
(16 answers)
Closed 2 years ago.
Quick question, most likely for a veteran will be easy, or maybe im asking for too much.
i have this code for laravel in php, im not fan to do a foreach, is there a better way? i guess should be an existing function that replace my values of arr to the keys match on arr2, but i dont know
Its really important not to change the order.
$arr= ['filters', 'repeat', 'via', 'type'];
$arr2= [
'filters' => 'text1',
'repeat' => 'text2',
'via' => 'text3',
'type' => 'text4',
];
foreach($arr as $k)
$res[]=$arr2[$k];
return $res;
You can do it using array_map.
$arr= ['filters', 'repeat', 'via', 'type'];
$arr2 = [
'filters' => 'text1',
'repeat' => 'text2',
'via' => 'text3',
'type' => 'text4',
];
$res = array_map(function($key) use($arr2) {
return $arr2[$key];
}, $arr);
print_r($res);
You can use Laravel's array_only helper function:
$res = array_only($arr2, $arr);
If you don't want to preserve the keys then you can use the array_values function too:
$res = array_values(array_only($arr2, $arr));
Yes, two ways:
Use array_values(), assuming the values of $arr are exactly in the same order as the keys in $arr2 and always have the same keys. I'm guessing this is not exactly what you meant, but that's what your example shows. If that's the case, then it is as simple as:
$res = array_values($arr2)
Use array_map() to map the values of one array (in this case $arr) to a new array ($res):
$mapper = function($k) use ($arr2) {
return $arr2[$k];
};
$res = array_map($mapper, $arr);
You can then also add handling of cases where $k does not exist in $arr2, e.g. return null and then use array_filter to filter out the null values;
(I did not test this code but generally, this is the approach)
You can use Collection in Laravel
$res = collect($arr)->map(function ($key) use ($arr2) {
return $arr2[$key];
})->toArray()
If you just want the array values you can use the array_values() function.
$arr2= [
'filters' => 'text1',
'repeat' => 'text2',
'via' => 'text3',
'type' => 'text4',
];
$res = array_values($arr2);
This will result in the following array:
['text1', 'text2', 'text3', 'text4']
Related
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 7 months ago.
How can I remove an item based on key for example, $array[testing3] or based on value, for example, Template3 from the below array in php.
$array = array(
'mentor' => 'Template',
'mentor1' => 'Template1',
'testing' => 'Template2',
'testing3' => 'Template3',
'testing4' => 'Template4',
'testing5' => 'Template5',
'testing6' => 'Template6'
);
Let's use array_filter() to achieve the goal.
$array = array(
'mentor' => 'Template',
'mentor1' => 'Template1',
'testing' => 'Template2',
'testing3' => 'Template3',
'testing4' => 'Template4',
'testing5' => 'Template5',
'testing6' => 'Template6'
);
Remove an item in an array, for example, Template3
$filtered_array1 = array_filter($array, function($val) {
return 'Template3' != $val;
});
print_r($filtered_array1);
Remove all elements in an array except Template3 from the array
$filtered_array2 = array_filter($array, function($val) {
return 'Template3' == $val;
});
print_r($filtered_array2);
So far, we used value to filter an array. You can filter an array based on key too. You need to use 3rd argument to the function. There two options for the 3rd argument - ARRAY_FILTER_USE_KEY and ARRAY_FILTER_USE_BOTH. You may use one of them. Let's use ARRAY_FILTER_USE_KEY flag to remove an item based on key, for example, testing3:
$filtered_array3 = array_filter($array, function($key) {
return 'testing3' != $key;
}, ARRAY_FILTER_USE_KEY);
print_r($filtered_array3);
To know more about array_filter() function please refer to this doc
You can use unset() to achieve this:
unset(myArray['testing3']);
You can use unset (https://www.php.net/unset)
$array = array(
'mentor' => 'Template',
'mentor1' => 'Template1',
'testing' => 'Template2',
'testing3' => 'Template3',
'testing4' => 'Template4',
'testing5' => 'Template5',
'testing6' => 'Template6');
unset($array['testing3']);
or if you need to find it by the value you can use the array_search (https://www.php.net/array-search)
// Remove the element if it exists
if($element = array_search("Template3",$array)){
unset($array[$element]);
}
To answer the question brought up in the comments about keeping only the array element you're looking for:
use array_search and overwrite your array (or create a new array from it).
$array = array_search('Template3', $array);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 6 months ago.
I'm not sure how to search for the problem I'm facing, so that's why I'm asking here.
I want to know if there is a native php-function to transform following array I get from a html-form:
Original Array
$array = [
'product_template_id' => [
'0' => '1',
'1' => '2'
],
'amount' => [
'0' => '50',
'1' => '100'
]
]
Desired Array
$array = [
'0' => [
'product_template_id' => '1',
'amount' => '50'
],
'1' => [
'product_template_id' => '2',
'amount' => '100'
]
]
I know this can be done with a loop, but that's not what I'm asking for.
PS(not my main question, just a sidequestion): How can I bulk format code in stackoverflow? Is it always 4 spaces? How can I perform the formatting quicker?
edit: PS is not the main question, it is more of a sidenote which has already been answered by #RiggsFolly
Just as prove of concept you can use array_map:
$keys = array_keys($array);
// For < PHPv5.6
// $zip = call_user_func_array('array_map', array_merge([null], $array));
$zip = array_map(null, ...array_values($array));
$result = array_map(function ($item) use ($keys) {
return array_combine($keys, $item);
}, $zip);
Here is working demo.
But in practice, there is too much overhead for so simple problem.
EDIT:
Here I am zipping (getting one corresponding element from each array and creating an array of this elements) all child arrays. It is done with array_map using its property:
An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function
As array_map can take an arbitrary number of arrays, I supply them to it with '...' splat operator.
This does what you need!
for ($x = 0; $x < count($array['amount']); $x ++) {
$arr[$x] = [
'product_template_id' => $array['product_template_id'][$x],
'amount' => $array['amount'][$x],
];
}
See here for a working example https://3v4l.org/dokHK
i have this two arrays:
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
The keys on each of them are the id for the client, the first one is the amount owed and the second one is the amount payed, i want to achieve the following:
$mergedArrays = [
'1' => [
'owed' => 285.52,
'payed' => 123.44
],
'2' => [
'owed' => 427.76,
'payed' => 48.32
]
];
I was wondering if there's a PHP function to do so, i tried with array_merge_recursive but it just makes an array with the four elements together.
Any help would be really appreciated.
you can loop in the first array and merge the second according to keys
foreach($array1 as $key => $val) {
$mergedArrays[$key] = array('owed' => $val, 'payed' => $array2[$key]);
}
sample
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
$final_arr = array_map(function($a1, $a2) {
return array(
'owed' => $a1,
'paid' => $a2
);
}, $array1, $array2);
$final_arr = array_combine(array_keys($array1), $final_arr);
var_dump($final_arr);
Based on the comment, it seems you're looking for built-in PHP functions to do the task for you rather than going for traditional looping. But the looping method provided by Fabio is the simplest one you could go for without any other complicated approaches. I've tried my best to provide you the solution using the core PHP functions. Hope you're happy with it!
I have two arrays as shown below. I need to merge the content of the arrays so that I can get the structure as shown in the third array at last. I have checked array_merge but can't figure out the way this is possible. Any help appreciated. Thanks.
[
['gross_value' => '100', 'quantity' => '1'],
['gross_value' => '200', 'quantity' => '1']
]
and
[
['item_title_id' => '1', 'order_id' => '4'],
['item_title_id' => '2', 'order_id' => '4']
];
I should get a merged array like this:
[
[
'gross_value' => '100',
'quantity' => '1',
'item_title_id' => '1',
'order_id' => 4
],
[
'gross_value' => '200',
'quantity' => '1',
'item_title_id' => '2',
'order_id' => 4
]
]
Use array_merge_recursive :
Convert all numeric key to strings, (make is associative array)
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
See live demo here
how about:
$arr1 = array(
0 => array(
'gross_value' => '100',
'quantity' => '1'
),
1 => array(
'gross_value' => '200',
'quantity' => '1'
)
);
$arr2 = array(
0 => array(
'item_title_id' => '1',
'order_id' => '4'
),
1 => array(
'item_title_id' => '2',
'order_id' => '4'
)
);
$arr = array();
foreach($arr1 as $k => $v) {
array_push($arr, array_merge($v, $arr2[$k]));
}
print_r($arr);
output:
Array
(
[0] => Array
(
[gross_value] => 100
[quantity] => 1
[item_title_id] => 1
[order_id] => 4
)
[1] => Array
(
[gross_value] => 200
[quantity] => 1
[item_title_id] => 2
[order_id] => 4
)
)
Have a look at array_merge
I would probably iterate over the arrays and merge them manually.
$result = array();
foreach ( $array1 as $key => $item )
{
$result[$key] = array_merge($array1[$key], $array2[$key]);
}
You will have an issue if the top-level arrays don't have strictly matching keys though.
If you have $array1 and $array2, try this:
foreach($array1 as $key1=>$innerArray){
$array1[$key1]['item_title_id'] = $array2[$key1]['item_title_id'];
$array1[$key1]['order_id'] = $array2[$key1]['order_id'];
}
The problem with things like merge recursive is that they don't know when to stop.
In some scenarios you want to stop traversing down an array and simply take a given value if it exists.
For instance if you have to override a nested config array you might not want the default keys to stick around at a a specific level.
here is my solution:
public static function merge_lvl2(){
$args = func_get_args();
return static::merge($args, 2);
}
public static function merge($args, $maxDepth = null, $depth = 1)
{
$merge = [];
foreach($args as $arg) {
if (is_array($arg)) {
if (is_array($merge)) {
if ($maxDepth == $depth) {
$arg += $merge;
$merge = $arg;
} else {
$merge = array_merge($merge, $arg);
}
} else {
$merge = $arg;
}
}
}
if ($maxDepth !== $depth) {
foreach($args as $a) {
if (is_array($a)) {
foreach($a as $k => $v) {
if (isset($merge[$k]) && is_array($merge[$k])) {
$merge[$k] = static::merge([$merge[$k], $v], $maxDepth, $depth + 1);
}
}
}
}
}
return $merge;
}
You can pass as many arrays to merge as you want to.
$merged = ClassName::merge_lvl2([..array1..], [..array2..], [..array3..], etc...);
It will stop merging at level 2 and accept the last instance of the key as an override instead of a merge.
You can also call merge directly with an array of args and setting the max depth.
If no max depth is set it will traverse the entire array.
The most modern, elegant, concise way to merge rows from two or more arrays (or the rows from a multidimensional array with 3 or more levels of depth) is to call array_merge() on each row (array_replace() can also be used). array_map() can call array_merge by its string name and the input data can be split into individual arguments with the "spread operator" (...) when needed.
Code for the OP's arrays: (Demo)
var_export(
array_map('array_merge', $arr1, $arr2)
);
The above technique will return a newly indexed array (though you might not notice because the sample input arrays were indexed to begin with). If your input data has associative first-level keys, they will be ignored and destroyed by this technique. If you have non-numeric first-level keys and want to merge on those, then array_merge_recursive() is likely to be the ideal native function - Demo.
However, it must be said, that for the OP's sample data array_merge_recursive() IS NOT a correct technique.
My first snippet is conveniently extended if you have more than two arrays which need their rows to be merge based on their positions. (Demo)
var_export(
array_map('array_merge', $arr1, $arr2, $arr3)
);
And as mentioned earlier, the spread operator can be used to unpack deeper arrays with the same result. Again, the number of subarrays containing rows can be dynamic. If your deep array only has one subarray containing rows, then the result will be a "flattening" effect where the top level is removed.
Code with new data structure: (Demo)
$masterArray = [
[
['gross_value' => '100', 'quantity' => '5'],
['gross_value' => '200', 'quantity' => '6']
],
[
['item_title_id' => '1', 'order_id' => '3'],
['item_title_id' => '2', 'order_id' => '4']
],
[
['foo' => 'bar1'],
['foo' => 'bar2']
]
];
var_export(
array_map('array_merge', ...$masterArray)
);
To be fair, array_replace_recursive() does provide the desired result using the OP's sample data, but I find the technique to be semantically misleading. (Demo)
All that said, you are not forced to use PHP's native array functions; you can use classic loops as well -- you will have several ways to "unite" the rows inside the loop. This approach is a little less D.R.Y. because you need to explicitly specific the separate arrays that you wish to synchronously iterate. Just make sure that you understand the nuanced differences in using array_merge(), array_replace(), and the union operator (+) with different qualities of data. Hint: associative, non-numeric keyed data in the respective rows will be affected when key collisions occur. Be careful to use a merging technique that will not overwrite the wrong data when rows share associative keys.
array_merge() to build a new array: (array_merge() Demo) (array_replace() Demo) (array union operator + Demo)
$result = [];
foreach ($arr1 as $i => $row) {
$result[] = array_merge($row, $arr2[$i]);
}
var_export($result);
Alternative, you can use the same general approach, but instead of populating a new $result array, you can merge data into the first array.
(array_merge() Demo) (array_replace() Demo) (array union assignment operator += Demo)
foreach ($arr1 as $i => &$row) {
$row = array_merge($row, $arr2[$i]);
}
var_export($arr1);
For the sake of completeness, if you have an indexed array of indexed arrays, then you might even use iterated calls of array_push() with the spread operator to achieve a similar functionality. This quite literally, indicatively appends the row data from subsequent arrays to the first array's rows.
Code: (Demo)
$arr1 = [
['A', 'B', 'C'],
['F', 'G']
];
$arr2 = [
['D', 'E'],
['H', 'I', 'J', 'L']
];
foreach ($arr1 as $i => &$row) {
array_push($row, ...$arr2[$i]);
}
var_export($arr1);
Related non-duplicate content on Stack Overflow:
Partially merge one array's row data with another another array:Add column of values from one array to another
Merge two flat arrays to create an array of merged rows:Transforming array values in elements of a subarray using PHP
Merge rows with indexed elements, remove duplicates and reindex:Merge two multidimensional arrays, preserve numeric keys, and combine values inside array
Merge arrays containing objects:Merge rows of two arrays containing objects by first level index
Push single elements from one array to rows in another array:Push elements from one array into rows of another array (one element per row)
On the above pages, the rabbit hole goes further because I've linked other related pages to them. Keep researching until you find what you need.
If you are using Laravel, you might be interested in its combine() and collect() methods.
I have an array that looks like this:
Array([0]=>Array([id]=>7 [name]=foo) [1]=>Array([id]=>10 [name]=bar) [2]=>Array([id]=>15 [name]=baz))
Each index contains an another array with various elements including an 'id'. I would like to "go up" a level, such that my top-level array is indexed by the ID element of the corresponding nested arrays, but that index still contains an array with all of the elements that were in the sub arrays?
In other words, how can I use PHP to turn the above array into this:
Array([7]=>Array([id]=>7 [name]=foo) [10]=>Array([id]=>10 [name]=bar) [15]=>Array([id]=>15 [name]=baz))
What you need to do here is extract the ids from each sub-array in your input. If you have these as an array of ids, you are just an array_combine call away from re-indexing your original array to use these ids as the keys.
You can produce such an array of ids using array_map, which leads to:
// input data
$array = array(array('id' => '7', 'name' => 'foo'),array('id' => 10, 'name' => 'bar'));
// extract ids from the input array
$ids = array_map(function($arr) { return $arr['id']; }, $array);
// "reindex" original array using ids as array keys, keep original values
$result = array_combine($ids, $array);
print_r($result);
The syntax I 've used for the anonymous function (first argument to array_map) requires PHP >= 5.3, but you can achieve the same (although a bit less conveniently) with create_function in any PHP version you 'd not be ashamed of using.
See it in action.
In modern, supported versions of PHP, this whole task can be achieved with array_column() alone.
Using null as the second parameter will leave the rows unchanged.
Using id as the 3rd parameter will assign those columnar values as the new first level keys. Be aware that if these columnar values are not unique, subsequently encountered duplicates will overwrite previously encountered rows with the same id value -- this is because keys cannot be duplicates on a given level in an array.
DO NOT bother calling array_combine(), it is simply unnecessary/indirect.
Code: (Demo)
$array = [
['id' => 7, 'name' => 'foo'],
['id' => 10, 'name' => 'bar'],
['id' => 15, 'name' => 'baz'],
];
var_export(
array_column($array, null, 'id')
);
Output:
array (
7 =>
array (
'id' => 7,
'name' => 'foo',
),
10 =>
array (
'id' => 10,
'name' => 'bar',
),
15 =>
array (
'id' => 15,
'name' => 'baz',
),
)
Try this:
$newArray = array();
foreach($oldArray as $key => $value) {
$newArray[$value['id']] = $value;
}
Since PHP 5.5.0, you can shorten the code by using array_column() instead of array_map().
$result = array_combine(array_column($array, 'id'), $array);