How to access attributes of php object inside a array of php objects from javascript

Poorna

I have an array of php objects. Inside a javascript function I want to access attributes of an object in the array. If I json_encode the whole array it shows these attributes are undefined. How can I do this. I new to PHP.

PHP Code:

$_SESSION['objArray'] = $objArray;

Javascript Code:

const objArray = <?php echo json_encode(($_SESSION['objArray'])) ?>;
Victor

You certainly need to encode your array to JSON so it can be usable by a JavaScript client.

As you mentioned, you have instances of particular classes in your array, so simple JSON encoding won't work for sure.

Here, PHP comes with the JsonSerializable interface. You will have to implement this interface on your classes. Let's take a Foo example:

class Foo implements JsonSerializable
{
  private $thing;

  public function __construct($thing)
  {
    $this->thing = $thing;
  }

  public function jsonSerialize()
  {
    return [
      'thing' => $this->thing,
    ];
  }
} 

Here is an example of the above code snippet. As you can see, now you can create an instance of Foo, embed it into an array or whatever, and JSON-encode it with your own representation:

$foo = new Foo('something');
echo json_encode(['foo' => $foo]);

// {"foo": {"thing": "something"}}

Outputting this into an inline JavaScript block would work similar to what you wrote:

<script type="application/javascript">
const obj = "<?php echo json_encode(($_SESSION['objArray'])) ?>;";
</script>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to use php inside of javascript?

분류에서Dev

How to access a property in array of objects without looping through all of them in php?

분류에서Dev

how is Array object different from other objects

분류에서Dev

How to delete an element inside a multidimensional array in PHP?

분류에서Dev

How to access a PHP array with keys of inner values

분류에서Dev

Javascript: extract objects from inside an array of objects, and put them into a new array

분류에서Dev

Turning my php array into [object Object] json/JavaScript

분류에서Dev

Merge a php array and a php object

분류에서Dev

access json object returned from php file via ajax

분류에서Dev

php array not saved in object

분류에서Dev

Sorting array object in php

분류에서Dev

PHP How to retrieve value from returned object

분류에서Dev

PHP: access object with key int

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

How to access PDO connection object across multiple class?[php +Mysql]

분류에서Dev

How to receive array data from post in php?

분류에서Dev

Ajax PHP/Javascript array

분류에서Dev

How to add php content or variable inside javascript alert box?! javascript, php, alertbox

분류에서Dev

How to access a var with "-" in PHP

분류에서Dev

How to access a JavaScript object got from document.getelementbyId() in jQuery?

분류에서Dev

How to populate an object inside an object in an array?

분류에서Dev

How to get all values of objects inside array

분류에서Dev

WordPress 및 PHP의 JavaScript Inside JavaScript

분류에서Dev

PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

분류에서Dev

How do I filter an array with only valid object in php

분류에서Dev

Update the array with attributes from different array with same key for both - javascript

분류에서Dev

How to remove a simple specific key value pair from all objects inside an array

분류에서Dev

Get object from array with NSDictionary objects

분류에서Dev

How to access user languages inside GraphObject using Facebook PHP SDK v4

Related 관련 기사

  1. 1

    How to use php inside of javascript?

  2. 2

    How to access a property in array of objects without looping through all of them in php?

  3. 3

    how is Array object different from other objects

  4. 4

    How to delete an element inside a multidimensional array in PHP?

  5. 5

    How to access a PHP array with keys of inner values

  6. 6

    Javascript: extract objects from inside an array of objects, and put them into a new array

  7. 7

    Turning my php array into [object Object] json/JavaScript

  8. 8

    Merge a php array and a php object

  9. 9

    access json object returned from php file via ajax

  10. 10

    php array not saved in object

  11. 11

    Sorting array object in php

  12. 12

    PHP How to retrieve value from returned object

  13. 13

    PHP: access object with key int

  14. 14

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  15. 15

    How to access PDO connection object across multiple class?[php +Mysql]

  16. 16

    How to receive array data from post in php?

  17. 17

    Ajax PHP/Javascript array

  18. 18

    How to add php content or variable inside javascript alert box?! javascript, php, alertbox

  19. 19

    How to access a var with "-" in PHP

  20. 20

    How to access a JavaScript object got from document.getelementbyId() in jQuery?

  21. 21

    How to populate an object inside an object in an array?

  22. 22

    How to get all values of objects inside array

  23. 23

    WordPress 및 PHP의 JavaScript Inside JavaScript

  24. 24

    PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

  25. 25

    How do I filter an array with only valid object in php

  26. 26

    Update the array with attributes from different array with same key for both - javascript

  27. 27

    How to remove a simple specific key value pair from all objects inside an array

  28. 28

    Get object from array with NSDictionary objects

  29. 29

    How to access user languages inside GraphObject using Facebook PHP SDK v4

뜨겁다태그

보관