How to divide array in parts with a token? - php

I have an array
foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info)
{
while($size = $sizes_info->children($k++))
{
$sizes_arr[] = $size->plaintext;
}
$sizes_arr[] = '_';
$k=0;
}
it looks like:
array(412) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L" [4]=> string(1) "_" [5]=> string(2) "43" [6]=> string(2) "44".....and so on
I need to divide this array in arrays with by a sign "_"... in other words I need to get this:
array(4) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L"}
array(2) { [0]=> string(2) "43" [1]=> string(1) "44"}
thanks in advance!

Why you're forming your array in such way if you need it in another way? Simply do:
foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info) {
$sizes_tmp = []; //or $sizes_tmp = array(); for PHP<=5.3
while ($size = $sizes_info->children($k++)) {
$sizes_tmp[] = $size->plaintext;
}
$sizes_arr[] = $sizes_tmp;
$k=0;//not sure what is it. leaving it as it is
}

Related

how to split one array in to multiple arrays based on clientID in php

I am trying to split the array and create for each clienID one new array. I have not yet manage how to split the array to two new arrays (example has two clientIDs). All rows with the same clientID should be in one array:
We can see that I have two clientIDs '51df' and '51ff'
File with raw data:
clientID,Timestamp, Room temp., Pressure, Humidity, PM1.0, PM2.5
51df,05/04/2021 11:20:42,22.28,988.61,37.97,0,0
51ff,05/04/2021 11:20:53,22.29,988.57,37.95,0,0
51df,05/04/2021 11:21:05,22.29,988.55,37.94,0,0
51ff,05/04/2021 11:21:16,22.29,988.63,37.95,0,0
51df,05/04/2021 11:21:27,22.28,988.57,37.97,1,1
51ff,05/04/2021 11:21:38,22.30,988.61,37.95,1,1
51df,05/04/2021 11:21:50,22.30,988.74,37.94,0,0
...
array(24) { [0]=> array(7)
{ [0]=> string(2) "ID" [1]=> string(9) "Timestamp" [2]=> string(16) "Room temperature" [3]=> string(8) "Pressure" [4]=> string(8) "Humidity" [5]=> string(5) "PM2.5" [6]=> string(5) "PM1.0" } [1]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:20:42" [2]=> string(5) "22.28" [3]=> string(6) "988.61" [4]=> string(5) "37.97" [5]=> string(1) "0" [6]=> string(1) "0" } [2]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:20:53" [2]=> string(5) "22.29" [3]=> string(6) "988.57" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [3]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:21:05" [2]=> string(5) "22.29" [3]=> string(6) "988.55" [4]=> string(5) "37.94" [5]=> string(1) "0" [6]=> string(1) "0" } [4]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:21:16" [2]=> string(5) "22.29" [3]=> string(6) "988.63" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [5]=>
...
What I have until now. I am reading all the file into an array and do the transition to split each row at ',', which gives me the above printout:
<?php
$latestFile = '/client/logs/logging_multipleClients.out';
$tmp = tail($latestFile, $lines = 400, $buffer = 4096);
$tmp1 = preg_split( "/\n/", $tmp );
$data = array();
$table = 'ID,Timestamp,Room temperature,Pressure,Humidity,PM2.5,PM1.0';
$tmp2 = preg_split( "/\,/", $table );
array_push($data, $tmp2);
for ($j=1; $j < count($tmp1); $j++) {
$tmp2 = preg_split( "/\,/", $tmp1[$j] );
array_push($data, $tmp2);
}
var_dump($data);
?>
How do I now split this $data array into two arrays according to the clientIDs?
So, at the end I would like to have:
client['0'] with all the row with clientIDs '51df'
client['1'] with all the row with clientIDs '51ff'
After I manage that it should be rather easy to split the $data array into as many clientIDs the file could have.
Would it be easier if I would read the file line by line, something like this:
if (($handle = fopen($latestFile, "r")) !== FALSE) {
$data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// do something here to push each line to the correspondent client array according to the clientID.
}
}
Thanks

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!

grouping array values like mysqli 'like' functionality

friends, i don't it's possible or not, anyway i want to group users who have same like preference names ( mysql 'like' functionality)
[0]=>
array(5) {
["id"]=>"21"
["user_id"]=>"58"
["preference_id"]=>"4"
["sub_preference_id"]=>"7"
["preference_name"]=>"stephens collage"
}
[1]=>
array(5) {
["id"]=>"22"
["user_id"]=>"52"
["preference_id"]=>"4"
["sub_preference_id"]=>"1"
["preference_name"]=>"st stephens"
}
[2]=>
array(5) {
["id"]=>"25"
["user_id"]=>"61"
["preference_id"]=>"4"
["sub_preference_id"]=>"9"
["preference_name"]=>"joseph"
}
means i want output as like this :
1]stephens collage, st stephens
2] joseph
i tried this code :
$school_preference ="SELECT * FROM gic_user_preference where preference_id='4'";
$result_school_preference = mysqli_query($createCon->connect(), $school_preference);
$school = array();
while ($show_school_preference = mysqli_fetch_assoc($result_school_preference)) {
$school_preference = $show_school_preference['sub_preference_id'];
$get_all_school_preference =mysqli_query($createCon->connect(),"SELECT gic_user_preference.id,gic_user_preference.user_id,gic_user_preference.preference_id,gic_user_preference.sub_preference_id,gic_user_wise_school_preference.preference_name FROM gic_user_preference INNER JOIN gic_user_wise_school_preference ON gic_user_preference.sub_preference_id = gic_user_wise_school_preference.id where gic_user_preference.sub_preference_id='$school_preference'");
while ($schools = mysqli_fetch_assoc($get_all_school_preference)) {
$school[$schools['preference_name']][] = $schools;
}
}
it's give the output
array(4) {
["stephens collage"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "21"
["user_id"]=>
string(2) "58"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "7"
["preference_name"]=>
string(16) "stephens collage"
}
}
["st stephens"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "22"
["user_id"]=>
string(2) "52"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "1"
["preference_name"]=>
string(11) "st stephens"
}
}
["joseph"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "25"
["user_id"]=>
string(2) "61"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "9"
["preference_name"]=>
string(6) "joseph"
}
}
}
any idea how to grouping this data as mysqli like function works ...
I think you can use regular expression match for find same names!

Avoid duplicate element in foreach

I would like the code below to display only one time each $row[2] element (no duplicates) :
foreach($rows as $row){
echo " {$row[2]} ";
}
How can I achieve this ? Thanks.
My array is very big but here is a sample from var_dump
[0]=>
array(10) {
[0]=> string(2) "39"
["id"]=> string(2) "39"
[1]=> string(3) "abc"
["A"]=> string(3) "abc"
[2]=> string(2) "123"
["B"]=> string(2) "123"
[3]=> string(1) "0"
["C"]=> string(1) "0"
[4]=> string(1) "1"
["D"]=> string(1) "1"
}
I'm only interested about [2]=> string(2) "123".
Here is the code you can use:
$uniqueArr = array();
foreach ($rows as $row) {
if(!(in_array($row[2], $uniqueArr))) {
echo $row[2];
$uniqueArr[] = $row[2];
}
}

PHP Arrays gatther arrays from previous post

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();

Categories