Convert database string to array [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a field in one of my database tables, a sample of this field looks like so ; ["1","4","7"]. Those are keys to a reference in another table. What i want to do is fetch the field and ideally loop through the values and get the data from the referenced table. As you will notice, ["1","4","7"] is PHP declaration for an array but for the life of me, I am unable to parse it as an array. How do i parse it as an array? What am I missing?

You should be able to decode and convert it into an array with PHP's json_decode function.
Example:
<?php
echo '<pre>';
print_r(json_decode('["1","4","7"]'));
echo '</pre>';
?>

If I understood your problem is that your database column store a string PHP array representation.
So you should use the function json_decode
By the way it's a strange way to store relation in a database

Related

Array Push in Laravel 123 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to push new array item into existing array variable that has items from database. What I want to do is add a new item named 'Others' at the end of this array and display it as select drop down in view which consists of all the items from database and at the end of this select the 'Others' item that I manually added in my controller.
Here is what I tried to do:
When you query from database in Laravel, the result is a collection object. You will first need to convert this collection object to array to be able to use array functions. The following should work in your case:
$admin = DB::table('tbl_account_admin')->where('id', $id)->get()->toArray();
You can use json_decode(json_encode($admin), true);
And then use array_push function

How to sort on title with relationsship [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have following query: $scholen = Scholen::with('media', 'translations', 'seo')->online()
when i dump it:
How can i sort the schools on 'title' from the relations -> translations -> fields
Thanks in advance.
Olivier
You could access fields with $scholen[$index]->translations[$index]->fields, however you couldn't access the title property since the whole value of fields is a string - you would have to strip the string to get the bit you wanted.
You would have to get() the results for the query, and then sort the results by including this logic inside a sortBy() callback, e.g.
$scholen->sortBy(function ($value, $key) {
// Logic here to get the part of the `fields` string you want
// and then sort it
});
Hope this helps, this looks like a bit of a tricky one

why the OpenServer displays the code in a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
why the OpenServer displays the code in a string but not in the column as indicated in the light image. I do everything the same, but I have everything on the line?
please help me, what am I doing wrong?
this is my code:
and this is my output and all one line:
but it should be like this[original of the required output]
You have var_dump($x) in there two times, which will display the array as a string like in your image. But the echo $x[0] is displayed/echoed correctly in between those two - as "18".
If you want to echo all values of the array, you need a foreach loop, like
foreach($array as $value) {
echo "<p>".$value."</p>";
}
EDIT AFTER COMMENT:
Put the arrays into pre tags and use print_r($x); instead of var_dump($x), like
<pre>print_r($x);</pre>

Submit an array string from codename one to PHP using connection request [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Am fetching data from mysqlite to a string array which i want to submit to PHP then update a mysql table.
Any easy way out, because doesn't seem to work?
I suggest using the network monitor tool to see that the Codename One code is sending out the right data as explained in the IO section of the developer guide.
I notice your PHP code tries to decode JSON into an array whereas your Java code adds a POST array both of which have nothing in common. I don't really know PHP's equivalent for handling an array so I can't help you there. On the Codename One side you can just write the array as JSON by converting it to a JSON String.

How to print the user birthdate from Facebook_API [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was successful in querying the Graph Api and receive the user birthday,but unable to print it.
My API call: /me?fields=id,name,picture,birthday{date};
echo $user['birthday'];
I am receiving this error while trying
Please help,i am newbie to PHP...
Use this to get birthday:
echo $user['birthday']->format("Y-m-d H:i:s");
you can't just echo ['birthday'] because it is a datetime object so you need to use a function of the DateTime object to convert to string.
What is the result of :
print_r($user)
Available indexes will be printed.
You would consider to test your variable with this 2 functions : is_array($user) and isset($user['birthday'])
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/function.is-array.php

Categories