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);
}
}
I have this json string as below:
$json = '[{"sessionNo":"1","sessionData":["4","6"]},{"sessionNo":"2","sessionData":["2"]},{"sessionNo":"3"}]';
I want to "translate" it as a PHP array. I tried to do the following but it returns an empty array :
var_dump(json_decode($json))
**UPDATE**
Now I am getting this:
array (size=3)
0 =>
array (size=2)
'sessionNo' => string '1' (length=1)
'sessionData' =>
array (size=2)
0 => string '4' (length=1)
1 => string '6' (length=1)
1 =>
array (size=2)
'sessionNo' => string '2' (length=1)
'sessionData' =>
array (size=1)
0 => string '2' (length=1)
2 =>
array (size=1)
'sessionNo' => string '3' (length=1)
I want to loop through this array so I get for each sessionNo the corresponding SessionData, smth like:
sessionNo SessionData
1 4
1 6
2 2
You can use something like...
$json = '[{"sessionNo":"1","sessionData":["4","6"]},{"sessionNo":"2","sessionData":["2"]},{"sessionNo":"3"}]';
$array = json_decode($json, true);
foreach ( $array as $session ) {
if ( isset ($session['sessionData'])){
foreach ( $session['sessionData'] as $data ) {
echo $session['sessionNo']."-".$data.PHP_EOL;
}
}
}
This is just converting the data and then loop over the arrays in a foreach(), only doing the inner one if there is any sessionData.
This outputs..
1-4
1-6
2-2
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.
I have a client calling my web service written in PHP. While calling it, the client application gives a little query string to it. I want to parse this string into an array. So with the given example query within an url, I have started doing this:
Url: $select=substringof("customer", tolower(toupper(bla))),test$filter=Name%20eq%20'test'%20and%20toupper(Name)$skip=10$top=2$orderby=day(time)%20desc
.
<?php
function parseOdataRequest($request)
{
$expression = array();
// Fetch parameters
$params = explode('$', $request);
// Leave empty parameter name alone
unset($params[0]);
$params = array_values($params);
// Check every parameter
for($i = 0; $i < count($params); $i++)
{
// every parameter has to be a name=value pair!
$param = explode('=', $params[$i]);
if(!(count($param) == 2))
throw new Exception("Invalid parameter. Providence must be name=value");
// Validate parameter name
if(in_array($param[0], $this->validParams))
{
$parseParam = 'parse' . ucfirst(strtolower($param[0]));
$expression[$param[0]] = $this->$parseParam($param[1]);
}
else
throw new Exception("Unkown parameter '" . $param[0] . "'");
}
var_dump($expression);
}
?>
As a result, I have the following $expression:
array (size=5)
'select' => null
'filter' => null
'skip' => null
'top' => null
'orderby' => null
What I want is something like the following:
array (size=5)
'select' =>
array (size=2)
'substringof' =>
array (size=2)
0 => string 'customer' (length=8)
'tolower' =>
array (size=1)
'toupper' =>
array (size=1)
0 => string 'bla' (length=3)
0 => string 'test' (length=4)
'filter' =>
array (size=3)
0 =>
array (size=3)
0 => string 'Name' (length=4)
1 => string 'eq' (length=2)
2 => string ''test'' (length=6)
1 => string 'and' (length=3)
2 =>
array (size=3)
'toupper' =>
array (size=1)
0 => string 'Name' (length=4)
0 => string 'eq' (length=2)
1 => string ''TEST'' (length=6)
'skip' => int 10
'top' => int 2
'order' =>
array (size=1)
0 =>
array (size=2)
'day' =>
array (size=1)
0 => string 'time' (length=4)
0 => string 'desc' (length=4)
I tried different things, but never was successful. I would appreciate something like an approach to solve this problem.