In PHP I have a dynamic 1-D Array which is like
$array = ['test1', 'test2', ..., 'testn'] And I need to convert this into a multidimensional array where array nesting level will be equal to the number of elements in the 1-D array, and each level will have its index with the name of 1-D array values, so, the output should be something like:
$multidimentional['test1']['test2'][...]['testn'] = [Some Fixed Value] And, after creating the multidimensional array assign a fixed value to it.
So, basically this fixed value needs to be assigned in a multidimensional array which has nesting level as per 1-D array values.
You can simply loop over the elements, assign the element as key to the current array and move on with the child array.
<?php
$array = ['test1', 'test2','test3', 'test4'];
$res = [];
$temp = &$res;
foreach($array as $val){
$temp[$val] = [];
$temp = &$temp[$val];
}
$temp[] = 45; // some fixed value
print_r($res);
Demo: https://3v4l.org/INfR2
Related
I want to push new array to another array at specific position for this purpose I used array_splice followed some stackoverflow links but it didn't work for me
I refered this links also but they mentioned only for single value not array.
How to insert element into arrays at specific position?
Insert new item in array on any position in PHP
Example:
$array_1 = array(1,2,3,4,5);
$array_2 = array(a,b,c);
Now I want to push $array_2 values in $array_1 at certain position like:
a at position 1
b at position 3
c at position 4
Expected output:
$final_array=(1,a,2,b,c,3,4,5);
You need to define positions as array and combine it with array_2. Now iterate over this combined array and use code of first reference thread:
<?php
$array_1 = array(1,2,3,4,5);
$array_2 = array('a','b','c');
//define positions array
$positions = array(1,3,4);
//combine position array with $array_2
$positionArray = array_combine($positions, $array_2);
//iterate over combined array
foreach($positionArray as $key => $value){
//use code of first example thread
array_splice($array_1, $key, 0, $value);
//here $key is position where you want to insert
}
print_r($array_1);
Output: https://3v4l.org/Djar2
I want to combine two arrays. First use the first array as the key(combining the duplicates) then add the values from the second array to adjust to the specific keys
//first array
array('1','0','1');
//second array
array('50','10','20');
//output -> first array ('1','0') second array -> ('70','10')
removing the duplicates in the first array and adding the corresponding "duplicate" key in the second array values
Use a result array to collect the outcomes like this:
//first array
$k = array('1','0','1');
//second array
$v = array('50','10','20');
$result = array();
foreach($k as $index => $value) {
if(!isset($result[$value])) {
$result[$value] = 0;
}
$result[$value] += $v[$index];
}
print_r($result);
hi i have three array like this
$arr1 = array(2,3,4,5);
$arr2 = array(1,2,3,4);
$arr3 = array();
i need a loop function to duplicate each of the value inside $arr2 with the value inside $arr1 so the end result should look like this:
$arr3= array(1,1,2,2,2,3,3,3,3,4,4,4,4,4,4);
i know that i need to do an array_push into the $arr3 with $arr2[i] by doing this
for($i=0;$i < count($arr2);$++){
array_push($arr3,$arr2[$i]);
}
but i dont know the outer loop for iterating the array_push loop, what should i add to do the duplicating?
Solution 1: You need to apply a foreach() and for() loop
1.Iterate over the first array $arr1
2.Check that value with the same key of the first array exists or not in the second array
3.Apply a loop based on first array values
4.Assign same value repeatedly based on loop
foreach($arr1 as $key=>$arr){
if(isset($arr2[$key])){
for($i=0;$i<$arr;$i++){
$arr3[] = $arr2[$key];
}
}
}
print_r($arr3);
Output:-https://eval.in/1005648
Solution 2: You can use array_merge() and array_fill()
foreach($arr1 as $key=>$arr){
$arr3= array_merge($arr3,array_fill(count($arr3),$arr,$arr2[$key]));
}
echo "<pre/>";print_r($arr3);
Output:-https://eval.in/1005666
I have String which look like this,
1|2|3|4
and I'm creating a array from that string by,
$arr = explode("|", $data['my_list']);
and then from each element of the array i need to pair with another value by creating associative array. After create, it should look like this,
1=>1
1=>2
1=>3
1=>4
so inside a loop I need to create this associative array. Can anybody please explain how to do this
$tempArr = array();
$arr = explode("|", $data['my_list']);
foreach($arr as $item) {
$tempArray['associative_with_key_' . $item] = 5; // where 5 is a new value
}
I've got an array containing values I wish to use as keys, such as:
$keys = array("first", "second", "third", "fourth");
The count and contents of these values will be changing dynamically within a loop. I want them to become the keys of a multidimensional array, but the count of the keys array will always be changing, so while this would work for that first array of keys:
$multidimensional[$keys[0]][$keys[1]][$keys[2]][$keys[3]] = "some value";
Later in the loop the keys may be something like:
$keys = array("first", "second", "gamma", "delta", "theta", "kappa");
So using this in the loop:
$multidimensional[$keys[0]][$keys[1]][$keys[2]][$keys[3]] = "some value";
Will not work, and needs to be dynamic too based on the count of the keys.
I've gone through each of the array functions in the PHP manual and can't seem to find something that fulfills this purpose. Am I totally overlooking something basic here? Maybe some curly brace magic?
There you go...
function setMultidimensionalValue($value, array $keys, array $multidimensional)
{
$node = &$multidimensional;
foreach ($keys as $key)
{
if (!isset($node[$key]))
$node[$key] = null;
$node = &$node[$key];
}
$node = $value;
return $multidimensional;
}
// Example of usage
$multidimensional = array();
var_dump(setMultidimensionalValue('value', array('first', 'second', 'third'), $multidimensional));