create new array php multidimensional [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 8 years ago.
Improve this question
I have array
Array
(
[0] => Array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
)
how to create new array become
Array
(
[Personal Impact] => Array
(
[Position] => array
(
[0] => array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
)
)
)
Is it possible?

Yes it's possible you can just foreach(your values as key => value) {} it or something like this:
Sample Fiddle
<?php
$new_values = array();
foreach($old_values as $key => $values) {
$new_values[$values['cluster_name']][$values['driver_display_name']][] = array(
'answer1' => $values['answer1'],
'answer2' => $values['answer2'],
'PercentEVQ' => $values['PercentEVQ'],
'PercentEVQ2' => $values['PercentEVQ2'],
'driver_display_name' => $values['driver_display_name'],
'cluster_name' => $values['cluster_name'],
);
}

Are you defining a new array?
$new_array['Personal Impact']['Position'] = $old_array;

Related

Order multidimensional array according to a second 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 would like to order $ArrayToOrder according to column SecondArrayField2 in $SecondArray, where the link between the two arrays are Field_3 (in $ArrayToOrder) and SecondArrayField1 in $SecondArray.
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3
)
[1] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2
)
[2] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11
)
)
$SecondArray=Array
(
[0] => Array
(
[SecondArrayField1] => 11
[SecondArrayField2] => Bruce
)
[1] => Array
(
[SecondArrayField1] => 3
[SecondArrayField2] => Arthur
)
[2] => Array
(
[SecondArrayField1] => 2
[SecondArrayField2] => Mary
)
)
Desired result as follows:
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 //(Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 //(Mary)
)
)
Here is snippet you can use,
// first fetching key as SecondArrayField2 and SecondArrayField1 as value
$Field_3 = array_column($SecondArray, "SecondArrayField1","SecondArrayField2");
// sort by key alphabetically
ksort($Field_3);
//
$sorted = array_values(array_map(function($v) use ($ArrayToOrder) {
// first fetched Field_3 matching with current value of $Field_3 in the order
// get the index of matching Field_3
// save it to sorted array its sub array
return $ArrayToOrder[array_search($v, array_column($ArrayToOrder,'Field_3'))];
}, $Field_3));
print_r($sorted);die;
Demo.
Output:
Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 // (Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 // (Mary)
)
)

Multi Array - Counting Values of Elements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have this array:
Array(
[0] => Array(
[id] => 1,
[value] => 100
)
[1] => Array(
[id] => 1,
[value] => 200
)
[2] => Array(
[id] => 1,
[value] = 300
)
[3] => Array(
[id] => 2,
[value] = 100
)
[4] => Array(
[id] => 2.
[value] => 200
)
)
How can I count sum of [values]'s of the elements with the same [id] and put to array like:
Array(
[1] => 600,
[2] => 300
)
?
THANKS!
$array=array(array('id'=>1,'value'=>100),array('id' => 1,'value' => 200),array('id' => 1,'value' => 300),array('id' => 2,'value' => 100),array('id' => 2,'value'=> 200));
$res=array();
foreach($array as $a)
{
if(array_key_exists ($a['id'],$res ))
{
$res[$a['id']]+=$a['value'];
}
else
{
$res[$a['id']]=$a['value'];
}
}
print_r($res);
output
Array ( [1] => 600 [2] => 300 )

