-4

Would anyone have a good example of a hard coded Multidimensional array using Codeigniter. The examples I've seen use a database, but I'm looking for an example that is hard coded.

Thanks

Greg

Greg_C
  • 11
  • 1
  • 2
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – techydesigner Sep 20 '16 at 04:04
  • Arrays are PHP and not framework specific. This question / request does not belong here. Best would be to change your question to note down a problem. – killstreet Sep 20 '16 at 08:18

2 Answers2

0
$data = array('one' => array('sub1' => 42, 'sub2' => 90310), 
              'two' => array('sub1' => 321, 'sub2' => 10122));

Same thing with "short syntax"

$data = [
        'one' => ['sub1' => 42, 'sub2' => 90310], 
        'two' => ['sub1' => 321, 'sub2' => 10122]
        ];
DFriend
  • 8,869
  • 1
  • 13
  • 26
0

There are two types of array

  1. Array like @DFriend answer

  2. Array objects

use print_r($arrayName); so that you can see the array structure.

For an example, let's say you getting result from the database. and if you use $result = $query->result(); you will get an array object and if you use $result = $query->result_array(); you will get 2D array.

those two array will be like below, (@DFriend example)

2D Array

$data = array('one' => array('sub1' => 42, 'sub2' => 90310), 
              'two' => array('sub1' => 321, 'sub2' => 10122));

Array Object link

Array ( 
[0] => 
    stdClass Object 
    ( 
        [id] => 25 
        [time] => 2014-01-16 16:35:17 
        [fname] => 4 
        [text] => 5 
        [url] => 6 
    ),
[1] => 
    stdClass Object 
    ( 
        [id] => 25
        [time] => 2014-01-17 16:35:17 
        [fname] => 45
        [text] => 55
        [url] => 66
    ) 
)
Community
  • 1
  • 1
mapmalith
  • 1,303
  • 21
  • 38
  • PM-Riable, What you call "Array objects" is a 1D array. To be multidimensional an array must containing one or more additional arrays. Your "Array objects" contains multiple Objects (a.k.a. classes) but Objects are not arrays. – DFriend Sep 20 '16 at 12:14
  • Yes it is and I accept with you. In the question Greg_C mentioned **examples I've seen use a database** so I talked about one another way of use database results. – mapmalith Sep 20 '16 at 13:51