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
Related
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
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>
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
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
I am attempting to debug a php script I did not write. In order to check if a location already exists, they used the following:
$locTable = JTable::getInstance('Location', 'DPCalendarTable');
if ($locTable->load(array('latitude' => $data['latitude'],'longitude' => $data['longitude'])){
$_eventLocCache[$data['alias']] = (int)$locTable->id;
}
What is the purpose of "..->load(array..."?
Since you use Joomla it retrieves data from the table called Location
JTable::load() - Loads a row from the database and binds the fields
to the object properties.
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
Hi I'm totally new here and with and would glad if someone could help as I'm stuck with how I can create a loop with some php code.
$TotalUniques = $this->TotalUniquesHits($Counts[$xx]['ID']);
The value I'm after is [$xx] which represents a position in the array. How to change it so that I get $TotalUniques looping through all the values of $xx?
You can try with foreach loop:
foreach($Counts as $xx){
$TotalUniques[] = $this->TotalUniquesHits($xx['ID']);
}
You may also consider checking if each $xx has ID key with isset or array_key_exists.