0

I am getting this as var_dump:

array(1) {
  [0]=>
  array(4) {
    ["num"]=>
    string(1) "1"
    ["yyyy"]=>
    string(4) "2013"
    ["mm"]=>
    string(2) "12"
    ["dd"]=>
    string(2) "11"
  }
}

How to access the array elements?

6
  • I am using fetch mode as ASSOC, so why am I still getting array within another array? Newbie here, pardon for noviceness :) Commented Nov 27, 2013 at 10:54
  • $array_variablename[index] Commented Nov 27, 2013 at 10:54
  • use foreach to iterate Commented Nov 27, 2013 at 10:54
  • @user2961121: you are probably getting multiple rows from your db. But since it's only one row, it has the index. If you were to select more rows it should be clear. Commented Nov 27, 2013 at 10:57
  • Perhaps you should read up on PHP arrays: us2.php.net/manual/en/language.types.array.php Commented Nov 27, 2013 at 10:59

6 Answers 6

1

Let's assume your array is $arr, you can do

echo $arr[0]['num'];
echo $arr[0]['yyyy'];
echo $arr[0]['mm'];
echo $arr[0]['dd'];

As you are fetching from a database, you will receive an array for each result row, and within each array will be another array of columns. you can use a foreach() loop to iterate over the data, as follows:

foreach($arr as $row) {
    echo $row['num'] . ':' . $row['yyyy'] . '-' . $row['mm'] . '-' . $row['dd'] . "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this

foreach($array as $value) {
    foreach($value as $k=>$v) {
        echo $k . " : " . $v .'<br/>';
    }
    echo '<hr>';
}

Comments

0

Please have a look at the official PHP Doc article about arrays.

In your case:

$yourArrayVariable[0]['yyyy']

Will let you access the element with value 2013.

Or if you have an undefined number of array entries you can iterate over it with either foreach or for.

foreach($yourArrayVariable as $key => $value) {
    echo $key , ': ' , $value , '<br>';
}

or if you have only numeric indeces without a gap:

$arrCount = count($yourArrayVariable);

for($i = 0; $i < $arrCount; ++$i) {
   echo $i , ': ' , $arrCount[0] , '<br>';
}

Comments

0

store array in a variable like

$arr =array(1) {
  [0]=>
  array(4) {
    ["num"]=>
    string(1) "1"
    ["yyyy"]=>
    string(4) "2013"
    ["mm"]=>
    string(2) "12"
    ["dd"]=>
    string(2) "11"
  }
}

for access array elements you have to use following code

echo $arr[0]['num'];
echo $arr[0]['yyyy'];
echo $arr[0]['mm'];
echo $arr[0]['dd'];o $arr[0]['num']

1 Comment

Did you just copy and paste my answer?
0
$arr =Array(
  0=>array(
    "num"=>"1",
    "yyyy"=>"2013",
    "mm"=>"12",
    "dd"=>"11",
  )
 );


 foreach ($arr as $value) {    
     echo "num: ".$value["num"];
     echo "yyyy: ".$value["yyyy"];
     echo "mm: ".$value["mm"];
     echo "dd: ".$value["dd"];
 }

Comments

0

You can get the value using echo $array[0]['num']; gives output as 1

    $array ='your array data here';
    foreach($array as $key=>$value) {
        echo "num: ". $value['num'] . "/yyyy: ". $value['yyyy']. " /mm: ". $value['mm'] . " /dd: ". $value['dd'] . "<br>";
    }

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@win, why not, how you said that. run this code will give output.
@win com'on buddy, just look at unaccepted answer as well. All the answers are not same, If could, even you can post better then all.

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.