Conditional where variable array is not empty, but has NULL values - php

I have the following problem in my php function:
$items is an array. It is never empty, But sometimes it has only has NULL values which returns an array value of [0]. How do I convert the conditional to say: If $items is not empty and if the $items array does not return a value of [0], then do this. . . ?
Thanks.
$items = Items_Model_Item::findBy(array('where' => new JO_Db_Expr('item.id IN (SELECT id FROM dbtable WHERE challengeId LIKE ' . Helper_Db::quote($challengeIDPLUS['code']) . ')')));
if (!empty($items)) {
foo
} else {
bar
}

Related

Remove empty arrays from PHP variable

I am using PDO in order to select some values from my database. For each iteration of my $teachArray, I store the selected value into my $language_id variable. However, I am also storing some empty arrays which is not my intention.
I would like to know if it is possible to exclude or just get rid off empty arrays inside my php variable.
Here is my simple query.
$sqlFindId = "SELECT language_id
FROM language_skill
WHERE person_id = :person_id AND language_learning = :language_learning AND language_id = :language_id";
foreach ($teachArray as $dataTeach)
{
$query = $handler->prepare($sqlFindUser);
$query->bindValue(':person_id', $_SESSION['person_id']);
$query->bindValue(':language_learning', 1);
$query->bindValue(':language_id', $dataTeach);
$query->execute();
$language_id = $query->fetchAll(PDO::FETCH_ASSOC);
print_r($language_id);
}
I use print_r on my query in order to see my variable contents.
Here are the results of my print_r. I want to get rid of the first and last arrays which are empty. It can be noted that these empty arrays can appear anywhere in my print_r.
Array ( ) Array ( [0] => Array ( [language_id] => 13 ) ) Array ( )
I have tried using
$array= array_filter(array_map('array_filter', $language_id));
but it gives the same result
foreach ($teachArray as $dataTeach)
{
$query = $handler->prepare($sqlFindUser);
$query->bindValue(':person_id', $_SESSION['person_id']);
$query->bindValue(':language_learning', 1);
$query->bindValue(':language_id', $dataTeach);
$query->execute();
$language_id = $query->fetchAll(PDO::FETCH_ASSOC);
if ( count( $language_id ) != 0 ) {
print_r($language_id);
}
}
Hope it can helps :)

Adding arrays to a php variable

I am using PDO in order to select some values from my database.
For each iteration of my $teachArray, I store the selected array into my $language_id variable. I then get rid of any empty array that I have inside my $language_id and assigning it to my $NoEmptyArray_language_id However, I am only storing the last array inside of $language_id in $NoEmptyArray_language_id.
I would like to concatenate every array in $language_id into $NoEmptyArray_language_id.
I cannot use $NoEmptyArray_language_id .= $language_id; since it will give me an error: Array to String conversion
Here is my simple query.
$sqlFindId = "SELECT language_id
FROM language_skill
WHERE person_id = :person_id AND language_learning = :language_learning AND language_id = :language_id";
$NoEmptyArray_language_id = "";
foreach ($teachArray as $dataTeach)
{
$query = $handler->prepare($sqlFindUser);
$query->bindValue(':person_id', $_SESSION['person_id']);
$query->bindValue(':language_learning', 1);
$query->bindValue(':language_id', $dataTeach);
$query->execute();
$language_id = $query->fetchAll(PDO::FETCH_COLUMN, 0);
if ( count( $language_id ) != 0 ) {
$NoEmptyArray_language_id .= $language_id;
}
}
print_r($NoEmptyArray_language_id);
You have to define $NoEmptyArray_language_id = array();
Also change code with $NoEmptyArray_language_id[] = $language_id;
Now you have array with values in $NoEmptyArray_language_id.
If you want to get string convert this array to string using implode function.

PHP Array within array issue

I have an array like the one given below:
{
"quantity":"1",
"product_id":"41",
"option[232]":"28",
"option[231][]":"25"
}
I run the above in a foreach loop as $key => $value. But when I try to concatenate them into a string, I get an Array to String conversion error.
Eg: $result = $this->db->query("SELECT a.quantity quantity, b.name optionname FROM " . DB_PREFIX ."product_option_value a, " . DB_PREFIX ."option_value_description b WHERE a.option_value_id = b.option_value_id AND a.product_option_id=".$key." AND a.product_option_value_id=".$value." AND a.product_id=".$product_info['product_id']." AND a.subtract=1");
When I remove the last entry of "option[231][]":"25", it works fine. Is there anyway I can convert the "option[231][]" to "option[231]".
I am posting to a php page via json.
Thanks
Just use the print_r function to convert the multi-dim array, e.g.
option[231] = print_r(option[231], TRUE);
or, if you don't like the resulting syntax, nest a foreach loop within the existing foreach loop which executes when the variable is an array, e.g.
if(is_array(option[$x])){
$y = '';
foreach(option[$x] as $value){
$y .= $value;
}
option[$x] = $y;
}

