I have a number of arrays like the one below. These are the result of data retrieved from the database. I need the final array to have the structure mentioned because I need to foreach loop each 'notes' in the successive code. I can't use a for loop only.
array('title' => 'Testing',
'content' => 'some text',
'created' => 'time',
'tags' => array(array('title' => 'hello')),
)
array('title' => 'Testing',
'content' => 'some text',
'created' => 'time',
'tags' => array(array('title' => 'hello')),
)
I want to merge all of these in an array of the format:
array(
array('notes' =>
array('title' => 'Testing',
'content' => 'some text',
'created' => 'time',
'tags' => array(array('title' => 'hello')),
),
),
array('notes' =>
array('title' => 'Testing',
'content' => 'some text',
'created' => 'time',
'tags' => array(array('title' => 'hello')),
),
),
);
I've tried many PHP functions but none gave me the result I want. Can someone help me please? Is there a built-in PHP function that can do this? Can someone show me an example.
Thanks.
If I'm understanding your question correctly, all you actually want to do is place each of your array elements into another array with the key notes and the value being that of the element (another array).
In which case either using foreach or array_map will do.
$newArray = array_map(function($v) { return ['notes' => $v]; }, $array);
Or using foreach (the more verbose way)...
foreach($array as $value) {
$newArray[] = ['notes' => $value];
}
this is a comment i want the formatting...
You have asked for an 'array' of 'array entries' where each entry
is an array
each array has a key of 'notes' and an associated data array.
I think the 'tags' part of you structure would be easier to use without the extra level of 'nesting'.
Here is the code to create and display the 'wanted' output with the 'tag' entry changed:
$wanted = array(
array('notes' =>
array('title' => 'Testing 1',
'content' => 'some text 1',
'created' => date('Y-m-d H:i:s'),
'tags' => array('title' => 'hello',
'tagFoo' => 'foo' ),
),
),
array('notes' =>
array('title' => 'Testing 2',
'content' => 'some text 2',
'created' => date('Y-m-d H:i:s'),
'tags' => array('title' => 'hello',
'tagBar' => 'bar'),
),
),
);
echo '<pre>';
print_r($wanted);
echo '</pre>';
Here is the output:
Array
(
[0] => Array
(
[notes] => Array
(
[title] => Testing 1
[content] => some text 1
[created] => 2015-01-04 15:39:34
[tags] => Array
(
[title] => hello
[tagFoo] => foo
)
)
)
[1] => Array
(
[notes] => Array
(
[title] => Testing 2
[content] => some text 2
[created] => 2015-01-04 15:39:34
[tags] => Array
(
[title] => hello
[tagBar] => bar
)
)
)
)
one possibilyti is to do:
foreach($tags as $key => $note){
$note["created"] = $created[$key]; //if created is an array
$note["created"] = $created; //if created is a string
}
Related
Hello i am trying to create data set like
Expected output:-
Array
(
[0] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Cyan
),
[1] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
)
but i am not sure what is missing in code.
Here is the code
$array = array(
0 => array(
'id_product_attribute' => '17615',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Cyan',
'id_attribute' => '1',
),
1 => array(
'id_product_attribute' => '17616',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Red',
'id_attribute' => '21',
),
);
$ids = array();
foreach ($array as $combinations) {
$ids['sku'] = 'sku';
$ids['variant_option_one_name'] = $combinations['group_name'];
$ids['variant_option_one_value'] = $combinations['attribute_name'];
}
print_r($ids);//
Here i am getting
Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
The above output i am getting. Seems like data is overwrite
Any correction to get both the data ?
I do not get both the colors in array. It
Thankyou
You're absolutely right, the values are being overwritten each time.
What you need to do is, each time you loop you should create a new array containing your values, and then assign that array to a new index inside the main array (so you get an array of arrays, like the expected output you've shown):
foreach ($array as $combinations) {
$arr = array();
$arr['sku'] = 'sku';
$arr['variant_option_one_name'] = $combinations['group_name'];
$arr['variant_option_one_value'] = $combinations['attribute_name'];
$ids[] = $arr;
}
Live Demo: http://sandbox.onlinephpfunctions.com/code/3a0cf7f8cbb994ef4192c1e23493bef397785937
foreach ($array as $combinations) {
array_push ($ids, [
'sku' => 'sku',
'variant_option_one_name' => $combinations['group_name'],
'variant_option_one_value' => $combinations['attribute_name']
]);
}
My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);
I have an array that look like this($myArray)
Array ( [0] => Array ( [0] => Array ( [ID] => 322 [Number] => 1 [Date] => 3117-01-41 [example] => Hello )
[1] => Array ( [ID] => 123 [Number] => 49 [Date] => 1717-05-21 [example] => Hi )
[2] => Array ( [ID] => 007A [Number] => 42 [Date] => 2005-11-24 [example] => Some Text )
[3] => Array ( [ID] => 999AAA [Number] => 492 [Date] => 3117-01-21 [example] => Text Test Text )))
In my page i am using a function which returns content($content) and that content is displayed onto the web browser. No echoing or printing just returning the content variable which is constantly being appended to.
I deally i want to loop through my array and print the values from a certain field to the screen for example
while(//not sure what goes here){
$content .= '<p>'.$someVariable["Number"].'</p>';
$content .= '<p>'.$someVariable["example"].'</p>';
$content .= '<p>'.$someVariable["date"].'</p>';
}
Im not sure if the while loop is the best way to achieve the desired result. Also using fetch_array is not an option i can use because it breaks previous code.
<?php
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Number' => 49,
'Date' => '1717-05-21',
'example' => 'Hi'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => 'Text Test Text'
)
);
?>
print whole array:
<?php print_r($yourArray); ?>
print certain levels of array:
<?php print_r($yourArray[2]); ?>
print certain element of array:
print_r($yourArray[2]['Date']);
print array level by level:
<?php
for($index=0; $index < count($yourArray); $index++){
echo $yourArray[$index]['ID'].'<br>';
echo $yourArray[$index]['Number'].'<br>';
echo $yourArray[$index]['Date'].'<br>';
echo $yourArray[$index]['example'].'<br>';
};
?>
This helps?
<?php
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Address' => '1283 street, 576',
'Date' => '1717-05-21',
'country' => 'Canada'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => array(
'subID' => 45,
'subAlias' => 'teste sub item'
)
)
);
$output = "";
function walkerFunction($item, $key) {
global $output;
$output .= $key . "->" . $item . PHP_EOL;
}
array_walk_recursive($yourArray, "walkerFunction");
echo $output;
It seems you are seeking this:
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Number' => 49,
'Date' => '1717-05-21',
'example' => 'Hi'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => 'Text Test Text'
)
);
$content = '';
foreach($yourArray as $key => $value):
$content .= '<p>'.$value["Number"].'</p>';
$content .= '<p>'.$value["example"].'</p>';
$content .= '<p>'.$value["Date"].'</p>';
endforeach;
print $content;
have an array like this:
array (
'#attributes' =>
array (
'status' => 'ok',
),
'time_entries' =>
array (
'#attributes' =>
array (
'page' => '1',
'per_page' => '100',
'pages' => '1',
'total' => '33',
),
'time_entry' =>
array (
0 =>
array (
'time_entry_id' => '1411884',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '1.75',
'date' => '2017-02-20',
'notes' => 'What is Quadra, Events Slider, Event Page Setup',
'billed' => '0',
),
1 =>
array (
'time_entry_id' => '1411254',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '1.5',
'date' => '2017-02-17',
'notes' => 'Events Slider, Background overlay, Intro',
'billed' => '0',
),
2 =>
array (
'time_entry_id' => '1410694',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '2.75',
'date' => '2017-02-16',
'notes' => 'Background Image SVGs, Header, Footer',
'billed' => '0',
),
3 =>
array (
'time_entry_id' => '1410586',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '0.5',
'date' => '2017-02-15',
'notes' => 'Site Background
Assign Less Variables',
'billed' => '0',
),
4 =>
array (
'time_entry_id' => '1409621',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '0.25',
'date' => '2017-02-14',
'notes' => 'Theme Install',
'billed' => '0',
),
...it actually goes on further than those first 4. What I am looking to do is sort these arrays by the key ["task_id"] so that I can group those together, and then add together the ["hours"] key - so that at the end I can output the total number of hours worked on under each task_id.
I've tried a bit of 'array_merge_recursive'and similar but I'm at a loss; this is PHP a good bit above my level. Help very appreciated.
So you're probably just better off doing a group by and summation by generating a new set of data rather than trying to modify this array.
Something like this will give you what you're looking for:
$time_entries = $your_array["time_entries"]["time_entry"];
$task_hours = [];
foreach ($time_entries as $time_entry) {
if (!isset($task_hours[$time_entry["task_id"]])) {
$task_hours[$time_entry["task_id"]] = 0;
}
$task_hours[$time_entry["task_id"]] += (float) $time_entry["hours"];
}
$task_hours would give you a value like:
Array
(
[3296] => 5
[1879] => 0.25
)
Instead of sorting the array and then summing the hours why not process over the relevant part of that array and do the summation all in one go.
$tots = array();
foreach( $array['time_entry'] as $task ) {
if ( isset($tots['task_id']) ) {
$tots['task_id'] += $task['hours'];
} else {
$tots['task_id'] = $task['hours'];
}
}
You should now have an array where the key is the task_id and its value is the summation of all that keys hours.
If you want the task_id's in order then sort the $tots array before outputing any results
ksort($tots);
If you want the results in order of the number of hours
asort($tots);
I am not sure if question title is ok or not!
I am trying to dynamically input array inside an array.
This is how it should be:
Example array:
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => TRUE,
'number' => -1,
'order' => 'DESC',
/*i want this part to be dynamic */
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
/*i want this part to be dynamic */
),
);
This is what I have:
//this array contains all works
$works = array(
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
);
Now how can I pass this to $myArray dynamically?
My desired array format
Array
(
[id] => 123
[name] => sufi
[works] => Array
(
[show_work] => TRUE
[number] => -1
[order] => DESC
[0] => Array
(
[title] => developer
[experience] => 5 years
[company] => ABC Inc.
)
[1] => Array
(
[title] => CEO
[experience] => 1 year
[company] => XYZ LLC.
)
)
)
Hope I understood your question correctly:-
Try this:-
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => true,
'number' => -1,
'order' => 'DESC'
)
);
$works = array(
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
);
foreach ( $works as $work ) {
$myArray ['works'][] = $work;
}
Final Array will be:-
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => TRUE,
'number' => -1
'order' => 'DESC',
[0] => array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
[1] => array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
),
);
Thanks to those nice people, who tried to help me. I have solved it based on idea from #Ashutosh.
This is the solution:
foreach ( $works as $work ) {
$myArray ['works'][] = $work;
}