user3524311
user3524311

Reputation: 31

How to access data in a multidimensional array?

I have a multi array, how do i access each individual item i.e. "profile title" and is there a way i could loop through the array and assign variable to each arrays values for example

$firstarray->name $firstarray->html

$secondarray->typeId

array(4) {
      [0]=>
      array(4) {
        ["name"]=>
        string(13) "profile title"
        ["html"]=>
        string(52) "<h2 class="entry-title" id="title">Your Profile</h2>"
        ["typeId"]=>
        string(1) "1"
      }
      [1]=>
      array(4) {
        ["name"]=>
        string(8) "username"
        ["html"]=>
        string(145) "<fieldset disabled><br><label for="nameinput">Username</label><input type="text" id="userName"class="form-control" placeholder="" ></fieldset><p>"
        ["typeId"]=>
        string(1) "1"
      }

Upvotes: 0

Views: 55

Answers (2)

Sasanka Panguluri
Sasanka Panguluri

Reputation: 3138

Vincent's answer should help you access each element. However, to assign them, you need to use array_push

`array_push`(  $firstarray, array('key'=>'value','key1'=>'value1')   );


OR 


$firstarray[0]['key'] = 'something';
$firstarray[0]['key1'] = 'something else';

$firstarray[1]['key'] = 'something';
$firstarray[1]['key1'] = 'something else';

etc..


OR 

$firstarray[0] = $secondarray1;
$firstarray[1] = $secondarray2;

etc..

Upvotes: 0

Vincent Decaux
Vincent Decaux

Reputation: 10722

Assuming your array is called $firstarray :

foreach ($firstarray as $row)
{
    echo $row['name']; // or $row->name;
}

Upvotes: 3

Related Questions