Get value of multidimensional array in array - php

I have below values in formsubmit,
items[0][item_name]: Test
items[0][item_description]: testing1
items[0][item_price]: 11
items[0][item_group]: 18
items[0][item_code]: 11
items[0][item_is_active][]: 1
items[1][item_name]: test2
items[1][item_description]: test
items[1][item_price]: 12
items[1][item_group]: 18
items[1][item_code]: 12
items[1][item_is_active][]: 1
and here is serverside DB entry in laravel,
$items_data = $request->items;
foreach ($items_data as $i) {
$items = new Menuitems; //Must be reinit to reiterate all entries
$items->item_name = $i['item_name'];
$items->item_description = $i['item_description'];
$items->item_price = $i['item_price'];
$items->item_group = $i['item_group'];
$items->item_code = $i['item_code'];
$items->item_is_active = $i['item_is_active'];
$items->save();
}
It works fine all entries except for item_is_active because it has one more [ ] in form input,
How can I add item_is_active in foreach loop? should it have one more foreach loop inside?
Below is formsubmit values when some checkboxes are not selected,
items[0][item_name]: Capachno
items[0][item_description]: best
items[0][item_price]: 12
items[0][item_group]: 17
items[0][item_code]: 11
items[0][item_is_active][]: 1
items[1][item_name]: Lambc
items[1][item_description]: sdf
items[1][item_price]: 34
items[1][item_group]: 17
items[1][item_code]: 12
items[1][item_is_active][]: 1
items[2][item_name]: tasto
items[2][item_description]: ewr
items[2][item_price]: 12
items[2][item_group]: 17
items[2][item_code]: 13
items[3][item_name]: hingo
items[3][item_description]: 123
items[3][item_price]: 123
items[3][item_group]: 17
items[3][item_code]: 32

Related

Php recursion of a tree of comments, with global variable

