-1

I am having issues when iterating through an array of arrays in PHP.

I have the following array, which I have posted to the page using Ajax.

Array ( 
[0] => Array ( [0] => T64 [1] => Array ( [name] => T64 [dummyA] => 2 [dummyB] 
=> 2 [dummyC] => 2 ) ) 
[1] => Array ( [0] => T65 [1] => Array ( [name] => T65 [dummyA] => 2 [dummyB] 
=> 2 [dummyC] => 2 ) ) 
[2] => Array ( [0] => T91 [1] => Array ( [name] => T91 [dummyA] => 2 [dummyB] 
=> 2 [dummyC] => 2 ) ) )

I have tried to print all of the inner values using the below method, however it always prints a T (on its own line), before each desired value.

foreach($sOptions as $row => $innerArray){
  foreach($innerArray as $innerRow => $value){
    print $value['dummyA'] . "<br/>";
    print $value['dummyB'] . "<br/>";
    print $value['dummyC'] . "<br/>";
  }
}

Output:

T
T
T
2
2
2
T
T
T
2
2
2
T
T
T
2
2
2

Would anyone be able to give be some incite to where these T values are coming from?

James Pavett
  • 310
  • 3
  • 17

3 Answers3

3

can you try to use only one foreach ? I haven't try this solution but you can try to do something of similar

foreach($sOptions as $row => $innerArray){
    $value = $innerArray[1]
    print $value['dummyA'] . "<br/>";
    print $value['dummyB'] . "<br/>";
    print $value['dummyC'] . "<br/>";
}
  • That works perfectly. Can I ask how this line works? $value = $innerArray[1]. I can't quite work out what it is doing. – James Pavett Nov 29 '18 at 09:13
  • I have simplified the code, I have taken the array and I have cycled in the array, for every element I have saved the second part of subarray in value, then I have statically taken the parts of subarray saved in value – David Marabottini Nov 29 '18 at 09:17
  • I think @ino answer is better because it also explain the root cause for that issue - when providing just the fix OP will learn less... – dWinder Nov 29 '18 at 09:22
3

It is obvious. You are trying to get indexed value of the actual value from second level array using a string index that does not exists.

Because any string can be accessed as an array then the non existing string index is interpreted as false so it returns first value, ie zero-index character from the string accessed as an array.

In first case the value T64 is actually accessed as array('T','6','1') so it returned T.

<?php

// simple example
// Get the first character of a string
$str = 'T64';
echo "First character is: ". $str[0] .PHP_EOL;
echo "Second character is: ". $str[1] .PHP_EOL;
echo "Third character is: ". $str[2] .PHP_EOL;

echo PHP_EOL;

echo "Applied to your code:". PHP_EOL;
$sOptions = Array ( Array ( 'T64' ) );

print_r($sOptions[0][0]['invalid string index']);

Look at the demo: https://eval.in/1063420

Output:

First character is: T
Second character is: 6
Third character is: 4


T

Note: turn on error reporting to simplify your debugging ;) How do I get PHP errors to display?

More reading about accessing strings as array in described in PHP manual Strings esspecialy Example #11 and later.

ino
  • 1,698
  • 1
  • 12
  • 21
0

You can also use array_column to isolate the part you want to iterate.

foreach(array_column($arr,1) as $row => $value){
    echo $value['dummyA'] . "\n";
    echo $value['dummyB'] . "\n";
    echo $value['dummyC'] . "\n";
}

This means the code will only iterate the following:

array(3) {
  [0]=>
  array(4) {
    ["name"]=>
    string(4) "T64 "
    ["dummyA"]=>
    string(2) "2 "
    ["dummyB"]=>
    string(2) "2 "
    ["dummyC"]=>
    string(2) "2 "
  }
  [1]=>
  array(4) {
    ["name"]=>
    string(3) "T65"
    ["dummyA"]=>
    string(2) "2 "
    ["dummyB"]=>
    string(2) "2 "
    ["dummyC"]=>
    string(1) "2"
  }
  [2]=>
  array(4) {
    ["name"]=>
    string(4) "T91 "
    ["dummyA"]=>
    string(2) "2 "
    ["dummyB"]=>
    string(2) "2 "
    ["dummyC"]=>
    string(2) "2 "
  }
}

https://3v4l.org/YA6Vq

Andreas
  • 24,301
  • 5
  • 27
  • 57