0

I'm really struggling to get my head around multi-dimensional arrays in PHP, my background is C and Java both of which I have no issues with arrays!

I'm trying to read from an SQL database a list of months, each month has a list of values corresponding to a value.

ie.

  2014-01-01, ("Val1", "Val2", "Val3", "Val4", "Val5"), (3, 4, 7, 5, 3)
  2014-02-01, ("Val1", "Val2", "Val3", "Val4", "Val5"), (5, 3, 6, 2, 8)
  2014-03-01, ("Val1", "Val2", "Val3", "Val4", "Val5"), (6, 5, 4, 3, 2) ...

I can read the values, I can split them down, but I want to be able to add those values to an array but putting the values into the correct month.

I c/Java I'd just create an array like this;

[0,0,0,0,0,0,0,0,0,0,0,0]  - Val1 goes in here
[0,0,0,0,0,0,0,0,0,0,0,0]  - Val2 goes in here
[0,0,0,0,0,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0,0,0,0,0]  - Val5 goes in here

then as I parse through the values, I'd pick up the month, and add the values I read into the correct month.

I can create the array in PHP, all I need to know, is there a blindingly obvious way that I've missed in my coffee/sleep deprived state that I can basically say something like;

If I want to change position 11 in the the 2nd array to 6 in C I would be this;

array[2][11] = 6

is there an equivalent to do this in PHP?

5
  • 2
    $array[2][11] = 6; not working for you? Commented Jun 24, 2014 at 10:53
  • 1
    It's exactly the same in PHP: $arr[2][11] = 6, assuming your array is numerically indexed. Please show your complete code so we can get a better understanding Commented Jun 24, 2014 at 10:54
  • $array[2][11] = 6; should work Commented Jun 24, 2014 at 10:54
  • Have you initialised your array ? PHP can't guess you are using nested array. You need to create each subarray manually before accessing them. Commented Jun 24, 2014 at 10:56
  • Shouldn't it be $array[1][11] for the second array? Commented Jun 24, 2014 at 10:57

2 Answers 2

1

This would work:

$myArray[2][11] = 6;
$myArray[2][12] = 7;
var_dump( $myArray ); //view structure
Sign up to request clarification or add additional context in comments.

Comments

0

A quick example for array access

$data = [
    '2014-06-24' => ['Val1', 'Val2', 'Val3'],
    '2014-06-23' => ['Val1', 'Val2', 'Val3']
];

In PHP you have different ways for array access. The internal iterator in arrays begins with 0. Quick and dirty you can do the following.

$data['2014-06-24'][0] = 'Val4';

So your $data array will look like

$data = [
    '2014-06-24' => ['Val4', 'Val2', 'Val3'],
    '2014-06-23' => ['Val1', 'Val2', 'Val3']
];

In a more object orientated way you can use the ArrayObject class of the StandardPhpLibrary (SPL), wich brings many methods and objtects (interfaces, iterators, etc.) for dealing with arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.