views.php
$events = array($events); //convert $events objects to array
asort($events); //sort array in ascending order
foreach($events as $event): //iterate array
echo $event; //getting error here
endforeach;
tried this also,
foreach($events as $key => $event): //iterate array
echo "$key = $event\n"; //getting error here
endforeach;
I am trying to iterate the array in foreach loop but i am getting this error "ErrorException [ Recoverable Error ]: Object of class Database_MySQL_Result could not be converted to string"
Need help to solve this.
$events = array($events);
This line does not convert objects to array, but it just inserts object into array;
So $events[0] equals old $events;
I don't know how you pull your data from DB, but as long as it's Kohana, maybe this would work:
$events = $events->as_array();
try casting events as an array, instead of inserting it into array like:
$events = (array)$events;
this will result in all public properties on the $events object being set as an assoc array.
Related
In my Symfony project I am returning all objects with Doctrine query builder defined with Paginator.
When dumping $posts['data'], response is on the image: IMAGE
When entering the loop and dumping first result, this is what I get: IMAGE
I want on each array object to assign new key-value pair. Every array object has destinationId (you can see on the image), and I have a method that searches for the name by that param.
I want to assign that new value to every object in foreach.
Code:
$posts = $this->entityManager->getRepository(Post::class)->getPage();
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$postsData['destinationName'] = $destinationName;
}
}
Error is:
Call to a member function getDestinationId() on string
This is very odd as this field has entity type defined as string and also when dumping
dump($post->getDestinationId());
I get: "107869901061558" which is string.
It's because you override you $postsData variable.
You need to use a different variable to store your $destinationName like this :
$posts = $this->entityManager->getRepository(Post::class)->getPage();
$destinationNames = [];
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$destinationNames[$post->getId()] = $destinationName;
}
}
Like this, you could send $destinationNames to your template and find the right $destinationName thanks to the index.
i want return data if a < b then a = b, if using foreach i have to make empty array first and parsing data to te empty array, how if record i much, that take time
public function getDepreciation(){
$values = $this->repository->getDepreciation();
$data = [];
foreach($values as $value){
if($value->depresiation_per_month > $value->balance_value){
$value->depresiation_per_month = $value->balance_value;
}
array_push($data,$value->depresiation_per_month,$value->balance_value);
}
return $data;
}
is there is simple code than my above, since i have to make relist many array
You can convert this array in a Collection then handle it easier by using the transform method.
First you have to create the collection using your array Create Collection
$values = collect($values);
Then you can iterate over the collection using the transform method and apply your logic Transform Method
Finally you have to convert your collection into an array using the toArray method toArray Method
I hope this works for you
I have a column that contains a json array, but I am having difficulty outputting them as individual items.
ListingController#show
public function show(Listing $listing)
{
$services = collect($listing->services_offered);
return view('listings.listing', compact('banner', 'listing', 'services'));
}
listing.blade.php
#foreach($services as $service)
<li>{{$service}}</li>
#endforeach
Output
In your Listing model you can cast the json array into a PHP array like this:
protected $casts = [
'services_offered' => 'array',
];
Then you can wrap that into a collection as you already do and list them on the view.
You need to convert the JSON string to actual JSON array.
$services = collect(json_decode($listing->services_offered, true));
or you can create an accessor in your model
public function getServicesOfferedAttribute($value)
{
return json_decode($value);
}
In laravel the get() method over the DB returns an array of stdObjs,
for example this one:
$designers = DB::table('designers')->get();
Will return all designers from the designers table as an array of stdObj.
Now I want to manually create an array of stdObjs using something like this:
public function index()
{
$id = Auth::id();
$user = User::find($id);
foreach ($user->owned_item as $i) {
$result = new \stdClass();
$result->type => $i->Item_type->type,
$result->color => $i->Item_color->color
}
obviously that code will overwrite the $result obj at every cycle, the question is, how do I create an array of all the stdobjs I need?
You need to assign them to an array before the loop execution ends for each item.
$objs = [];
foreach (...) {
$obj = new \stdClass();
...
$objs[] = $obj;
}
After the loop the $objs variable contains an array containing the objects created inside the foreach loop.
EDIT: if you want to set specific array keys for each object, you can use
$objs[$key_here] = $obj;
when inserting the objects to the array. $key_here should be a string or an integer.
I have an array that I access like this:
$item['id'];
What can I do to the array to access it like this instead?
$item->id
Use this code:
$item = (object) $item;
echo $item->property;
The -> syntax is for objects, not associative arrays. You can use the (object) cast operator to cast an array into an object of the class stdClass though.
Cast it to (a stdClass) object:
$item = (object) $item;
If that array is coming from a database, such as mysql you can fetch objects instead of arrays with mysql_fetch_object() or set the flag PDO::FETCH_OBJ if you are using PDO.
Maybe it is not relevant to you however ...
You need to convert it into an object
$item = (object) $item;
echo $item->id;