<?php
tree($Comments, 0, 0);
$var = -1;
function tree($Comments, $parent_id = 0, $level=0) {
global $var;
foreach($Comments as $Comment){
if($Comment['parent_id'] == $parent_id) {
If ($level > $var) {$var++;} else {echo ($var-$level+1); $var=$level;};
for ($i=$level; $i>0; $i--)
echo "-";
echo $Comment['text']."<br>";
tree($Comments, $Comment['id'], $level+1);
}
}
}
?>
This is my recursive method of displaying the tree of comments(thread comments),
the problem is I have to make, in that (if ($level > $var) statement get variable $var of how many (</div> for a bootstrap design) I should use for making the design for the comment threads. The problem is that I get first result 1, which supposedly I shouldn't, the rest gets as It should have to.
Edit 1, Example of data in database(to easy understand):
id id_theme parent_id user text
16 14 0 DomainFlag This is 1!
17 14 16 DomainFlag This is 2!
18 14 16 DomainFlag This is 3!
20 14 17 DomainFlag This is 4!
21 14 17 DomainFlag This is 5!
22 14 21 DomainFlag This is 7!
23 14 22 DomainFlag This is 8!
24 14 18 DomainFlag This is 6!
25 14 0 DomainFlag This is 9!
26 14 25 DomainFlag This is 10!
27 14 25 DomainFlag This is 11!
My table:
id
parent_id
id_theme
user
text
upVotes
downVotes
My idea is to make a threaded system of comments like(with $level which I get from the recursive function):
0
1
2
3
2
1
1
0
1
0
So I get this result with that 1 at first no matter what, which I shouldn't( 1 This is 1!). The rest is working perfectly.
1This is 1!
-This is 2!
--This is 4!
1--This is 5!
---This is 7!
----This is 8!
4-This is 3!
--This is 6!
3This is 9!
-This is 10!
1-This is 11!
Why that 1 at the beginning appears? I heard that global scopes isn't good to implement, why? and what I should do to not use it?

Multiple comparison and edit file

Here is my $file1 structure (there are thousands of like that groups):
Group id_7653
{
type register
sub_name 155
1 3123 1 12
2 3124 1 8
3 3125 1 4
4 3126 1 12
5 3127 1 8
6 3128 1 4
.....
}
Group id_8731
{
type register
sub_name 155
1 4331 1 12
2 4332 1 8
3 4333 1 4
4 4334 1 12
5 4335 1 8
6 4336 1 4
.....
}
And here is my $file2 structure (again, there are thousands of defined values)
.....
3123 Spada+1
3124 Spada+2
3125 Spada+3
3126 Spada+4
3127 Spada+5
3128 Spada+6
3129 Spada+7
3130 Spada+8
.....
And here is my Worker script that makes, compares $file1 and $file2.
<?php
//read the first file in as a string
$file1 = file_get_contents("dataparser\names1.txt");
//read the second file in as an array
$file2 = file("dataparser\names2.txt");
//index from file2 that we are going to build
$file2Index = array();
foreach($file2 as $line){
//split the line
$line = explode("\t", $line, 2);
//validate the line, should be only 2 values after explode and first should be a number
if(count($line) == 2 && is_numeric($line[0])){
//add to index
$file2Index[$line[0]] = $line[1];
}
}
//now get all the values from file1 that we want (second column)
preg_match_all('/^\s*\d+\s*(\d+)\s*\d+\s*\d+\s*$/m', $file1, $matches);
$file1Values = array_unique($matches[1]);
//loop over the matches from column 2
foreach($file1Values as $value){
//check if the key doesn't exist
if(!isset($file2Index[$value])){
//echo error message
echo "Value {$value} does not exist in file2<br>";
}
}
?>
What makes that script:
Compares $file1 and $file2 and shows me which values are not defined in $file2
So far, everything works okay.
I want to extend that my script a little bit, so I want to replace that {$value} with my $file2 structure.
This time, I don't want to check that value, I want replace it directly from $file1 value. (Spada etc...)
Which paths I should follow...? Can I get some examples please...

Extract data from a file PHP

I have this array $awstat that I extacted from an awstat file, an withing $awstat I have this that I need:
BEGIN_TIME 24
0 3245 9955 143463426 8047 13601 475741423
1 3122 9131 146244440 7579 12936 507921700
2 2639 5706 95369716 7351 11987 490330698
3 1917 4062 79234871 8245 13009 579453498
4 1757 4263 65580607 7887 11437 454870321
5 1723 4022 44682383 6888 10263 326819624
6 1876 4677 56964771 7339 11242 355385677
7 2796 8473 120152521 7770 12176 362904239
8 4227 13791 196173677 7421 12196 366706352
9 7984 25965 375376297 8398 13883 406545549
10 14605 34418 434054375 7183 13341 380773129
11 15533 41259 559938996 7123 12690 372426426
12 17495 40043 505139834 7432 13402 518541077
13 15815 34170 385108531 6519 12390 396494926
14 16330 41073 508838859 6761 12318 348417806
15 19093 44058 483568307 7692 13583 454365520
16 30429 59672 577852398 8273 13231 473134295
17 25094 48897 478246556 8207 12898 476038603
18 19136 42665 482073005 8087 12983 468300958
19 28849 46228 371229572 7721 12688 471632281
20 14068 30981 341103557 7832 13251 417443822
21 14727 33458 394841797 7575 12644 388811384
22 13480 31364 365096742 7460 13114 411771572
23 7189 19744 272606100 6643 12398 397762547
END_TIME
So I tried this and it doesn't seem to work!
preg_match("/BEGIN_TIME(.*)END_TIME/is", $awstats, $matches);
$time = $matches[0] ;
var_dump($time); // it displays "NULL"
Any solution for this? Thanks!
Not so much an answer, more of an opinion.
Using regular expressions here is overkill.
Something as simple as this will do:
$lines = file("awstats.output");
$lines = array_slice(1); // remove first line
$lines = array_slice(0, -1); // remove last line
foreach ($lines as $line) {
$data = explode(" ", $line);
// handle data
}
You can match linebreaks with:
([^\n]*\n+)+
So this should work:
preg_match("/BEGIN_DAY([^\n]*\n+)+END_DAY/is", $awstats, $matches);
$time = file_get_contents('awstat.log');
$time = preg_replace('/BEGIN_TIME(.*?)END_TIME/sim', '$1', $time);

How to arrange item data from MySQL database like given my example

how to arrange below list any way in php,
Actually we will get item array list from MySQL Database(Select Output).
For example,
If We will get Array list like below
$item_arr = array("102","101","103","103","101","101","102","103","102");
The We have to arrange like below,
101 102 103 101 102 103 101 102 103
Again If Array list like below,
$item_arr = array("102","103","102","101","103","101","103","103","101");
The We have to arrange like below,
101 102 103 101 103 103 101 102 103
Again If Array list like below,
$item_arr = array("103","101","102","103","102","102","102","103","102");
The We have to arrange like below,
101 102 103 102 102 103 102 102 103
$rtr_item_arr = arrange_item($item_arr);
function arrange_item($results)
{
$last_val="";
$unique_only = array();
$additional = array();
foreach($results as $val):
if(in_array($val, $unique_only)):
$additional[] = $val;
endif;
if($val != $last_val && !in_array($val,$unique_only)):
echo $val." ";
$unique_only[] = $val;
endif;
$last_val = $val;
endforeach;
$additional = array_filter($additional);
if(!empty($additional)):
arrange_item($additional);
endif;
}
How to arrange it to keep similarity On the Other hand, how to arrange nicely.
Please any suggestion?
It looks like you are repeating the pattern of 101, 102, 103 no matter what order it comes out of the database.
I don't see your SQL posted here, but you could group by the 101, 102, 103 field and count the number of instances of each then order by the 101,102,103 field and create an associative array:
$item_arr = array(101=>count of 101,102=>count of 102,...)
Then in the PHP arrange them in another array using a loop from 0 to highest count. Each time through the loop add each key to the new array if the value is > 0 and subtract 1 from the value of each of the key value pairs.

proper arrangement in foreach

$random = rand(4, 23);
$range = range(1, $random );
HI.. guys
I have a random range value here in foreach function i want to display with below
rules.. my aim is to display like a square box
if i get range 1 to 3 it has to display table like this
1 2
3
if range from 1 to 6
1 2 3
4 5 6
if range from 1 to 19
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19
get the ceil of the square root of the number of records, and then anytime you're at an index with a mod of that value that is equal to 0, start a new line. Since you already have $random something like:
$dim = ceil(sqrt($random));
foreach ($range as $index => $number) {
print $number;
if (!(($index + 1) % $dim)) {
print "\n";
}
else {
print " ";
}
}
May need some adjustment (I'm not in PHP mode atm) and also doesn't factor in the padding but that should be straightforward.

Categories