Use array values as new multidimensional array keys [duplicate] - php

This question already has answers here:
Using a string path to set nested array data [duplicate]
(8 answers)
Closed 4 years ago.
Edit (after downvote): There is a similar question here > Using a string path to set nested array data
However I didn't find that question when searching for an answer due to the way it's worded, and I'm sure this will happen for other people, so this question may act as a useful gateway to that question and it's answers.
I'm sure I'm missing something obvious, but I can't think how to do this: I have an array containing one or more items:
array('value1', 'value2');
I need to use these values as the keys in a multidimensional array :
array['value1']['value2'] = 'somevalue';
How do I do this?

You can use a nice recursion here:
function nestArray($items, $value) {
return $items ?
array($items[0] => nestArray(array_slice($items, 1), $value))
: $value;
}
$array = array('value1', 'value2');
print_r(nestArray($array, 'somevalue'));

Related

Set Array Equal to Another Array without Specified Key/Values [duplicate]

This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.

Turning PHP key=>value into sub array: [key,value] [duplicate]

This question already has answers here:
reformat an php array [duplicate]
(2 answers)
Closed 10 months ago.
I'm having following data:
Array ( [one] => this [two] => this2 )
Which I want to convert to json type which looks like:
data:{[one,this],[two,this2]}
How can I get through this in effecient manner?
Edit:
I've tried a lot of things This is actual data which I need to make datatable compatible:
{"draw":0,"recordsTotal":null,"recordsFiltered":null,"data":‌​[[{"first":[""],"sec‌​ond":[""],"third":["‌​"],"fourth":[""],"fi‌​fth":[""],"sixth":["‌​value"]}]]}
as the data here is in key=>value form json is not compatible for datatables (PHP)
You can use array_map and array_keys:
$result = array_map(null, array_keys($array), $array);
$json = json_encode($result);
Here is working demo.

Why to declare array before setting it (php)? [duplicate]

This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)
You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.

Get nth key of associative php array [duplicate]

This question already has answers here:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);
array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];

Php Unset All Keys [duplicate]

This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.

Categories