PHP array associative [duplicate] - php

This question already has answers here:
PHP combine two associative arrays into one array
(8 answers)
Closed 5 years ago.
Whats the easiest way to convert array a to b
a= [['x'=>'a'], ['y'=>'b']]
b= ['x'=>'a', 'y'=>'b']
a and b are just two examples.

Using array_walk_recursive for arbitrary depth:
$b = [];
array_walk_recursive($a, function ($v, $k) use (&$b) { $new[$k] = $v; });
Using #splash58 trick with the spread operator if you have only one level deep:
$b = array_merge(...$a);

Related

Merge Two Aarrys [duplicate]

This question already has answers here:
PHP - Merging two arrays into one array (also Remove Duplicates)
(9 answers)
Closed 3 years ago.
I have two arrays one is the superset of other,how to create a third array which has all the values of both arryas but not repeating
me please
$a = array(1,2,3);
$b = array(1,2,3,4);
output must be like this
$c = array(1,2,3,4);
use array_merge() along with array_unique()
array_unique(array_merge($a,$b));
Output:-https://3v4l.org/lfm1b
Note:- if you want to re-index array use array_values() [added in my working example link]
Reference:
array_values()
$c = array_unique(array_merge($a, $b));
All of this can be seen in: http://php.net/manual/en/function.array-merge.php
Use array_merge with array_unique
print_r(array_unique(array_merge($a,$b)))
Use + sign to merge them
$c = $a + $b;
Working example :- https://3v4l.org/5P2LP

PHP - Two JSON lists in one (two-dimensional) [duplicate]

This question already has answers here:
How to create key value pair using two arrays in JavaScript?
(7 answers)
Closed 5 years ago.
I'm trying using PHP to bring the two JSON lists into one, as follows:
list1
[1,2,3,4,5]
list2
["a","b","c","d","e"]
How to join lists to get:
[[1,"a"],[2,"b"],[3,"c"],[4,"d"],[5,"e"]]
How to link two lists to one, as in a given example?
You need to loop through the arrays, and create a new one with the combined values:
<?php
$arr1 = [1,2,3,4,5];
$arr2 = ["a","b","c","d","e"];
$result = [];
foreach ($arr1 as $i => $a) {
$result[] = [$a, $arr2[$i] ?? ''];
}
?>

Sort the elements of the array [duplicate]

This question already has answers here:
PHP: Sort an array by the length of its values?
(12 answers)
Closed 5 years ago.
I have an array like the following:
$a= $array('PHP','HTML','JS','LARAVEL');
I want to sort the elements in the array, descending by the total number of characters of an element
$b= $array('LARAVEL','HTML','PHP','JS');
Please help me to descend the elements of the array, based on the number of characters in an array.
I see you have the Laravel tag, so you can use Laravel collections with the sortByDesc function for that.
$array = collect(['PHP','HTML','JS','LARAVEL'])->sortByDesc(function($value) {
return strlen($value);
});
You need to use usort():
function sort($a,$b){
return strlen($b)-strlen($a);
}
$array = ['PHP','HTML','JS','LARAVEL'];
usort($array,'sort');

How to array_push unique values inside another array [duplicate]

This question already has answers here:
array_unique vs array_flip
(4 answers)
array_unique and then renumbering keys [duplicate]
(1 answer)
PHP arrays: delete duplicates and reorder keys [duplicate]
(2 answers)
array_unique does not re-sort the array
(2 answers)
Removing undefined array indexes after calling array_unique
(2 answers)
Closed 8 months ago.
I have two arrays:
$DocumentID = array(document-1, document-2, document-3, document-4,
document-5, document-4, document-3, document-2);
$UniqueDocumentID = array();
I want to push the unique objects inside of $documentid array to $UniqueDocumentID array.
I can't use array_unique() as it copies the key of its predecessor array and I want sequential keys inside the $UniqueDocumentID array.
You could foreach() through $DocumentID and check for the current value in $UniqueDocumentID with in_array() and if not present add it. Or use the proper tool:
$UniqueDocumentID = array_unique($DocumentID);
To your comment about wanting sequential keys:
$UniqueDocumentID = array_values(array_unique($DocumentID));
The long way around:
$UniqueDocumentID = array();
foreach($DocumentID as $value) {
if(!in_array($value, $UniqueDocumentID)) {
$UniqueDocumentID[] = $value;
}
}

PHP - multidimensional array from arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}

Categories