I have the array below and I can't seem to figure out how to pull out the information from the array Team_1 and echo out the 0 - 5 values inside it.
array (size=3)
'Team_1' =>
array (size=5)
0 => string '1199' (length=4)
1 => string '1182' (length=4)
2 => string '1105' (length=4)
3 => string '1212' (length=4)
4 => string '891' (length=3)
'Team_2' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'Team_3' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
The key is Team_1 so you can reference it directly and do a foreach loop to do the echo (you can add any html you want to echo to format the values).
foreach ($arr['Team_1'] as $val) {
echo $val.'<br>';
}
If your intention is to loop through all teams and echo values
foreach ($teams as $team => $vals) {
echo $team;
foreach ($vals as $val) {
echo $val;
}
}
$output1 = $array['Team_1'][0]; // Should output 1199
echo $output1;
OR
foreach ($array['Team_1'] as $data){
echo $data.'<br/>';
}
I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)
Right now i'm trying to validate some postdata with filter_var(). I want to get the filter related to each input from my database. So if the input should be filtered by EMAIL, the variable would contain FILTER_VALIDATE_EMAIL. This would then be passed like so:
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, $requirements[$key][1]) === false){
$errors = true;
}
}
}
The $postdata looks like this:
array (size=4)
'personer_navn' =>
array (size=1)
0 => int 0
'personer_alder' =>
array (size=1)
0 => int 1
'personer_kon' =>
array (size=2)
0 => int 2
1 => int 3
'personer_by' =>
array (size=1)
0 => int 4
And the $requirements looks like this:
array (size=4)
'personer_navn' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_alder' =>
array (size=4)
0 => string 'int' (length=3)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_kon' =>
array (size=4)
0 => string 'allowed' (length=7)
1 => string 'allowed' (length=7)
2 => string 'allowed' (length=7)
3 => string 'allowed' (length=7)
'personer_by' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
Again the problem seems to ba passing $requirements[$key][1] to the filter_var() function.
Any help is appreciated.
FILTER_VALIDATE_EMAIL is not to be used as a string. Try using it without the quotes. Examples can be found in the documentation
A simple change that will fix the bug
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, constant( $requirements[$key][1]) ) === false){
$errors = true;
}
}
}
The constant function returns the (integer) value of the filter that is string.
read P.P-s answer too.
if(( (in_array(0,$status_arr)) || (in_array(0,$genstatus_arr)) ) &&
((!in_array(0,$escalation_arr)) || (!in_array(0,$genescalation_arr))) ){
echo 'Something';
}else if(( (in_array(1,$status_arr)) || (in_array(1,$genstatus_arr)) )){
echo 'Something Else';
}
Here I am comparing 0 value in_array and non-zero which is !in_array. The results varies according to if-else condition but does not output expected result.
To avoid this, I tried with the third parameter, true, placing the comparison in strict mode which will not only compare values, but types as well:
How can I check if 0 or 1 exists in some array and do not exist in another array ?
The array values would be like below:
var_dump($status_arr);
array (size=6)
0 => string '0' (length=1)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
var_dump($genstatus_arr);
array (size=6)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
4 => string '1' (length=1)
For the below condition its not working
if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) )
Also its not a possible duplicate of IN ARRAY
var_dump(in_array(0, $status_arr, true));
Should return true, nothing abnormal there. If you are seeing false it is most probably because either of your 0's is a string 0 and not numeric 0.
If that is the case, remove the strict parameter
$array=array(0,2,3);
var_dump(in_array(0,$array,true));
Returns
bool(true)
And
$array=array('0','2','3');
var_dump(in_array(0,$array,true));
Returns
bool(false)
You mentioned
For the below condition its not working
print_r($status_arr);
Array([0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0)
print_r($genstatus_arr);
Array([0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1)
if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) )
Which is an incorrect observation, because the condition works perfectly fine.
Example
In your case your are trying to work with strings
var_dump($status_arr);
array (size=6)
0 => string '0' (length=1)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
var_dump($genstatus_arr);
array (size=6)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
4 => string '1' (length=1)
Just try this example:
<?php
$status_arr = array();
$status_arr = array_pad($status_arr, 6, 0);
$genstatus_arr = array();
$genstatus_arr = array_pad($genstatus_arr, 6, 1);
var_dump($status_arr);
var_dump($genstatus_arr);
if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) ) {
echo 'Works as espected';
}
I have an array in loop while like this when I var_dump it.
array (size=73)
0 => string '1' (length=1)
'address' => string '1' (length=1)
1 => string '2' (length=2)
'street_no' => string '2' (length=1)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
I want disply data on screen for first data is
`AddressMain: 1
streetMain: 1
.....
AddressLeft: vbfgh
street1Left: fgfd
.....
AddressRight: vbfgh
streetRight: fgfd
.....
AddressCenter: vbfgh
streetCenter: fgfd
.....`
How I can change my label like this if this code in loop while ?
And this is my code
while( $row = pg_fetch_array($result)){
echo "AddressMain:".$row['address'];
echo "streetMain:".$row['street_no'];
echo "<br/>";
echo ".......";
}
Plz help me how I can change my label in this loop ?
thanks
Use an array to define labels, then loop around it.
$labels = array('Main','Left','Right','Center');
$i = 0;
while( $row = pg_fetch_array($result)){
echo "Address".$labels[$i].":".$row['address'];
echo "street".$labels[$i].":".$row['street'];
echo "<br/>";
$i++;
}