How to proccessing array in array in laravel [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 2 years ago.
Improve this question
Can someone help me with this difficult array
In the web im find all cities and regions of Ukraine and im want to display this in select-option tag
I want to show separately regions and cities
This is link of json file
Also im saved this JSON file on my PC
This is my code of proccessing arrays
public function index()
{
$result = json_decode(file_get_contents('5.json'));
foreach ($result as $k => $v)
{
foreach ($v as $item)
{
dd($item);
}
}
return response()->json($item);
}

I think should format $result to array
$result = json_decode(json_encode($result), true);

Laravel provides a fluent wrapper for arrays. See Collections.
Initialize a collection by:
$result = json_decode(file_get_contents('5.json'));
$collection = collect($result);
Looking at your data, I don't know if this will perform well. I had experiences before where performance starts to become a problem, so please be wary of usage.

Related

How to get records between two latitudes and longitudes using Laravel, php [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
here what I tried to get but it returns empty between 2 given coordinates
function searchListings(){
$query = Listings::query();
$sw_lat = request('sw_lat');
$ne_lat = request('ne_lat');
$sw_lng = request('sw_lng');
$ne_lng= request('ne_lng');
$query->whereBetween('latitude', [$sw_lat, $ne_lat])->whereBetween('longitude', [$sw_lng, $ne_lng]);
return $query::paginate(5);
}
Check datatype
Use a numeric database column type. Example of errors with a text/varchar column:
User::truncate();
User::factory()->count(1)->create(['name' => 1.5]);
User::whereBetween('name', [1,2])->paginate()->first();
// One result
User::truncate();
User::factory()->count(1)->create(['name' => -1.5]);
User::whereBetween('name', [-2,-1])->paginate()->first()
// No results (!)

Convert and iterate through Javascript object array to PHP array [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 the following being sent to a PHP/Laravel endpoint which I am working on. I need to convert it to PHP array so I can iterate through the members but I can't seem to be able to do json_decode() on it
{
levels: [
{role_id:1, access_level_id:3},
{role_id:2, access_level_id:1},
{role_id:3, access_level_id:2}],
category_id: 3
}
On my backend I have my method as follows
public function myfunctionname (Request $request)
{
$levels = json_decode($request->levels);
}
I want to be able to iterate through the items in $request levels as a PHP array but $levels is returning as empty
I guess I am doing something wrong about this.
Please guide if you can
Thank you
Read this solution only if you are using javascript to generate the json:
When sending json via ajax you can turn it into a regular json string with JSON.stringify:
var v = {
levels: [
{role_id:1, access_level_id:3},
{role_id:2, access_level_id:1},
{role_id:3, access_level_id:2}],
category_id: 3
};
var j = JSON.stringify(v);
console.log(j);
{"levels":[{"role_id":1,"access_level_id":3},{"role_id":2,"access_level_id":1},{"role_id":3,"access_level_id":2}],"category_id":3}
... and you can decode this with json_decode(..., true) on PHP.
Your keys are not quoted properly, this is the format that json_decode expects, this is the reason why you are getting null. To iterate through the items it's better to use json_decode($jsonString, true); Note the second parameter set as true, this second parameter will return an array instead of an object of type \stdClass.
See the following example:
https://3v4l.org/GM5fR

Defining a mySql result as a variable for a while loop [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 8 years ago.
Improve this question
I am wondering if this is possible. I know this is hardly practical...I did though, run into a case where this might come in handy.
Take this example, straight forward.
while( $row = $qry->fetch(PDO::FETCH_ASSOC) ){}
Now, can something like this be done?
$newVar = '$row = $qry->fetch(PDO::FETCH_ASSOC)';
while($newVar){}
I have tried many ways but to no avail.
My while loops print out a lot of data and depending on the type of query/connection (mySql, mySqli, or PDO) would depend on what statement goes into the while loop.
This is somewhat my fallback idea if prepared statements are not supported.
If the function to fetch a row is different, you may be able to use closures depending on the PHP version you are using. To use closures, you would want to define a function that knows how to fetch a row, then you can pass it to your while loop. For example, here are closures for PDO and MySQL:
$pdo_fetch = function() use ($qry)
{
return $qry->fetch(PDO::FETCH_ASSOC);
}
$mysql_fetch = function() use ($result)
{
return mysql_fetch_assoc($result);
}
These closures can come from anywhere, then passed into your while loop:
if($pdo)
{
$fetch = $pdo_fetch;
}
else
{
$fetch = $mysql_fetch;
}
while($row = $fetch()) { }

Get reference variable from multidimensional array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://sandbox.onlinephpfunctions.com/code/88256ea59ecdeae948cf664b477e113d7263f2c8
As you can see I use $this->options as input value.
What I'm trying to achieve here is getting option from value by key name and return it.
That is the easy part, but what I want to do with variable from returnOption is setting new key on it and see changes in $this->options array.
How can I archieve this?
function & returnOption(&$options, $optionName)
{
foreach($options as $key => &$value)
{
if($key === $optionName)
return $value;
}
}
$op =& $this->returnOption($this->options, $optionName);
$op['newValue'] = 'value';
var_dump($this->options);
Something like that should work. Then again, $this->options should probably be some sort of object with proper accessors for the options you're trying to get to.

JSON with PHP foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok as per your suggestion I updated... By default Laravel returns JSON... I have set it to return an array but I am still getting the same row duplicated using:
$limits = array();
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
}
return json_encode($limits);
}
You're just overwriting $limits inside that foreach() loop. Perhaps you mean something more like
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
^^--- array push?
}
As well, you don't "implement" JSON instead of arrays. You work with NATIVE data structures, then encode that structure into JSON. JSON's a transport format, it's not something you should ever deal with natively.
the $limits array will hold the last value what coveragelimit::index() returns over the iterate, I would suggest a check on coveragelimit::index() return value if it falls with "Marc B"'s answer.
EDIT:
foreach($pieces as $key=>$coverage_limit) {
$limits[$key] = coveragelimit::index($coverage_limit);
}
or
foreach($pieces as $coverage_limit) {
array_push($limits, coveragelimit::index($coverage_limit));
}
both should returns the same as Marc B's answer

Categories