please find below contains array in php
$data = array ( 'questions' => array ( 1 =>
array (
'section_id' => '61',
'questionid' => '2035',
'time_spent' => '0',
),
2 =>
array (
'section_id' => '61',
'questionid' => '2036',
'time_spent' => '0',
),
3 =>
array (
'section_id' => '61',
'questionid' => '2037',
'time_spent' => '0',
),
),)
how can i access section_id, and time_spent data from array
Please use the below code : Also see this link
foreach($data as $key => $values){
foreach ($values as $ikey => $secArr) {
# code...
$section_id = $secArr['section_id'];
$time_spent = $secArr['time_spent'];
echo 'Section Id: '.$section_id.' | Time Spend:'.$time_spent.'</br>';
}
}
I hope it will helps you.
Related
I got 2 arrays. In first one field number is empty:
array (
3 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 40,), ),
4 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 40, ), ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 41, ), ),
7 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 41, ), ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41, ), ),
)
In the second one fields number are not empty, however the array is bit different, because it doesn't have a 2 arrays in the middle (id = 3, time = 40 and id = 3, time = 41).
array ( 3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
6 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
)
What I want to do is to some sort of merge of this arrays into one looking like this:
array (
3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
7 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41,), ),
)
I tried some variations of array_merge() and array_combine(), but neither of them seems to work, or perhaps I've been using them badly.
Again, notice how row 5 and 8 are not affected because (id = 3 and time = 40) and (id = 3 and time = 41), respectively, are not represented in the second array.
What I tried and it works, but it doesn't look well in my opinion:
foreach($arr2 as $wpis2){
foreach($arr as $key=>$wpis){
if($wpis2['id'] == $wpis[0]['id'] && $wpis2['time'] == $wpis[0]['time']){
$arr[$key] = $wpis2;
}
}
}
You will need to iterate over the first array in order to populate a key-map based on the combination of id and time values from all rows.
Use a second loop to iterate over the second array and access the correct key to overwrite by leveraging the mapping array.
Code: (Demo)
$map = [];
foreach ($first as $k => [['id' => $id, 'time' => $time]]) {
$map["{$id}_{$time}"] = $k;
}
foreach ($second as $row) {
$key = $map["{$row['id']}_{$row['time']}"];
$first[$key] = $row;
}
var_export($first);
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']
]);
}
I am trying to rebuild php array, which I can get with one query from database.
To build lists and add items I need the array to be grouped and rebuild.
From query I receive array like this:
array (
0 =>
array (
'item_id' => '1',
'item_name' => 'aaa',
'group_id' => '7',
'group_name' => 'first'
),
1 =>
array (
'item_id' => '2',
'item_name' => 'bbb',
'group_id' => '7',
'group_name' => 'first'
),
2 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
'group_id' => '9',
'group_name' => 'second'
),
3 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
'group_id' => '9',
'group_name' => 'second'
)
);
I need to rearrange this array to following, which I can use for js parsing:
array(
array ('group_id' => '7', 'group_name' => 'first', 'items' => array (
0 => array (
'item_id' => '1',
'item_name' => 'aaa',
),
1 => array (
'item_id' => '2',
'item_name' => 'bbb',
)
)
),
array ('id' => '8', 'name' => 'second', 'items' => array (
0 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
),
1 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
)
)
)
);
I have stuck with following code:
function rebuild($groupby, $param, $data) {
$newarray = array();
foreach($data as $key => $val) {
if(array_key_exists($groupby, $val)){
$newarray[$val[$groupby]] = $val[$param];
$newarray['items'][] = $val;
}else{
$newarray[""][] = $val;
}
}
return $newarray;
}
$show = rebuild('group_id', 'group_name', $data);
echo "<pre>" . var_export($show, true) . "</pre>";
Any advise would appreciated.
I always like to use the unique identifier (here, the group ID), as array key in the resulting array - that makes it easier to associate the right items to the right new entry, without having to “search” the new array for the entry with the matching identifier.
$newarray = array();
foreach($data as $item) {
$newarray[$item['group_id']]['group_id'] = $item['group_id'];
$newarray[$item['group_id']]['group_name'] = $item['group_name'];
$newarray[$item['group_id']]['items'][] = [
'item_id' => $item['item_id'],
'item_name' => $item['item_name']
];
}
$newarray = array_values($newarray); // resets the array keys to a zero-based index
var_dump($newarray);
group_id and group_name get overwritten in the result array elements all the time - but since we access the correct entry by the group id already, that does not matter. They will always be “overwritten” with the values they already have, so that does no harm.
And item_id and item_name simply get added to the items array of the entry identified by the group id.
I have a nested array who is 4 levels deep.
I would like to replace the keys by numerical ones but not the meta level, this array should keep the alphabetical keys.
The array will have a lot of values and there will be more accounts like Marco Mueller, this is just a short example. It is important that it will keep the same nested structure, after its replaced by numerical keys.
$array = array (
'Marco Mueller' =>
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'Conrad, Ute' =>
array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'Fidor Bank' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'H1-2019' =>
array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
),
),
);
The result should be
$array = array (
array (
'meta' =>
array (
'accountName' => 'Marco Mueller',
'accountId' => '1',
'sum' => '3,659.06',
),
'items' => array (
'meta' =>
array (
'customerName' => 'Conrad, Ute',
'customerId' => '8391',
'sum' => '457.59',
),
'items' =>
array (
'meta' =>
array (
'bankName' => 'Fidor Bank',
'bankKey' => 'FID',
'sum' => '457.59',
),
'items' => array (
'meta' =>
array (
'periodName' => 'H1-2019',
'periodId' => '5',
'sum' => '457.59',
),
),
),
array (
'meta' =>
array (
'periodName' => 'H2-2019',
'periodId' => '6',
'sum' => '600',
),
),
),
),
),
);
I solved my problem with this function, if anyone has an idea how to make this more elegant and efficient, your welcome!
private function getValues(array $array)
{
$newArray = [];
foreach ($array as $item) {
$meta = $item['meta'];
unset($item['meta']);
$arrayValues = array_values($item);
if (count($arrayValues) > 1 || count($arrayValues[0]) > 1) {
$arrayValues = $this->getValues($arrayValues);
}
if (!$arrayValues) {
$values = [
'meta' => $meta,
];
} else {
$values = [
'meta' => $meta,
'items' => $arrayValues,
];
}
$newArray[] = $values;
}
return $newArray;
}
How can I combine for example this arrays?
(int) 0 => array(
'Test' => array(
'id' => '108',
'bericht' => '12',
'fzge' => '35'
'treiber' => 'Analyse'
)
),
(int) 1 => array(
'Test' => array(
'id' => '109',
'bericht_id' => '12',
'fzge' => '25',
'treiber' => 'Analyse'
)
)
The result that I want is somethings like: 'Analyse' => '60', so I would like to sum the "fzge" if "treiber" is the same.
Thank you in advance.
Use the below code. Considering keys like ('Test', 'treiber' and 'fzge') of array remain same for all arrays
// declare new array
$new_array = array();
foreach($array as $key=>$value){
$new_array[$value['Test']['treiber']] = (isset($new_array[$value['Test']['treiber']])) ? $new_array[$value['Test']['treiber']]+$value['Test']['fzge'] : $value['Test']['fzge'];
}
print_r($new_array);
Output:
Array
(
[Analyse] => 60
)