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
Related
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.
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 1 year ago.
Improve this question
I have the following array:
$fields = ['field1', 'field2', 'field3'];
What I would like to do using an existing helper ('hopefully') is to convert those array string values into object key value pairs with an empty value.
This is the desired output:
$fields = (object) [
'field1' => '',
'field2' => '',
'field3' => '',
];
Is it possible to do that using a Laravel helper, so I don't have to use my own function?
You could try the following code to create a generic object.
$obj = new \stdClass();
foreach ($fields as $field) {
$obj->$field = '';
}
The reason this works is that it will iterate over each field, add set properties on the object, using the value of the array.
Note the ->$ symbol. This is because PHP will solve the $ into the value of the variable, then it will set that as the property after the arrow.
If you were to use an associative array, then you could take this a step further.
$fields = ['field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3'];
foreach ($fields as $field => $value) {
$obj->$field = $value;
}
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
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 3 years ago.
Improve this question
I have this array:
$files = [
'text.php' => 'Philip Adams',
'help.php' => 'Bobby Barnett',
'video.php' => 'Philip Adams',
'events.php' => 'Bruce Aguilar',
'mysql.php' => 'Bobby Barnett',
'sunday.php' => 'Victoria Rose',
'facebook.php' => 'Bobby Barnett',
'blog.php' => 'Philip Adams',
'view.php' => 'Victoria Rose',
];
Now, I want to make the array like this:
$files = [
'Philip Adams' => [
'text.php',
'video.php',
'blog.php',
],
'Bobby Barnett' => [
'help.php',
'mysql.php',
'facebook.php',
],
'Bruce Aguilar' => [
'events.php',
],
'Victoria Rose' => [
'sunday.php',
'view.php',
],
];
How can I do this?
You can loop the existing array by key/value and push to a new array, something like:
$output = []; // Declare an empty array to hold the output
// Loop the $files array by $file (eg: text.php) and $owner (eg: Philip Adams)
foreach($files as $file => $owner){
$output[$owner][] = $file; // Add the $file to the $owner key in the new array
}
print_r($output); // Output the array
Working example
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 years ago.
Improve this question
How do I create an input argument and call the function defined below?
function insert_data ($var) {
$set_entry_array = array(
array('name' => 'name' ,'value' => $var['phone']),
array('name' => 'email' ,'value' => $var['circle']),
array('name' => 'assigned_user_id' ,'value' => 1),
);
}
I want to call this function to pass the actual value in the name and email parameter.
Your function accepts an array. You need to define the array, and pass it to the function. It appears the function expects two array keys to be set: phone and circle. We need to set them.
$myarray = array();
$myarray['phone'] = '555-1234';
$myarray['circle'] = 'foo#bar.com';
insert_data($myarray);