Accessing items in array shared using Laravel View::share - php

I have this in my routes (only place it works...)
$constants = DB::table('constants')->get();
View::share('constants', $constants);
If in one of my views I var_dump constants I get this:
array(1) { [0]=> object(stdClass)#129 (3) { ["id"]=> string(1) "1" ["type"]=> string(13) "business_name" ["value"]=> string(17) "Project Framework" } }
I then want to do:
$business_name = $constants->business_name;
or
$business_name = $constants['business_name'];
How can I manipulate my array from the DB call to be able to use the constants variable like this?
Is there a way in Laravel to do this simpler / more efficient than looping through in a foreach and creating the array?

Assuming you have only one row in your constants you should use first() to retrieve a single record and not a collection:
$constants = DB::table('constants')->first();
Then you get a single object so you can use this notation to access the properties:
$business_name = $constants->business_name;
Edit
With your type-value structure I suggest using lists:
$constants = DB::table('constants')->lists('value', 'type');
This will create an array like this:
[
'business_name' => 'Project Framework',
'foo_type' => 'bar_value'
]
Usage
$business_name = $constants['business_name'];

Related

Does PHP allow to have duplicate properties inside an stdObject?

I'm stuck with a very weird bug. I have an object called $row that looks like this:
stdClass Object
(
[title] => Some Title
[body] => My body
[topic] => Topic
[dataType] => Survey
[csvrownum] => 1
)
I'm just trying to print out the title property in the following way:
print_r($row->title);
However for some reason that doesn't output anything.
Then I've tried to manually set the title property and print it right after, something like this:
$row->title = 'My Title';
print_r($row->title);
Surprisingly it worked but why? To make this more strange I decided to var_dump the object after set the title variable by hand:
$row->title = 'My Title';
var_dump($row);
And this is what I've got:
class stdClass#391 (6) {
public $title =>
string(3) "Some title"
public $body =>
string(7) "My body"
public $topic =>
string(6) "Topic"
public $dataType =>
string(17) "Survey"
public $csvrownum =>
int(1)
public $title =>
string(8) "My title"
}
Notice the title key is duplicated with different values. Is there any condition under this could happen?
No, PHP does not allow an object to have duplicate property names, because objects in PHP are implemented just like arrays. They are both implemented as ordered hashmaps. In a hashmap, two things that have the same hash, overwrite each other.
You likely just have unprintible characters in your object property name. You can see this more clearly by doing something like the following for debug purposes...
foreach($row as $key => $value) {
var_dump($key);
}
If we had an object like this, for example, you'd see it gets overwritten.
$row = new stdClass;
$row->title = "First";
$row->title = "Second";
But something like this might be more deceptive...
$row = new stdClass;
$row->{"title\0"} = "First";
$row->title = "Second";
Output from the foreach using var_dump on the key, would reveal this...
string(6) "title"
string(5) "title"
Notice one is string of length 6 and the other is a string of length 5.
Grain of salt
It's always better to use var_dump when attempting to debug variables than using something like print_r, as var_dump was specifically designed for debug purposes, whereas print_r is just a recursive print (hence the name). Printing values like null, false, or empty strings, gives you no useful information for debug purposes, but var_dump does.

PHP Array to Object

Given the following array:
$array = array(
'item_1' => array(
'item_1_1' => array(
'item_1_1_1' => 'Hello',
),
'item_1_2' => 'World',
),
'item_2' => array(),
);
How can I convert that into an Object?
Option 1
$obj = (object) $array;
Or
Option 2
$object = json_decode(json_encode($array), FALSE);
Or something else?
I would like to know the difference in the output between the 2 option and understand the best practice for creating this conversion.
Well you are answering somehow your own question, but if you want to have an object with the attributes like your array you have to cast it, this way an array will remain an array
$obj = (object) $array;
OUTPUT:
object(stdClass)#1 (2) {
["item_1"]=>
array(2) {
["item_1_1"]=>
array(1) {
["item_1_1_1"]=>
string(5) "Hello"
}
["item_1_2"]=>
string(5) "World"
}
["item_2"]=>
array(0) {
}
}
if you are using the json_decode version it will convert arrays to objects too:
object(stdClass)#2 (2) {
["item_1"]=>
object(stdClass)#3 (2) {
["item_1_1"]=>
object(stdClass)#4 (1) {
["item_1_1_1"]=>
string(5) "Hello"
}
["item_1_2"]=>
string(5) "World"
}
["item_2"]=>
array(0) {
}
}
NOTE: just the empty array will be an array here.
To Answer your question: The best practice depends on what YOU need.
It depends, really: if you are working on data that might be an array in one case, and an object the next, it would probably be best to use the json_decode trick, simply because unlike a cast, its result is "recursive". There is one very important thing to keep in mind here, though: numeric indexes can, and probably will cause problems for you at some point in time. Take a look at this bug report
This is documented here, but not in a way that really stands out:
If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible;
Exampe of the problem:
$data = [
'foo' => 'bar',
123 => 'all is well',
];
$obj = json_decode(json_encode($data));
var_dump($obj->foo);//bar
var_dump($obj->{123});//all is well
$cast = (array) $obj;
var_dump($cast);//shows both keys
var_dump(isset($cast[123]));//FALSE!!!
var_dump(isset($cast['123']));//FALSE
Basically: If you start converting arrays to objects and back again, numeric keys are not reliable anymore. If I were you, I'd simply change the code that is passing the data where possible, or I'd create a value object that can be set using an array or an object, and normalize the data that way.

