I am using laravel 5.
I query dates from this query:
$data = DB::table('itemregistrations')
->select('itemregistrations.dd', 'itemregistrations.mm', 'itemregistrations.yy')
->where('itemregistrations.itemregistrationid', $id)
->get();
I want to get the value to put in an array so that i can implode the array into string. I cannot implode it in this format.
dd($data);
produce this:
Collection {#521 ▼
#items: array:1 [▼
0 => {#515 ▼
+"dd": 15
+"mm": "0"
+"yy": 2007
}
]
}
How to turn this result to:
0 => {15, 0, 2007}
Thank you
why don't you convert the collection to an array using toArray() method and then simply do the json_encode on the array you got.
later on you can even pass it in your response.
Hope it helps other..
I find solution guided by those comments from this question here:
How do I implode a json object to a string in php?
I modify my code:
//query tarikh masuk
$datatarikh = DB::table('itemregistrations')
->select('itemregistrations.dd', 'itemregistrations.mm', 'itemregistrations.yy')
->where('itemregistrations.itemregistrationid', $id)
->get();
//**************** format the value of the date************************
$data = json_decode($datatarikh, true);
$val_arr = array();
foreach($data as $val)
{
foreach($val as $key => $value)
{
$val_arr[] = $value;
}
}
$result = implode("-", $val_arr);
It changes associative array to indexed array to be imploded. Thanks for your input.
From an array:
array:1 [▼
0 => array:3 [▼
"pdrm_dd" => 15
"pdrm_mm" => "01"
"pdrm_yy" => 2007
]
]
The output result:
"15-01-2007"
Related
i have value array like this
^ array:2 [▼
0 => "random value1<tr>random string1</tr>random endvalue1"
1 => "random value2<tr>random string2</tr>random endvalue2"
)
]
how do i change the value of the dimensional array above to be like below
^ array:2 [▼
0 => "random string1"
1 => "random string2"
)
]
im trying using
$array = array_map(function($key) {
$exp = explode('<tr>', $tes);
//$exp1 = explode('</tr>', exp[1]); -from this line3 my explode cant access array
return exp;
}, $myarray);
dd($array);
my array in line3 become like this
^ array:2 [▼
0 => array:2 [▼
0 => "random value1"
1 => "random string1</tr>random endvalue1"
]
1 => array:2 [▼
0 => "random value2"
1 => "random string2</tr>random endvalue2"
]
)
]
how to improve conditions like this, or have another way that is better ?
$data = [
"random value1<tr>random string1</tr>random endvalue1",
"random value2<tr>random string2</tr>random endvalue2",
];
$trim = collect($data)
->map(function (string $value) {
preg_match('/(?<=<tr>)(.*)(?=<\/tr>)/', $value, $match);
return empty($match) ? $value : $match[1];
})
->toArray();
Could be done this way, using the framework collect method to map through the values & extract the string you want.
This is how I create my array fields:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = (array) $page;
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
The output is:
array:3 [▼
"\x00App\Entity\Pages\x00id" => 3
"\x00App\Entity\Pages\x00name" => "cat"
"\x00App\Entity\Pages\x00color" => ""
]
But I actually need this result:
array:3 [▼
"id" => 3
"name" => "cat"
"color" => ""
]
According to the suggestions I made this change:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getManager()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = get_object_vars($page);
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
But this outputs me an empty array.
You have two options:
1. Use Query::HYDRATE_ARRAY instead of findOneBy()
$query = $this->getDoctrine()
->getRepository(Pages:class)
->createQueryBuilder('p')
->getQuery();
$result = $query->getResult(Query::HYDRATE_ARRAY);
(stolen from this answer)
2. Use a Serializer
Use the Serializer Component or JMSSerializerBundle to serialize your entity object.
I met some difficulties when I was trying to get value from an array in my php blade.
It has rather clear structure (printed with dd function)
{{dd($attr)}}
array:4 [▼
"id" => "215"
"type" => "select"
"name" => "Status"
"value" => array:2 [▼
"pred" => array:3 [▼
0 => "Employed"
1 => "On vacation"
2 => "Dismissed"
]
"sel_val" => "0"
]
]
And when I want to get a value by key 'sel_val' or 'pred'
print_r($attr['value']['pred']);
it gives me Illegal string offset 'pred'
And it works nice in Controller. What should i do?
It gives that error because pred is also an array. you'll have to do $attr['value']['pred'][0] to get Employed, $attr['value']['pred'][1] to get On vacation, $attr['value']['pred'][2] to get Dismissed and $attr['value']['sel_val'] to get the value of sel_val which is 0 in this case. Hope this helps.
working fine when we send array in compact function in the controller
$record = array('id' => '215', 'type' => 'select', 'value' => array('pred' => array('0'=> 'Employed', '1' => 'On vacation', '2' => 'Dismissed'),'sel_val' => '0'));
return view('home', compact('record'));
I have write that code but i want take an array of this, is this possible?
$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get();
To get laravel query builder result in array check this:
$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get()->toArray();
Or
If you prefer to use Query Builder instead of Eloquent here is the solutions
$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get();
First Solution
$array = (array) $result;
Second Solution
$array = get_object_vars($result);
Third Solution
$array = json_decode(json_encode($result), true);
hope it may help
You can see from the API docs that the get() function returns either a Collection or a Builder.
Laravel collections have the toArray() method (described in the docs) as shown below:
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
/*
[
['name' => 'Desk', 'price' => 200],
]
*/
Therefore, you can do the following:
$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get()->toArray();
i am trying to create an Array in yii2 in following format
[
['id'=>'2', 'name'=>'ABC'],
['id'=>'3', 'name'=>'DEF']
]
So far i have tried to use ArrayHelper class but if i map using it than it removes the key part from the array,i need to create the array in exact this format
$data = User::find()
->where(['id' => $id])
->orderBy('id DESC')
->all();
As you know after this code i get the data in ActiveRecord format so i need to convert them to id and name array only,i dont need any other data.
I tried doing this
$data2 = ArrayHelper::map($data, 'id', 'name');
But returns the data in following format
[
['2'=>'ABC'],
['3'=>'DEF']
]
I have tried this as well
$data = MainCategory::find()->select('id,name')
->where(['id' => $id])
->orderBy('id DESC')
->asArray()->all();
But its returning the array in following format
[
'0' =>
[
'id'=>'2',
'name'=>'ABC'
],
'1' =>
[
'id'=>'3',
'name'=>'DEF'
]
]
How can i achive key and value format in yii2?
You can fetch data as array like this:
$data = User::find()
->select('id, name')
->where(['id' => $id])
->orderBy('id DESC')
->asArray()
->all();
To create plain KEY => VALUE array use column() method:
$data = User::find()
->select(['name', 'id'])
->where(['id' => $id])
->orderBy('id DESC')
->asArray()
->indexBy('id')
->column();
First selected column [name] will be returned as a VALUE.