PHP Values of One Array to Key of Another Array

Combine array values on another array keys in php

Define the keys that you want in the output array.

$keys = ['id', 'title', 'amount', 'price', 'type', 'installmenttype'];

Then map array_combine over 4-count chunks of your original array.

$products = array_map(function($product) use ($keys) {
return array_combine($keys, array_merge($product, [0,0]));
}, array_chunk($array1, 4));

(array_merge adds the zero values for the extra keys so that array_combine can be used.)

Append one array values as key value pair to another array php

Just walk $array1 and modify each sub-array by adding the new key and the value of $array2 with the same key:

array_walk($array1, function(&$v, $k) use($array2) { $v['date'] = $array2[$k]; });

Add rows from one array as child items to an array of parent rows

First create a temp array to store index of a given id_modulo from first array. So while pushing an item from second array to first array, we don't have to search every time.

$arrModuleIndex     =   [];
// $arr here is your first array.
foreach($arr as $key => $data){
$arrModuleIndex[$data['id_modulo']] = $key;
}

Output:
Array
(
[114] => 0
[118] => 1
[128] => 2
)

Now loop through second array and push it to items of first array at index based on temporary array.

// Here $arr2 is your second array
foreach($arr2 as $data){
$arr[$arrModuleIndex[$data['id_modulo']]]['items'][] = $data;
}

Output:
Array
(
[0] => Array
(
[id_modulo] => 114
[nome_modulo] => 1. Acessos
[items] => Array
(
[0] => Array
(
[id_modulo] => 114
[id_pergunta] => 547
[pergunta] => Example
[resposta] => C
)

[1] => Array
(
[id_modulo] => 114
[id_pergunta] => 548
[pergunta] => Example
[resposta] => C
)

[2] => Array
(
[id_modulo] => 114
[id_pergunta] => 550
[pergunta] => Example
[resposta] => C
)

)

)

[1] => Array
(
[id_modulo] => 118
[nome_modulo] => 4. Área de Vivência
[items] => Array
(
[0] => Array
(
[id_modulo] => 118
[id_pergunta] => 549
[pergunta] => Example
[resposta] => C
)

)

)

[2] => Array
(
[id_modulo] => 128
[nome_modulo] => 14. Supressão
)

)

Copy array key into another existing array key

You can use the function array_combine() to combine the keys from the second array (for the following example called $array_keys) with the values from the first array (called $array_values):

Example:

$combined = array_combine(array_keys($array_keys), $array_values);
print_r($combined);

This prints out the array exactly like you described.

PHP Find keys in one array that exist as values in another array, and pull back the value

I assume you use the ** to highlight things as print_r would not display it that way, rather [**address**]. But str_replace supports arrays, so search the second array for the keys in the first array and replace with the values of the first array:

$result = str_replace(array_keys($first), $first, $second);

Array merge or push key and values of one array into another array

Loop through first array then merge values in new array:

$array_1= array
(
0 => array
(
"supply_id" => 2
),

1 => array
(
"supply_id" => 4
),

2 => array
(
"supply_id" => 5
),

);

$array_2=array
(
"status" => 1,
"t1_id" => 59
);
$new_array = array();
foreach ($array_1 as $key => $value) {
$new_array[] = array_merge($value,$array_2);
}
var_dump($new_array);

o/p:

array (size=3)
0 =>
array (size=3)
'supply_id' => int 2
'status' => int 1
't1_id' => int 59
1 =>
array (size=3)
'supply_id' => int 4
'status' => int 1
't1_id' => int 59
2 =>
array (size=3)
'supply_id' => int 5
'status' => int 1
't1_id' => int 59

How to extract specific array keys and values to another array?

Just use a good ol' loop:

$newArray = array();
foreach ($oldArray as $entry) {
$newArray[$entry['id']] = $entry['type'];
}

Extract from array values having keys as values of another array

Here we are using array_intersect_key, array_flip and array_values. This single liner will be enough.

1. array_values will return values of an array.

2. array_flip will flip array over keys and values.

3. array_intersect_key will return array on the basis of two input array's over intersecting keys.

Try this code snippet here

print_r(
array_values(
array_intersect_key(
$first, array_flip($second))));

Select a value from an array based on another array in php

You can do it with this line where array_keys gives us an array of keys of our filtered array. end and max do the same job in this case because we have a sorted array and want to return its last index. So finally $percentiles[$lastIndexKeyOfOurFilteredArray] is what we print out:

echo $percentiles[max(array_keys(array_filter($percentile_bounds,function ($x) use ($total_score){return $x<$total_score; })))]


Related Topics



Leave a reply



Submit