Array is not able to display in CI [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
Here is my php code:-
Array
(
[cus] => Anand Roy
[depart] => science>
[add1] => sdfrfrferwfrfrfeferv
[add2] => veververv eververv
[cty] => Jalpaiguri
[pin] => 735101
[st] => West Bengal
[em] => cosmilisation#gmail.com
[tele] => 919641758183
[fax] =>
[admin] => uh
[aphn] => 919641758183
[bill] => khkj
[bphn] => 919641758183
[key] => khkj
[kphn] => 919641758183
[am] => 1 am
[pm] => 1 pm
[week] =>
)
I m unable to display their data in view....
$data['myarray']=Array
(
[cus] => Anand Roy
[depart] => science
[add1] => sdfrfrferwfrfrfeferv
[add2] => veververv eververv
[cty] => Jalpaiguri
[pin] => 735101
[st] => West Bengal
[em] => cosmilisation#gmail.com
[tele] => 919641758183
[fax] =>
[admin] => uh
[aphn] => 919641758183
[bill] => khkj
[bphn] => 919641758183
[key] => khkj
[kphn] => 919641758183
[am] => 1 am
[pm] => 1 pm
[week] =>
); // initialize your array
$this->load->view("viewfile",$data); // pass the array to your view page
And in your viewfile, access it like :
foreach($myarray as $array) // foreach array elements
{
echo $array['cus']; // echo it based on array index
}

group the array by the same key and value and create json data with it [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
Array
(
[0] => Array
(
[religion] => Christian
[emp_count] => 4
[b_name] => tripura
[branch_id] => 7
)
[1] => Array
(
[religion] => Christian
[emp_count] => 2
[b_name] => Tirunelveli
[branch_id] => 10
)
[2] => Array
(
[religion] => Christian
[emp_count] => 4
[b_name] => Madurai
[branch_id] => 12
)
[3] => Array
(
[religion] => Hindu
[emp_count] => 3
[b_name] => tripura
[branch_id] => 7
)
[4] => Array
(
[religion] => Hindu
[emp_count] => 4
[b_name] => Tirunelveli
[branch_id] => 10
)
[5] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
[6] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
[7] => Array
(
[religion] => Muslim
[emp_count] => 1
[b_name] => Tirunelveli
[branch_id] => 10
)
[8] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
)
i need to group the array by the same key and value and create a json data with the grouped array.
For Eg: i need to display it as
{
name : 'Christian',
data :[4,2,4]
},
{name:'Hindu',
data:[3,2,1]
} ....
Give this a go!
Create your own array
Loop through your current array ($arrArray)
Group elements, by adding them to $arrGrouped
The code
$arrGrouped = array();
foreach($arrArray as $arrElement) {
if( array_key_exists($arrElement['religion'], $arrGrouped) ) {
$arrGrouped[$arrElement['religion']] += $arrElement['emp_count'];
} else {
$arrGrouped[$arrElement['religion']] = $arrElement['emp_count'];
}
}
echo '<pre>'. json_encode($arrGrouped, true) .'</pre>';
I would do this in two steps:
Group the same religion together and gather the counts
Map the result into the final data array
So:
$result = array_reduce($array, function(&$result, $current) {
$result[$current['religion']][] = $current['emp_count'];
return $result;
}, []);
$data = [];
foreach ($result as $religion => $emp_counts) {
$data[] = [
'name' => $religion,
'data' => $emp_counts,
];
}
echo json_encode($data);
See: array_reduce() json_encode()

PHP replace value by key in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array:
Array
(
[0] => Array
(
[id] => 31299
[name] => 37322426212
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322426212
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 4373
[name] => 37322983029
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322983029
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[auth_type] => ani
[name] => 37322983029
[ani] => 37322983029
[orig_enabled] => Array
(
[0] => on
)
[orig_rate_table] => 7
)
)
Items with 0,1,2 may be more(3,4...10...and so on).
What I am trying to do is to find array with key ani and name = 37322983029 and replace
name with '###' and ani to empty:
[mane]=>"###",//where name = '37322983029'
[ani]=> //where ani = '37322983029'
I tried with str_replace but no success.
How can this be done?
I think, this is what you are looking for:
foreach($yourArray as &$a)
{
if($a["ani"] == 37322983029 and $a["ani"] == $a["name"])
{
$a["name"] = "###";
$a["ani"] = "";
}
}
Not sure how you were using str_replace, but the following should work (assuming that ani and mane (or name?) are actually string indices into your array - not obvious from your example):
foreach($myArray as &$val) {
if($val['ani'] == 37322983029) {
$val['ani'] = '';
$val['name'] = "###";
}
}
important Note the use of & to pass the value by reference so you are working with actual elements of the array, not copies... In that way, when you modify $val you are actually modifying the original array. Without the ampersand, your initial array would be unchanged by the loop.
Also note - it is not clear from your example whether you want to test for just ani==37322983029 or also for name=37322983029. I am sure you can modify the code above to have both conditions (or see Alexxus's answer)
A complete code example (tested, working):
<?php
$a = Array(Array('a'=>4, 'b'=>5),Array('a'=>6, 'b'=>7));
echo '<pre>';
print_r($a);
foreach($a as &$v){
if($v['a']==6) {
$v['a'] = 12;
$v['b'] = '';
}
}
print_r($a);
echo '</pre>';
?>
Produces output
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 6
[b] => 7
)
)
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 12
[b] =>
)
)

Categories