1

I got an array containing results from a SELECT statement. The weird thing is that I can print the array using:

echo json_encode($results);

I got :

[
  {
    "Habitacions": "3",
    "Tipus": "Piso",
    "Localitat": "El Vendrell",
    "Metres": "2",
    "Preu": "300",
    "Embarg": "0",
    "Operacio": "Lloguer",
    "Imatge1": "3_1.jpg",
    "Imatge2": "3_2.jpg",
    "Imatge3": "3_3.jpg",
    "Imatge4": "3_4.jpg",
    "Banys": "2",
    "Idelement": "3",
    "Tipus_EN": "Flat",
    "Tipus_CAT": "Pis",
    "Imatge1_Big": "3_1B.jpg",
    "Imatge2_Big": "3_2B.jpg",
    "Imatge3_Big": "3_3B.jpg",
    "Imatge4_Big": "3_4B.jpg",
    "Descripcio": "Test es",
    "ref": "3",
    "Obra": "0",
    "Descripcio_CAT": "Test cat",
    "Descripcio_EN": "Test en"
  }
]

But if I try this, I don't get anything echoed:

<?php echo $results['Descripcio_CAT']?>

Same for the rest of the keys.

2
  • What does var_dump($results) output? Commented Aug 16, 2013 at 15:03
  • 2
    $results[0]['Descripcio_CAT'] Commented Aug 16, 2013 at 15:07

2 Answers 2

1

Try something like

$row = mysql_fetch_array($result);
echo $row['Descripcio_CAT'];

for each row returned.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is, is that your json object shows that $results is an array around an object. This is what you need to do to access a value in that object.

<?php echo $results[0]->Descripcio_CAT; ?>

If you want the object alone you can assign it to a variable

<?php
$object = $results[0];
echo $object->Descripcio_CAT;
?>

Or if you will have a list of objects, you can use a foreach loop.

<?php
foreach($results as $object) {
    echo $object->Descripcio_CAT;
}
?>

If you just want to see the variable without knowing the contents you can var_dump it!

<?php var_dump($results); ?>

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.