return passed array keys and values

I am working in WordPress. I am using a plugin to get the admin options. The plugin takes an argument as an ID to get the value from the database like so.
$option = ot_get_option('email_address');
The above line of code would return
myEmail#example.com
I want to write a helper function that would get multiple values at once. Normally, I would get the options like this.
$option_1 = ot_get_option('option1');
$option_2 = ot_get_option('option2');
$option_3 = ot_get_option('option3');
I figured there could be a better way that would look a little nicer. I put together this little function that does work
function ritual_get_options($arg_list = array())
{
$options = array();
foreach( $arg_list as $key => $value){
$options[] = ot_get_option($value, array());
}
return $options;
}
using the function above, I can now pass the id's of the options like so
ritual_get_options('option1','option2','option3');
My issue is that the above will return an array with numeric keys. 0,1,2. Here is the exact array that is getting returned when I do a var_dump on the front end
[0]=>
string(16) "100 Main Street,"
[1]=>
string(18) "Hamilton, Ontario."
[2]=>
string(15) "+1 800 999 9898"
[3]=>
string(19) "mail#yourdomain.com"
I want to return the array keys with the value so that I can easily figure out what value is in what key. Here is the line that I used to get the above array
$options = ritual_get_options(array('number' => 'streetnumber','street' => 'street','phone' => 'phone_number','email' => 'email'));
When I go to use the returned values, I want to be able to use the key so that I could do
echo $options['number'];
instead of doing
echo $options[0];
I have not been able to figure out how to return the keys passed in to the function and preserve them into through the return.
Set the option name as key while building the array:
foreach( $arg_list as $option ){
$options[$option] = ot_get_option( $option );
}
Btw, using extract() on the $options array will allow you to use e.g. $phone_number instead of $options['phone_number']

Understanding CodeIgniter objects and multidimensional arrays

I'm working on a project in CodeIgniter 2 and now I'm stuck on the most basic of concepts.
Model:
Get an object array from my table which contains all rows that match a specific value for a field named foo. There can be one or more rows that match. In this example, two rows.
public function get_sample($foo)
{
$query = $this->db->get_where('sample_db', array('foo' => $foo));
return $query->result();
}
Controller:
Assign the results and make output available to view.
public function view($foo)
{
$data['array'] = $this->sample_model->get_sample($foo);
$this->load->view('view', $data);
}
View:
echo var_dump($array); // for testing
echo $array[1]->text
var_dump() of $array:
array(2) {
[0]=> object(stdClass)#23 (4) {
["id"]=> string(1) "1"
["foo"]=> string(3) "bar"
["number"]=> string(4) "1234"
["text"]=> string(23) "This is content in 1234"
}
[1]=> object(stdClass)#24 (4) {
["id"]=> string(1) "2"
["foo"]=> string(3) "bar"
["number"]=> string(4) "9999"
["text"]=> string(23) "This is content in 9999"
}
}
The rendered ouput of echo $array[1]->text; is: This is content in 9999
And I understand how all that is working: $array[1]->text is the content of the text field in the array object with the index of 1, my second object.
However, I have a field called number and I want to access the object with a certain number and get its corresponding text value.
Example: How can I retrieve the value of text where the number is 9999? I cannot use $array[1]->text since I can never be sure of the object's position in the array. Something like $array['number' => '9999']->text, but I know that's not right. Maybe I need to loop through the array looking for a match?
This looks so simple yet everything I've tried has failed and resulted in various PHP errors. I've been studying the PHP manual here and here, but cannot seem to find anything about what I'm looking to do, or maybe I'm just misapplying what I'm reading. Any guidance is appreciated.
In addition to an answer using best practices following the MVC model, I'm hoping for a link to the proper page in the documentation, as well as pointing out any errors in my wording/terminology above.
EDIT:
This answer contains the actual code I used to solve my problem. Although, Yagi's answer was accepted because it put me in the right direction.
How about using this :
foreach ($array as $row) {
if ($row->number == '9999') {
echo $row->text;
}
}
Loop through the array, and find the number object value of 9999 and get the text
Why don't you just to a query and search for that number (or order by that number?)
Either way, what you need is a multidimensional array search function (that iterates through array of object and returns the found field value combo )
Something like this (put this in helper)
function multi_array_search($array, $field, $value)
{
$results = array();
if (is_array($array))
{
if ($array->$field == $value) //chek the filed against teh value, you can do addional check s(i.e. if field has been set)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, multi_array_search($subarray, $key, $value)); //recurisve serach
}
return $results;
}
//than in view or in controller, depending where you need it
$result = multi_array_search($array, "number", "9999");
var_dump($result) will return or whether number of foudn instances
array() {
[0]=> object(stdClass)#24 (4) {
["id"]=> string(1) "2"
["foo"]=> string(3) "bar"
["number"]=> string(4) "9999"
["text"]=> string(23) "This is content in 9999"
}
}
you might be looking for something like this...
array_filter($array, function($o){ return $o->number == 9999; } );
http://php.net/manual/en/function.array-filter.php
EDIT
$o is the parameter in the callback function. each element of the array is passed to the callback function. if the function returns false, the element will be filtered out.
$arr = array(
(object) array(
'num' => 33,
'text' => 'hello',
),
(object) array(
'num' => 44,
'text' => 'world',
),
);
$filtered = array_filter($arr, function($o){ return $o->num == 33; });
echo $filtered[0]->text; // hello
as you stated, index will remain, so use array_values or array_shift. here's an example with function
function get_by_num($arr, $num){
return array_shift(array_filter($arr, function($o) use ($num) { return $o->num == $num; }));
}
$obj = get_by_num($arr, 44);
var_dump($obj);
// object(stdClass)#2 (2) { ["num"]=> int(44) ["text"]=> string(5) "world" }
in this case $obj will be either NULL if element is not found, or the first match. $num is transferred along so you can use any value. you can even improve it:
function get_first_match($arr, $field, $val){
return array_shift(array_filter($arr, function($o) use ($field, $val) { return $o->{$field} == $val; }));
}
$obj = get_first_match($arr, 'num', 44);
and now you can search any field. in your case
$obj = get_first_match($array, 'number', 9999);
Thanks to Yagi's answer, I came up with the following very simple solution. I loop through the array and assign the value of text to a new array with an index that matches the value of number. Then in the view file, I can access the text value based on this index.
Controller:
public function view($foo)
{
$data['array'] = $this->sample_model->get_sample($foo);
foreach ($data['array'] as $row) {
$data['result'][$row->number] = $row->text;
};
$this->load->view('view', $data);
}
View:
if (isset($result[9999])) echo $result[9999];

