PHP: Recursively enhance array from set of strings [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array that needs to be enhanced dynamically from the values of several strings.
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
// desired result
$target = array(
1 => array(
2 => array(
4 => array(
12 => array(
4 => 'somevalue'
)
)
),
4 => array(
6 => 'somevalue'
)
)
);
Question is: how would I get from $paths to $target?
Thank you

Explode on / for a path say '1/4/6'. Now, you have 1,4 and 6.
Keep assigning them iteratively to the previous parent key. In the below code, I have made use of & to edit the same address location of the child.
<?php
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
$target = array();
foreach($paths as $path){
$temp = &$target;
foreach(explode("/",$path) as $key){
if(!isset($temp[$key])) $temp[$key] = array();
$temp = &$temp[$key];
}
$temp = 'some value';
}
print_r($target);
Demo: https://3v4l.org/P3VQB

Related

Populate php associative array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have an array as below in php:
enter image description here
i need to loop through the above array to get video url and push it in the below array using a for loop:
$post_data = array(
'sources' => array(
'src' => "video_url",
'type' => "mp4"
)
);
for each element in the first array, it need to add a new element in the second array to get the below result
$post_data = array(
'sources' => array(
'src' => "video1.mp4",
'type' => "video/mp4"
),
'sources' => array(
'src' => "video2.mp4",
'type' => "video/mp4"
)
);
I tried using a loop as below
for ($x = 0; $x < count($allAds); $x++) {
}
and using array_push did not do the work.

I have two array in php. Need to combing in one at following way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have two arrays:
$array1 = array('104', '104', '104', '51', '228', '228');
$array2 = array('12121', '12120', '12119', '11821', '11788', '11787');
I need to create an array with two dimensions consisting of the elements of these two arrays in a specific way:
$array3 = array('104'=>array('12121', '12120', '12119'),'51'=>array('11821'),'228'=>array('11788', '11787'));
Array1 and Array2 always have the same number of elements.
How can I do this?
You can do it this way:
<?php
$array1 = ['104', '104', '104', '51', '228', '228'];
$array2 = ['12121', '12120', '12119', '11821', '11788', '11787'];
$result = [];
$index = 0;
foreach( $array1 as $key => $value ){
$result[$value][] = $array2[$index];
$index++;
}
Result:

Make top of position from associative array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this array:
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
How I can get position by numeric value?
Ex: John will get position 4 because have high value
Eric get position 3.....
I want to find position of key by value!
Another way to do it like below,
<?php
function find_rank($name){
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
asort($top);
return array_search($name,array_keys($top))+1; # array index starts from zero that's why added extra 1
}
echo find_rank('John');
WORKING DEMO: https://3v4l.org/8rp95
This is not correct way to handle this but if you really have to deal with it here is a dirty painful to read code
asort($top);
$top = array_flip(array_values(array_flip($top)));
echo $top['Eric']; // will result 2 (list starts from 0 maybe you would like to +1 to result)
Another option:
asort($top);
$top = array_combine(range(1, count(array_keys($top))), array_keys($top));
echo $eric= array_search('Eric', $top); // return 3

how to manipulate laravel get request with multiple data set [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i got laravel request form data with multiple data set like:-
array:5 [▼
"start_date" => array("2019-01-01","2019-01-02","2019-01-03");
"planned" => array("1","2","3");
"planned_inc" => array("2","8","16.5");
"actual_inc" => array:10("7.9","1.2","3.6");
]
i want to manipulate data set like:-
$data = ['2019-01-01', '1', '2', '7.9'];
$formData = request(['start_date', 'planned', 'planned_incr', 'actual_incr' ,'actual']);
dd($formData);
foreach ($formData as $data) {
$jso = $data['start_date'];
$da = $data['planned'];
print_r($da);
}
I think you want to convert $formData into multidimensional array like below:
$dataSet = [];
foreach ($formData['start_date'] as $key=> $value) {
$dataSet[] = array(
'start_date' => $value,
'planned' => (isset($formData['planned'][$key]) ? $formData['planned'][$key]: ''),
'planned_inc' => (isset($formData['planned_inc'][$key]) ? $formData['planned_inc'][$key]: ''),
'actual_inc' => (isset($formData['actual_inc'][$key]) ? $formData['actual_inc'][$key]: ''),
);
}
print_r($dataSet);
Output:-https://3v4l.org/fo4j5

Array value as index [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have an array
$term = array(
0 => array(
'id'=>'0902001',
'name'=>'bob',
'cgpa'=>'3.81',
),
1 => array(
'id'=>'0902002',
'name'=>'jhon',
'cgpa'=>'3.52',
),
);
I want to make a new array that the id will be the index for cgpa of new array.
$new_arr = array(
'0902001' => '3.81',
'0902002' => '3.52',
);
Thanks!
This should work for you
for ($i=0, $c = count($term); $i<$c; ++$i) {
$new_arr[$term[$i]['id']] = $term[$i]['cgpa'];
}
// for old php version
$new_arr = array();
foreach($term as $value){
$new_arr[$value['id']] = $value['cgpa'];
}
// for php 5.5+
$new_arr = array_column($term, 'cgpa', 'id');

Categories