Imploding array within a foreach loop (PHP) to build a SQL query: invalid argument error

I'm trying to build a SQL query by looping in PHP using 2 arrays (one of which is array of arrays):
//build array of arrays using predefined arrays
$regions = array_filter(array($EAPRO, $WCARO, $ROSA, $TACRO, $MENA, $ESARO));
//just a normal array
$regionnames = array('EAPRO', 'WCARO', 'ROSA', 'TACRO', 'MENA', 'ESARO');
$sql = "";
foreach(array_combine($regions, $regionnames) as $region => $regionname)
{
$sql .="UPDATE `database`.`table` SET `region`='$regionname'
WHERE `countryname` IN (" . implode(",",$region) . ");";
}
echo $sql;
However, debugging this in ideone gives me:
Warning: implode(): Invalid arguments passed on line:
UPDATE `database`.`table` SET `region`='ESARO' WHERE `countryname` IN ();
Which tells me that the array on each loop is not being imploded correctly. Is there something wrong with the way I've defined my array of arrays?
Thanks
From the PHP Docs: array_combine ( array $keys , array $values )
So the problem is that the variables are in the wrong places array_combine($regions, $regionnames) (a key can never be an array).
So this should fix the problem:
//build array of arrays using predefined arrays
$regions = array_filter(array($EAPRO, $WCARO, $ROSA, $TACRO, $MENA, $ESARO));
//just a normal array
$regionnames = array('EAPRO', 'WCARO', 'ROSA', 'TACRO', 'MENA', 'ESARO');
$sql = "";
foreach(array_combine($regionnames, $regions) as $region => $regionname)
{
$sql .="UPDATE `database`.`table` SET `region`='$regionname'
WHERE `countryname` IN (" . implode(",",$region) . ");";
}
echo $sql;

Why my php array is over written when they have same Keys

I have a custom PHP function which executes a stored procedure and returns an array:
function test(){
$in = array("abc","bcd","efg");
$result = mydba->executestoredprocedure('proc1',$in);
$arr_sim = array();
foreach ($result['recordset'] as $rows) {
if (!empty($rows)) {
echo $arr_sim[$rows['field1']] = $rows['field2'];
}
}
return $arr_sim;
}
In the above function $arr_sim is returning the number of items correctly when the rows["field1"] values are different. If the rows["field1"] values are the same then it is overwriting the first value and returning only the last one. How can I overcome this?
array ( [chicago] => 'sears', [rochester] => 'liberty' )
If the $arr_sim contains these items then it is returned correctly. Because the keys are different.
array ( [chicago] => 'MCD', [chicago] => 'TACOBELL' )
If the $arr_sim contains these items then it is not returned correctly. Because the keys are the same, "chicago".
Array keys must be unique. Instead, do something like this:
// You want the array to look like this
// array('chicago' => array('MCD', 'TACOBELL'));
function test(){
$in = array("abc","bcd","efg");
$result = mydba->executestoredprocedure('proc1',$in);
$arr_sim=array();
foreach ($result['recordset'] as $rows) {
if(!empty($rows)){
if(array_key_exists($rows['field1'], $arr_sim) {
$arr_sim[$rows['field1']][] = $rows['field2'];
} else {
$arr_sim[$rows['field1']] = array($rows['field2']);
}
}
}
return $arr_sim;
}
Replace $arr_sim[$rows['field1']] = $rows['field2'] with $arr_sim[$rows['field1']][] = $rows['field2']. This will create an array of arrays.
echo $arr_sim['chicago'][0]; // MCD
echo $arr_sim['chicago'][1]; // TACOBELL
Technically, you should write something like this to avoid notices:
if (!isset($arr_sim[$rows['field1']])) $arr_sim[$rows['field1']] = array();
$arr_sim[$rows['field1']][] = $rows['field2'];
But you must really ask yourself, is the field1 (city names) worthy of being the primary key for the array? If not, you should choose some other identifier.

Categories