PHP Arrays gatther arrays from previous post - php

Using WP I'm building the breakdown of a score. Each breakdown is a custom select field with values from 1 to 10. The final result i'm trying to achieve is to show the total score inside the WP loop, by calculating the sum of all breakdown scores and dividing it by the number of added breakdown scores.
The problem is that each post i add with the total score it taking the array from a previous posts also. For example:
Post 1
array(6) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" }
Post 2
array(9) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" }
Post 3
array(13) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" [9]=> array(0) { } [10]=> string(1) "9" [11]=> string(2) "10" [12]=> string(1) "3" }
As you can see Post 2 got it's own array and the array from Post 1, Post 3 got it's own array and arrays from Post 1 and Post 2.
This is the code i'm using inside the WP while loop
<?php
$score_rows = get_post_meta($post->ID, 'review_score', true);
$score[] = array();
foreach( $score_rows as $row ):
$score[] = $row['score_number'];
endforeach;
$score_items = count( $score );
$score_divide = $score_items;
$score_sum = array_sum( $score );
$score_total = $score_sum / $score_divide;
?>
This code works great in single.php where i have only one post, but what to do where i have more then one? How to prevent from posts to combine arrays like this?
UPDATE
I have found that all the values are strings, maybe this is what causing it act like this. Is it possible to convert the values to integers?

change
$score[] = array();
to
$score = array();

Related

Creating an array reading from different files in PHP

I'm trying the following,
I have different files (8 files) that stores values like this:
1 2 3 4 5 6
7 8 9 10 11 12
....................
I would like to read 8 files at the same time and create an array that will store the first value of each file, another array with the second element, and so on.
For example, if first element of file1 is 1, in file2 8, ..., in file8 23; the resulting array in this iteration would be:
first_array = [1, 8, ....., 23]
I was making some tests on reading files in PHP like this:
$myfile = fopen("textoPrueba.txt", "r",
"/home/berni/Documentos/Vesta1");
// Output one character until end-of-file
while(!feof($myfile))
{
echo fgetc($myfile);
}
fclose($myfile);
This code just show me the elements of a file, but I would like to take specific elements in an iteration.
Someone can give me a hint? Thanks in advance
(NOTE: files have more than a million of elements)
Another option is that, after we would file_get_content or read our files, we would use a simple expression and collect our numbers using a preg_match_all:
$re = '/([0-9]+)/m';
$str = '1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
1 2 3 4 5 6
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $key => $numbers) {
foreach ($numbers as $key2 => $number) {
echo $number . "\n";
}
}
var_dump($matches);
array(24) {
[0]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[1]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[2]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[3]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[4]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[5]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[6]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "7"
}
[7]=>
array(2) {
[0]=>
string(1) "8"
[1]=>
string(1) "8"
}
[8]=>
array(2) {
[0]=>
string(1) "9"
[1]=>
string(1) "9"
}
[9]=>
array(2) {
[0]=>
string(2) "10"
[1]=>
string(2) "10"
}
[10]=>
array(2) {
[0]=>
string(2) "11"
[1]=>
string(2) "11"
}
[11]=>
array(2) {
[0]=>
string(2) "12"
[1]=>
string(2) "12"
}
[12]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[13]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[14]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[15]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[16]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[17]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[18]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[19]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[20]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[21]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[22]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[23]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
}
Demo
Making the following assumptions:
each file has a string of numbers separated by spaces
numbers are not bigger than a billion
code to extract the first element of each file, starting with the code you've provided:
$myfile = fopen("textoPrueba.txt", "r", "/home/berni/Documentos/Vesta1");
// get the first 10 elements one at a time
$str = array();
for($i=0; $i<10; $i++) {
// get the first 10 elements one at a time
$str[] = fgetc($myfile);
}
fclose($myfile);
// squish them into a single string
$temp = join($str);
// explode it into an array separated by spaces
$split = explode(' ', $temp);
// get the first number
$first_element_of_file = $split[0];
For learning purposes, this will do what you asked. I make no claims about it being the best way to approach it!

passing array into view (codeigniter )

array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);

Count values in specific position in multidimensional array

I have a multidimensional array that looks like this:
array(6) {
[0]=>
array(6) {
[0]=>
string(5) "email"
[1]=>
string(5) "vote1"
[2]=>
string(5) "vote2"
[3]=>
string(5) "vote3"
[4]=>
string(5) "vote4"
[5]=>
string(5) "vote5"
}
[1]=>
array(6) {
[0]=>
string(5) "a#a.a"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[2]=>
array(6) {
[0]=>
string(5) "b#b.b"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[3]=>
array(6) {
[0]=>
string(5) "c#c.c"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[4]=>
array(6) {
[0]=>
string(5) "d#d.d"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[5]=>
array(6) {
[0]=>
string(5) "e#e.e"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
}
I want to count how many times each value occurs at position [1] of each array.
I have tried many things including array_count_values(), but this doesn't allow the option to count occurrences in just a certain position of each array. I'm not quite sure how to go about this issue because the array is multidimensional and I have never had to deal with just counting the values at a specific position.
I apologize if the answer to this is blatantly obvious, I've been working on this issue for a few hours but to no avail, but I also am a beginner when it comes to arrays, especially multidimensional. All ideas are welcome :)
What you can do is, you can loop through the array and put the values of [1] into another array, and apply array_count_values on the resultant array.
$a = array();
foreach ($arr as $ar) {
$a[] = $ar[1];
}
print_r(array_count_values($a));
This will give you:
vote1: 1
A: 5
If the above is what you are looking for? If you want a shorter version, you can also use array_column.
use array_column($array, 1) to get position[1] elements as array; Then use your array_count_values() to count them.
<?php
$array = [[1,2,3],[2,3,4]];
var_dump(array_count_values ( array_column($array,1) ));

PHP array_combine() function not working properly?

I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle

Getting Unique Values in Multidimensional Array PHP

I have an array similar to this:
array(5) { [0]=> string(2) "32" [1]=> string(2) "67" [2]=> string(19) "2013-07-15 15:56:28" [3]=> string(1) "1" [4]=> string(4) "Fail"}
array(5) { [0]=> string(2) "32" [1]=> string(2) "89" [2]=> string(19) "2013-09-15 13:50:34" [3]=> string(1) "2" [4]=> string(4) "Pass"}
array(5) { [0]=> string(2) "37" [1]=> string(2) "55" [2]=> string(19) "2013-07-15 16:36:12" [3]=> string(1) "1" [4]=> string(4) "Fail"}
array(5) { [0]=> string(2) "39" [1]=> string(2) "92" [2]=> string(19) "2013-08-15 15:46:20" [3]=> string(1) "1" [4]=> string(4) "Pass"}
The first value displays a content ID, the second a score, the third a date, the fourth an attempt number (no limit), and the fifth a result. I want to print these values in the browser, but if there is more than one attempt, I only want to print the one with the highest score. I can't seem to get it working, any help would be greatly appreciated.
The way you can achieve this is:
1st: Group the results by content ID.
2nd: Descending Order the sub-array by score using usort
3rd: Display the 1st sub-array to user for each content ID.

Categories