My code is giving an error when I try to echo all values within an array but also expand and echo the values of any arrays in the original array.
while (list($key, $val) = each($bal)) {
If (is_array($val)){
while (list($k, $v) = each($v)) {
If (is_array($va)){
while (list($ka, $va) = each($va)) {
echo "$ka => $va\n <br>";
}
} else {
echo "$k => $v\n <br>";
}
}
} else {
echo "$key => $val\n <br>";
}
}
The error I am getting is
Warning: Variable passed to each() is not an array or object in
C:\xampp2\htdocs\money\production\simalgorithm1.php on line 234
Line 234 is while (list($k, $v) = each($v)) {
Shouldn't this automatically not be an array as I did a check the line previous? My Syntax must be off I am fairly new to PHP if someone could help me see what I am not seeing I would greatly appreciate it.
Sometimes the simplest solutions are the best one line problem solved.
print_r($bal);
The problem is this line:
while (list($k, $v) = each($v)) {
I think you want this
while (list($k, $v) = each($val)) {
EDIT: I would also point out that your code will not work if the arrays are nested more than a few levels deep. You should consider defining a recursive function or using var_export or var_dump.
Related
$numofOfficer = sizeof($_POST['officer']); // = 2
for ($cntr = 0; 0 < $numOfficer; $cntr++)
{
foreach ($_POST['officer'][$cntr] as $index => $value)
{
// DO SOMETHING HERE...
}
}
Please help. I don't how to fix this warning.
The argument of foreach is an array having 2 length and 2 dimension. BUT, the other codes same with this went well, same length and length dimension of array argument. I just cant figure what is difference of the code above to the other.
There is no need to use for and foreach doing the same. You can omit the for and reduce you code to:
if (is_array($_POST['officer']))
{
foreach ($_POST['officer'] as $officer)
{
foreach ($officer as $index => $value)
{
// DO SOMETHING HERE...
}
}
}
Below is the code. I tried what was suggested in a related post - removing $_post and also modifying it to $_MY_POST. Both result in several other errors across multiple files, etc. Can someone help? I am not a developer so I apologize for my ignorance in advance.
function clean_data($_POST){
foreach ($_POST as $k => $v) {
$POST[$k] = htmlentities(strip_tags(stripslashes($v)));
$POST[$k] = addslashes($POST[$k]);
}
return $POST;
}
You can't reassign a global variable like $_POST.
This code works:
function clean_data($data){
foreach ($data as $k => $v) {
$_POST[$k] = htmlentities(strip_tags(stripslashes($v)));
$_POST[$k] = addslashes($_POST[$k]);
}
return $_POST;
}
I have one array
$array = array(0=>array('id'=>1 ,'va'=>2),1=>array('id'=>3,'va'=>4));
With the help of two foreach() loop i can use it.
foreach($array as $temp)
{
foreach($temp as $key=>$val)
{
echo $key.'=>'.$val;
}
}
As array has more than 5 lacs record. this solution is not feasible for me.
Keys are dynamic so i can not put keys as static inside first for each loop.
I tried following code but till now did not get any solution.
function myfunction($value,$key)
{
foreach($value as $k=>$a)
{
echo $k.'=>'.$a;
}
}
$array = array(0=>array('id'=>1,'va'=>2),1=>array('id'=>2,'va'=>2));
array_walk($array,"myfunction");
And this one also
$keys = Array_keys($array['0']);
for($i=0;$<=count($array);$i++)
{
for($j=0;$j<count($keys);$j++)
{
echo $keys[$j].'=>'.$array[$i][$keys];
}
}
I want to make this code as optmize as possible.
I am trying to generate an MS Excel spread sheet using PHPExcel 1.7.6 . I am having trouble determining the structure of the array expected.
The code that builds up the columns and rows is as follows:
function _headers() {
$i=0;
foreach ($this->data[0] as $field => $value) {
if (!in_array($field,$this->blacklist)) {
$columnName = Inflector::humanize($field);
$this->sheet->setCellValueByColumnAndRow($i++, 4, $columnName);
}
}
$this->sheet->getStyle('A4')->getFont()->setBold(true);
$this->sheet->getStyle('A4')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$this->sheet->getStyle('A4')->getFill()->getStartColor()->setRGB('969696');
$this->sheet->duplicateStyle( $this->sheet->getStyle('A4'), 'B4:'.$this->sheet->getHighestColumn().'4');
for ($j=1; $j<$i; $j++) {
$this->sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($j))->setAutoSize(true);
}
}
function _rows() {
$i=5;
foreach ($this->data as $row) {
$j=0;
foreach ($row as $field => $value) {
if(!in_array($field,$this->blacklist)) {
$this->sheet->setCellValueByColumnAndRow($j++,$i, $value);
}
}
$i++;
}
}
I'm currently getting an 'Invalid argument supplied for foreach()' error.
I would appreciate it if somebody can outline the correct array structure required.
As IsisCode said, it sounds like you're looking in the wrong direction by associating the problem with PHPExcel. That error is generally just saying that the first argument supplied to foreach() isn't an array.
I don't see anything indicating that the problem is explicitly with the initial foreach in the _headers() method though. If there's a non array element in $this->data then your _rows() method could produce the error as well.
Given:
$this->data = Array(
Array('foo' => 'bar'),
'baz'
)
That would cause the second foreach in _rows() to fail, as an example. Your error message should be able to point you to which foreach() is failing, but ultimately it sounds like you've got a non-array element in $this->data where you don't expect it. If that can't be helped, then consider verifying you're dealing with an array before calling foreach:
function _rows() {
$i=5;
foreach ($this->data as $row) {
$j=0;
if(!is_array($row)) { continue; } // Ignore non-array elements
foreach ($row as $field => $value) {
if(!in_array($field,$this->blacklist)) {
$this->sheet->setCellValueByColumnAndRow($j++,$i, $value);
}
}
$i++;
}
Verifying the type of a variable before handing it to a type-specific function is never a bad idea and can save a lot of headache in general.
I'm going crazy, spent a couple of hours trying different methods in replace values in arrays, but I can't get it to work.
foreach($potentialMatches as $potentialKey)
{
$searchKeywordQuery = "SELECT keyword, id FROM picture WHERE id='$potentialKey'";
$searchKeywords = mysql_query($searchKeywordQuery) or die(mysql_error());
while ($searchKeyWordsRow = mysql_fetch_array($searchKeywords))
{
$keyword = $searchKeyWordsRow['keyword'];
$pictureKeywordArray[$searchKeyWordsRow['id']]['keywords'] = explode(",", $keyword);
$pictureKeywordArray[$searchKeyWordsRow['id']]['match'] = 4;
}
}
foreach($pictureKeywordArray as $key = > $picValue)
{
foreach($picValue['keywords'] as $key = > $picIdValue)
{
if ($picIdValue == $searchIdKey)
{
echo $picValue['match'];
$picValue['match']++;
echo $picValue['match'];
}
}
}
foreach($pictureKeywordArray as $key = > $picValue)
{
echo $picValue['match'];
}
I'm novice as you can see, When I echo the picValue['match'] in the foreach loop it gives me a correct value after "++". But then below when I call the array again it gives me the value of 4 instead of 5 as intended. Thanks in advance for any help with this.
Cause you work with the item copy in first case try $pictureKeywordArray[$key]['match'] instead of $picValue['match']
In that second foreach you need to call it by reference:
foreach($pictureKeywordArray as $key => &$picValue)
{ //^-- `&` makes it by reference
foreach($picValue['keywords'] as $key => $picIdValue)
{
if ($picIdValue == $searchIdKey)
{
echo $picValue['match'];
$picValue['match']++; //now updates what you want it to update
echo $picValue['match'];
}
}
}
foreach works on a copy of the data. You must use a reference to modify the original:
foreach ($foo as $i => &$f)
{
$f++;
}
unset($f); // important to do this if you ever want to reuse that variable later