Is Possible Custom Select Field using ORM on Fuelphp?

I have a problem when I want to query table using ORM ,example I have article table with field id,author,text.
My code like this :
// Single where
$article = Model_Article::find()->where('id', 4);
print_r($article);
that't code will be fetch all field on table article, it's like select * from article where id = 4
Try Possibility
$article = Model_Article::find(null, array('id','title'))->where('id', 3);
the response is
object(Orm\Query)#89 (14) {
["model":protected]=>
string(10) "Model_Article"
["connection":protected]=>
NULL
["view":protected]=>
NULL
["alias":protected]=>
string(2) "t0"
["relations":protected]=>
array(0) {
}
["joins":protected]=>
array(0) {
}
["select":protected]=>
array(1) {
["t0_c0"]=>
string(5) "t0.id"
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["rows_limit":protected]=>
NULL
["rows_offset":protected]=>
NULL
["where":protected]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(9) "and_where"
[1]=>
array(3) {
[0]=>
string(5) "t0.id"
[1]=>
string(1) "="
[2]=>
int(3)
}
}
}
["order_by":protected]=>
array(0) {
}
["values":protected]=>
array(0) {
}
}
that's is not return id or title field.
but when i'm try by adding get_one() method
$article = Model_Article::find(null, array('id','title'))->where('id', 3)->get_one();
id is return , but title is not and another field, i don't know why ?
Reference
ORM Discussion FuelPHP it's say ORM currently will be select all column, no plans to change that at the moment.
My Problem
Select Custom Field using ORM like this select id,owner from article where id = 4 it's will be return only id & owner, Is Possible to get that using ORM on FUELPHP ?
Do not use
Model_Article::find()->
but use
Model_Article::query()->
The first one works but is considered an error situation which might change in future versions.
As of version 1.4 the ORM supports partial selects, using
Model::query()->select('id', 'value')->
The second parameter of find() is an array of conditions for the find, such as 'where' or 'order_by' clauses. There is no support for selecting column names in this array.
The Orm\Model fetches all column because it can't deal with incomplete / partials objects.
If you want a custom query, don't use the ORM, use the query builder for that.
DB::select('id','title')->from(Model_Article::table())->where('id', 4);
if you are trying to find out result like query "select * from article where id = 4"
$article = Model_Article::find()->where('id', 4)->get_one();
print_r($article);
and one more concept you should understand
get_one() returns only one record (as a object ).
get() returns multiple record ( in the form of array of objects ).
and if your field is not displaying then check the model Model_Article .this problem may be occur when you have not declared fields in properties list.
Fuelphp has been release new version , you can see this documentation , in my case fuelphp version is 1.2 and cannot select custom field using orm.
select custom field using orm is available since version 1.4
http://fuelphp.com/docs/packages/orm/crud.html
// Find only some columns
Model_Article::query()->select('id', 'name');
Model_Article::find('all', array('select' => array('id', 'name')));

Categories