json_decode() expects parameter 1 to be string, array given
This error message appears when you attempt to call json_decode() on a variable that is already an array. In your specific case, $data is constructed as an array of associative arrays containing guide information. When PHP encounters the line echo json_decode($data);, it throws a fatal error because the function signature strictly requires a string input representing JSON data, not a native PHP array structure.
The core misunderstanding here stems from confusing the direction of data transformation. json_encode() converts a PHP array or object into a JSON-formatted string, while json_decode() performs the reverse operation: it takes a JSON string and converts it back into a PHP variable. You are currently holding a native PHP array in $data and trying to decode it as if it were already a string. Since there is nothing to decode, the function fails immediately upon checking the parameter type.
If you intend to view this data as a JSON string (perhaps for debugging or sending it over an API), you need to apply json_encode() instead of json_decode(). When you switch to json_encode($data), PHP generates a valid JSON string representation of your array. This is why your updated code outputted a structure like [{"guide": "Sid Sad Dadashzadeh"}, ...]. The function successfully serialized the internal PHP arrays into a text format that adheres to the JSON specification.
However, there is a critical issue in your data payload: the presence of Persian characters being rendered as Unicode escape sequences (e.g., \u0633). This indicates that json_encode() was executed with UTF-8 encoding enabled (which is default), but the output was likely not displayed correctly due to character encoding mismatches between your browser, PHP output handler, or terminal. By default, json_encode() sets the JSON_UNESCAPED_UNICODE behavior to false in older PHP versions or specific configurations, causing it to escape non-ASCII characters into \uXXXX sequences to ensure data integrity during transmission. If you want these Persian words to appear as readable text in your browser output rather than escaped strings, you should pass the JSON_UNESCAPED_UNICODE flag to the function:
echo json_encode($data, JSON_UNESCAPED_UNICODE);
This modification instructs PHP to output UTF-8 characters directly without escaping them, provided the output stream supports UTF-8. If you are using a modern version of PHP (5.6+), this is often the default behavior depending on configuration, but explicitly setting the flag guarantees readability for non-Latin scripts like Persian or Arabic.
On the other hand, if your goal was to pass this data from one part of your application to another as JSON, you must ensure that the receiving end expects a string and can handle the decoding process correctly. If $data is intended to be stored in a database or sent via an HTTP request body, keeping it as a JSON-encoded string is appropriate. However, if you are simply iterating over this data within your PHP application logic, there is no need to encode it at all. Passing a native array around your code is generally more efficient and easier to manipulate than constantly encoding and decoding strings.
Additionally, be wary of the content of your $data array. In your example, one of the entries contains null. While valid in JSON, ensuring data consistency before serialization is best practice. If a column in your database allows nulls, json_encode() will handle it gracefully by outputting "guide":null, but if you later attempt to access that value without checking, it could lead to unexpected behavior in downstream logic.
For more complex scenarios involving large datasets or frequent data exchange between systems, consider leveraging Laravel's helper methods or database-specific JSON column types if you are using MySQL 5.7+ or PostgreSQL, which can store and query native JSON objects directly, eliminating the need for manual encoding/decoding cycles in your PHP code.
