I am trying to parse this array in order to save it in the database, but I can't do it.
This is what $this->input->post("question") produces:
Array
(
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
[2] => Array
(
[0] => queston 2
[answer] => Array
(
[0] => answer 21
[1] => answere 22
[2] => anwer 23
[3] => answer 24
[4] => answer 25
)
)
)
I tried this:
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
$data = array(
'question' => $question,
);
$this->db->insert('questions', $data);
$question_id = $this->db->insert_id();
//another foreach to go throw answers
}
}
Just to check if I save the questions correctly, but I am getting this message:
Message: Array to string conversion
First of all, you are getting that message because you are trying to convert an array to a string.
Second:
foreach ($this->input->post("question") as $questions) {
}
first level. After this, you remain with:
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
//here, $question is formed of 2 arrays
//[0] => question 1
//[answer] => Array
// (
// [0] => answer 1
// [1] => answer 2
// [2] => answer 3
// [3] => asnwer 4
// [4] => answeer 5
// )
}
}
that is the second level. so in that foreach, you should use:
$data = array(
'question' => $question[0], // to get the question string
);
Let me know if anything more is needed!
P.S: for answers, you have to go one level deeper, and make another foreach.
Related
I want to create a new array using loop(foreach).
My array is looking like this :
$q_list = Array(
[0] => Array
(
[id] => 2
[subject_id] => 1
[question] => Question No One
[recordstatus] => 1
)
[1] => Array
(
[id] => 3
[subject_id] => 1
[question] => Question No Two
[recordstatus] => 1
)
[2] => Array
(
[id] => 4
[subject_id] => 1
[question] => Question No Three
[recordstatus] => 1
)
)
I have done like this but not working :
foreach ($q_list as $key => $q) {
$question[] = $q['question'];
$question[] = $q['subject_id'];
}
This will group your array on subject_id.
I use subject_id as the key in a multidimensional array that way it will just add the question arrays to the correct subarray.
foreach($q_list as $q){
$res[$q['subject_id']][] = $q;
}
var_dump($res);
https://3v4l.org/HnlYW
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}
This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.
I'm so tired of arrays today - thrown me all over the place.
So, here's the output of an array:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091909] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091901] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
I'm going to be writting a preg_match to iterate each [2010091907], but before I can, I do not understand how to get this piece of information, or how to call it. I'd be doing something like:
$json=json_decode($data,true);
foreach ($json['dont-know-what-to-call-it'] as $key => $value) {
echo "Key: ".$key."; Value: ".$value."<br />";
}
I just don't know how to call each one of those [2010091901] blocks, like what name I'm suppose to call them as. I know how to call the stuff under score, since it's named "score", and the data is under all that. I don't know how to get the key/value of the initial "sections" of the array. In the end, I'm going to want to grab each [2010091901], manipulate/use the data that is inbetween each one of the [2010091901], and then go to the next "record".
$date_keys = array_keys($json) would give (0 => 2010091907, 1 => 2010091909, ...) . then you could do
foreach ($date_keys as $d) {
foreach ($json[$d] as $key => $value) {
...
Also, if you do not actually need the indices of the outer array (the date values - 2010091907, etc) than you could just do
foreach ($json as $j) {
foreach ($j as $key => $value) {
...
ignoring the keys of $json
Can't you just nest foreach()s?
foreach($jsondata as $somedate => $value){
//do you actually need $somedate?
foreach($value['home']['score'] as $score){
echo $score.PHP_EOL;
}
}
You can just do
$json = json_decode($data, true);
foreach($json as $ymd => $data)
{
// $ymd = [2010091907, 2010091909,… ]
// $data is the array starting with the key home. so $data['home']['score'][1] = 7 for the first iteration
}
This answer your question? It's not 100% clear what you're asking