PHP Prepend Associative Array with Literal Keys

Prepend associative array elements to an associative array

Can't you just do:

$resulting_array = $array2 + $array1;

?

PHP - add item to beginning of associative array

You could use the union operator:

$arr1 = array('key0' => 'value0') + $arr1;

or array_merge.

php - prepend an array value with key

Your $choices array has only numeric keys so array_unshift() would do exactly what you want.

$choices = array(
array('label' => 'test1','value' => 'test1'),
array('label' => 'test2','value' => 'test2'),
array('label' => 'test3','value' => 'test3'),
);
echo $choices[0]['label']; // echoes 'test1'

$array_to_add = array('label' => 'All','value' => 'all');
array_unshift($choices, $array_to_add);

/* resulting array would look like this:
$choices = array(
array('label' => 'All','value' => 'all')
array('label' => 'test1','value' => 'test1'),
array('label' => 'test2','value' => 'test2'),
array('label' => 'test3','value' => 'test3'),
);
*/
echo $choices[0]['label']; // echoes 'All'

Prepend key value to array - PHP

Un-shift should work if you pass the arguments correctly:

array_unshift($data["data"], $prepend);

Alternatively, you could use array_merge, like this:

$data["data"] = array_merge(array($prepend), $data["data"]);

With the following example data:

$data = [
"data" => [
[
"value" => "1145",
"label" => "11:45"
]
]
];

$prepend = [
"value" => "asap",
"label" => "ASAP"
];

$data["data"] = array_merge(array($prepend), $data["data"]);
print_r($data);

You would get this output (with both solutions):

Array (
[data] => Array (
[0] => Array (
[value] => asap
[label] => ASAP
)
[1] => Array (
[value] => 1145
[label] => 11:45
)
)
)

In PHP, for a specific key in an associative array, find identical keys and combine values into a single string variable

$items = [
['id' => 1000012, 'kp_uid' => 100000570, 'assigned_uid' => 'tim.hughes@sampleco.com', 'full_name' => 'Tim Hughes'],
['id' => 1000013, 'kp_uid' => 100000570, 'assigned_uid' => 'brad.slater@sampleco.com', 'full_name' => 'Brad Slater'],
['id' => 1000014, 'kp_uid' => 100000570, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis'],
['id' => 1000015, 'kp_uid' => 100000597, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis']
];

$grouped = [];

// group items by kp_uid
foreach ($items as $item) {
$grouped[$item['kp_uid']][] = $item;
}

function mapNamesCallback($item)
{
return $item['full_name'];
}

// iterate over groups and return a single item
// in form of kp_uid => x, full_names => 'name, name2 etc.'
$result = array_map(function ($group, $kpUid) {
return ['kp_uid' => $kpUid, 'full_name' => implode(', ', array_map('mapNamesCallback', $group))];
}, $grouped, array_keys($grouped));

This returns desired result.
I left a few comments in the code for clarity.

Create an array from an associative array using one of its value in a key value pair

You are nearly there, just need to make the new array all in one go and just use the $arry[$val->language][] to create a new sub array under that new or existing language key.

Also $data is an array or arrays not an array of objects so the addressing of the items was wrong.

$arry = array();
foreach ($data as $val) {
$arry[$val->language][] = ['id' => $val['id'], 'config_id' => $val['config_id']];
}

How to insert element into arrays at specific position?

array_slice() can be used to extract parts of the array, and the union array operator (+) can recombine the parts.

$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array)-3, true);

This example:

$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);

gives:


Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)

Associative arrays - grabbing specific keys and values

Just Change your foreach loop

This:

<?php
$items = [
'Mac' => [
'quantity' => $qty1,
'price' => 1899.99
],
'Razer Mouse' => [
'quantity' => $qty2,
'price' => 79.99
],
'WD HDD' => [
'quantity' => $qty3,
'price' => 179.99
],
'Nexus' => [
'quantity' => $qty4,
'price' => 249.99
],
'Drums' => [
'quantity' => $qty5,
'price' => 119.99
]
];

foreach($items as $key => $arrVal) {
if($key == 'Mac')
echo $key ."=". $arrVal['quantity'];
}
?>

The if(){} controls which key is getting printed so if you want to print
Nexus just change the value from Mac to Nexus or smthing and if you want to check multiple ones just use || like this if($key == 'Mac' || $key == 'Nexus')

|| = OR

And if you want to get price just change $arrVal['quantity']; to $arrVal['price'];



Related Topics



Leave a reply



Submit