Dynamic Array, taking data from select - php

I have an array that has this structure:
array (size=6)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
I have values from a selection that is Dorado->32GB->Madera and I need find this value in the array; if its true that it exists, then take the other values like a vlr, pcost, etc.
What is the best method to find that, remembering that the values with number are dynamic, sometimes with 1 or 5 example:
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
0 => string 'Azul' (length=6)
1 => string '32GB' (length=4)
Can somebody explain how I could do this?

I suppose, this is a code to start with:
$searchValue1 = 'Dorado';
$searchValue2 = '32GB';
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if ($item[0] === $searchValue1 && $item[1] === $searchValue2 && $item[2] === $searchValue3) {
print_r($item);
}
}
A bit sophisticated version with checking whether $searchValue_ is not empty is:
$searchValue1 = 'Dorado';
$searchValue2 = null;
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if (
(!empty($searchValue1) && $item[0] === $searchValue1)
&&
(!empty($searchValue2) && $item[1] === $searchValue2)
&&
(!empty($searchValue3) && $item[2] === $searchValue3)
) {
print_r($item);
}
}
update: with unknown number of search parameters you can:
$searchValues = ['Dorado', 'Plastico'];
foreach ($yourArray as $item) {
// find intersection of two arrays
$intersect = array_intersect($item, $searchValues);
// if size of intersection is same as size of `$searchValues`
// then you can be sure that all elements of `$searchValues` are in `$item`
if (count($intersect) === count($searchValues)) {
print_r($item);
}
}

Related

Pull array out from an array

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/>';
}

Split Array at Repeating Object in Array

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

Give filter_var() filter by passing a variable PHP (filter_var() expects parameter 2 to be long, string given)

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.

Php - in_array() and ! in_array() check 0 value

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';
}

php how i can change my label in loop

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++;
}

Categories