I have created an array as:

//$field = "email";
//$value = "text";
$structure[$field] = $value;

$structure gets passed to a function that uses a for() loop to work through a process and in this process I need to extract the key/value pair

for($i=0; $i<$cntFields; $i++) {
    $a = $structure[$i][0];
    $b = $structure[$i][1]; 
    echo "Field = $a / Value = $b<br>\n";
    // at this point I need the following:
    // $a should = "email"
    // $b should = "text"
  }

For some reason, when I run the code in the for(), $a and $b come up as empty so how do I reference these by their numerical index instead of their named index? Do I need to create the original array differently?

Thanks!

Pete

$value is an array or a simple variable like 'text'? If that's variable, the resultant code should be:

for($i=0; $i<$cntFields; $i++) {
    $a = $structure[$i];
    $b = $structure[$i];
    echo "Field = $a / Value = $b<br>\n";
    // at this point I need the following:
    // $a should = "email"
    // $b should = "text"
  }

Thanks Martin.

$field and $value are both simple text strings.

I tried it as $a = $structure[$i] and got nothing back... print_r shows the entire array.

Thanks Martin.

$field and $value are both simple text strings.

I tried it as $a = $structure[$i] and got nothing back... print_r shows the entire array.

Try the following in the loop and if it dumps the array of two values then continue reading.

print_r($structure[$i]);

If that dumps an array that is not empty as you expect then your code may need to be as follows.

for($i=0; $i<$cntFields; $i++) {
$a = $structure[$i]['0'];
$b = $structure[$i]['1'];
echo "Field = $a / Value = $b<br>\n";
}

If that doesn't work then please post the result of print_r($structure);

OK, tried that too... but nada

$structure = Array ( [id] => omit [id_hoa] => omit [username] => text [email] => text [name] => text [phone] => text [position] => text [term_start] => date [term_end] => date )

print_r($structure);  // prints the entire array with values
  echo "<br>\n";
  // $cntFields = 9
  for ($i=0; $i<$cntFields; $i++) {
    print_r($structure[$i]);    // prints nothing
    $a = $structure[$i]['0'];
    $b = $structure[$i]['1'];
    echo "Field = $a / Value = $b<br>\n";  // Field = / Value =
  }

Try this code if it works:

for($i=0; $i < count($structure); $i++) {
  $a = $structure[$i]['email'];
  $b = $structure[$i]['name']; 
  echo "Field = $a / Value = $b<br>\n";
  // at this point I need the following:
  // $a should = "email"
  // $b should = "text"
}

The code above is wrong. Just, I understand what has to be the result.
Is more easier if you extract the elements of the array using FOREACH instead of FOR.

foreach($structure as $arr) {
	foreach($arr as $key=>$value) {
	  $a = $key;
	  $b = $value; 
	  echo "Field = $a / Value = $b<br>\n";
	  // at this point I need the following:
	  // $a should = "email"
	  // $b should = "text"
	}
}

The problem is, you use string tags for index instead of numbers. For that reason the original code doesn't work.

Problem is, I can't hard code the 2nd deminsion as those names will change from instance to instance.

And I do recognize that numerical indexs are required to reference elements via for() loops. I also know that you can mix index types within arrays so I was hoping there was a workaround.

Look at it like this, you get data from a form via $_POST, the data comes in based on the order of the fields. So, inside a function you need to break that down inside a for() loop where you may have continue; statments that will skip the foreach() which means the two processes could become out of sync.

For now, I have added a next() in the for() loop as in...

for($i=0; $i<$cnt; i++, next($structure)){
  list($a, $b) = $structure;
}

OK, tried that too... but nada

print_r($structure);  // prints the entire array with values
  echo "<br>\n";
  // $cntFields = 9
  for ($i=0; $i<$cntFields; $i++) {
    print_r($structure[$i]);    // prints nothing
    $a = $structure[$i]['0'];
    $b = $structure[$i]['1'];
    echo "Field = $a / Value = $b<br>\n";  // Field = / Value =
  }

It looks like your array design is wrong. As you have populated the array you have overridden the old values of the array. Could you please post the section of script that generates the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.