Populate php associative array [closed] - php

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.

Related

PHP: Recursively enhance array from set of strings [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 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

PHP - How to group file owner from an 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 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

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

Calling Array Function [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 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);

PHP generate json - list of categories [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 8 years ago.
Improve this question
Hi I need to generate json code with php like this http://api.androidhive.info/contacts/
You are looking for json_encode(). Passing an associative array to it will return a JSON representation.
http://us1.php.net/json_encode
You can use an multi-dimensional array with the json_encode function to achieve this structure.
$var = array(
"contacts" => array (
array(
"id" => 1,
"name" => "foo"
),
array(
"id" => 2,
"name" => "bar"
)
)
);
echo json_encode($var);

Categories