2

I'm total newbie to PHP and Drupal but I fix this simple(?) thingo on my template. I want to get title, date, text and link path from this array? I can't get it out of there. I think its because its in inside of another array and because im noob in PHP I cant get it out of there ? Also I would like to get it to a loop. If theres more content, I would get it as a list or something. I would use foreach for this? Like foreach $contents as $content and so on?

I get this output from this: var_dump($contents);

array
'total' => string '1' (length=1)
'data' => 
  array
    0 => 
      array
      'cid' => string '13231' (length=3)
      'title' => string 'TITLEBLABLABLA' (length=3)
      'body_text' => string 'TEXTBLABLABAL' (length=709)
      'created' => string '313131' (length=10)
      'created' => string '2010-07-13 14:12:11' (length=19)
      'path' => string 'http://goog.fi' (length=72)

3 Answers 3

1

Think of accessing multidimensional arrays in the same way you'd access a file in a subdirectory: just reference each level/directory in sequence. Assuming that array is stored in $arr, you'd get the title as follows:

$arr['data']['0']['title']
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a basic PHP array tutorial

It comes down to this:

You retrieve an element of an array with []

$array = array( 'a' => 'b');
echo $array['a']; //Prints b;

Comments

0

To loop through a multi-dimensional array and echo date try this...

foreach ($contents as $key => $content) {
        echo $content[$key]['created'];   
    }

If that doesn't work, post the output of print_r($content) in your question so I can't build you exact array. I'm kinda confused over the structure of your array in your question.

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.