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

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] ?? ''];
}
?>

Related

Filter array by skipping every 2nd and 3rd element from array [duplicate]

This question already has answers here:
Selecting every nth item from an array
(9 answers)
Remove every 3rd and 4th element from array in php
(1 answer)
How to access N-th element of an array in PHP
(3 answers)
Remove every nth item from array
(6 answers)
foreach step 5 steps forward in an array
(3 answers)
Closed 4 months ago.
i have a array like this
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
what i wan to remove
$remove = "b,c,e,f,h,i,k,l";
then i need a new array from the remaining elements like below
$new_arr = [a,d,g,j,m];
Use array chunk to split by 3 and take out first element.
<?php
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
$chunked =array_chunk($names, 3);
$filtered = [];
foreach($chunked as $chunk){
$filtered[] = $chunk[0];
}
var_dump($filtered);
?>
Instead of removing number 2 and number 3 from each 3 elements, the task is simply written like this:
Keep the first of every 3 items.
This can be determined using the index and the modulo operator %.
Using array_filter saves the foreach loop. This allows the solution to be implemented as a one-liner.
$names = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];
$result = array_filter($names,fn($k) => $k%3 == 0, ARRAY_FILTER_USE_KEY );
var_dump($result);
Demo: https://3v4l.org/sKTSQ
The $names array needs to be noted as in the code above. A notation without quotes like in the question produces error messages.

How do I extracting the multidimensional array value in php? [duplicate]

This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}

PHP array associative [duplicate]

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);

How To Find All Combinations From An Array In PHP [duplicate]

This question already has answers here:
PHP: How to get all possible combinations of 1D array? [duplicate]
(1 answer)
How To Find Words Combination In Php
(1 answer)
Closed 6 years ago.
I want to get all the possible combination from an array. I know this question has been asked several times but didnot worked for me correctly .
The array input should be single dimensional array as well as the output
for example:- input
$new_array=array('c','a','t','m','p');
possible combinations such as ca,ct,cm,cp,cat,cam,cap.... and soo on...
I have tried this but didnot worked correctly .
I have taken the code from PHP CookBook
function pc_array_power_set($array) {
// initialize by adding the empty set
$results = array();
foreach ($array as $element)
foreach ($results as $combination)
array_push($results, array_merge($eleme, $combination));
return $results;
}
$set = array('c', 'a', 't','m','p');
$power_set = pc_array_power_set($set);
print_r($power_set);
I need the output in single dimensional array

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