I have the code to show TripUUID. But it showed just one data. The code is as follows:
function list_of_tripreview(){
$data = $this->data;
$arr = [];
$data['list_of_tripreview'] = $this->cms_model->list_of_tripreview();
$TripUUID = $data['list_of_tripreview'][0]['TripUUID'];
echo json_encode($TripUUID);
This code gives the output TripUUID on index 0. How do I revise this program to show all index data without [0]?
You can use foreach loop
$data['list_of_tripreview'] = $this->cms_model->list_of_tripreview();
$datas = $data['list_of_tripreview'];
//$TripUUID = $data['list_of_tripreview'][0]['TripUUID'];
foreach($datas as $key => $item){
echo $item['TripUUID'].'<br>';
}
Related
$data = json_decode(file_get_contents("php://input"));
$Product=$data->Product;
$items = array();
print_r($items);
foreach($Product as $index => $value)
{
$Product1 = $Product[$index];
array_push($items, $Product1);
}
I need to push $Product1 value into the array for every iteration. How do I do this?
<?php
$data = json_decode(file_get_contents("php://input"),true);// ensure you don't get an std-class object
$Product=$data->Product;
$items = array();
//print_r($items); // you will always print an empty array if you put it here
foreach($Product as $index => $value)
{
$Product1 = $Product[$index];
echo $Product1; // for debug reasons
$items[]=$Product1;
}
echo '<pre>'; //just for the array format
print_r($items); //for debug reasons
This code should fill the array $items with the values you need. Read my comments as well please, maybe we could help more if you gave us the debugging results but still this code should work for you.
here is what I'm trying to do. I'm retrieving information from a database via array. What is happening is the information from the previous array is going into the next array.
Here is the code:
$i = 0;
foreach ($array_name as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$array_name[$i]['new_infroamtion'] = $data'
}
}
So right now based on the code data from the table is correctly going into the first array, however, information based from the first array is going into the second array..
Let me know if you need anymore information.
Thank you
You are using $array_name while you are iterating through $array_name. This is valid code if you want to do this, but I don't think you do. You need to change the second $array_name to something else.
$i = 0;
foreach (**$array_name** as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id'] as $key => $test_id {
$data = ModelClass::Information($test_id);
**$array_name**[$i]['new_infroamtion'] = $data
}
}
I did find a solution. What I had to do was add the following
$s = array()
Then in the for loop, I added the following code:
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$s[] = $data
$array_name[$i]['new_infroamtion'] = $s'
}
Simple question to which I don't have an answer.
How can I change my array from this:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
to this:
[{"sku":"6","buyers":"7","base":"8"}]
I have three queries for three different database tables:
$sku = DB::table('mapiranje')->select(DB::raw('count(*) as sku'))
->where('mate_fk', '=', NULL)
->get();
$kupac = DB::table('mapkupci')->select(DB::raw('count(*) as buyers'))
->where('kupci_fk', '=', NULL)
->get();
$base = DB::table('dist_base')->select(DB::raw('count(*) as base'))
->where('base_fk', '=', NULL)
->get();
now each returns:
[{"sku":"6"}]
[{"buyers":"6"}]
[{"base":"6"}]
I have used merge_array to make a single array, but I get:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
what I want is:
[{"sku":"6","buyers":"7", "base":"8"}]
Refactor your code according to right Laravel way:
$result = [
'sku' => DB::table('mapiranje')->whereNull('mate_fk')->count(),
'buyers' => DB::table('mapkupci')->whereNull('kupci_fk')->count(),
'base' => DB::table('dist_base')->whereNull('base_fk')->count()
];
$result = [];
foreach($input as $oneInputRow) {
$result[$oneInputRow[0]] = $oneInputRow[1];
}
$target = array();
$start = array(array("sku"=>"6"), "buyers"=>"7"), "base"=>"8"));
foreach($start as $sub){
foreach($sub as $key => $val){
$target[$key] = $val;
}
}
Not shure if laravel provides any special syntax, but just with php I'd do it as above.
Basicly you loop over the start-array. In that you loop over every array to get the key/val combination and put that into the target-array.
For the second loop there would be other ways if you only have one entry in every secondary array.
Please try below code
$dd = '[{"sku":"6"},{"buyers":"7"},{"base":"8"}]';
$data = json_decode($dd,true);
$result = array();
foreach($data as $key=>$value){
foreach($value as $key1=>$value1){
$result[$key1] = $value1;
}
}
echo json_encode($result); //this will print your required format result
function dp(){
$this->load->library('heart');
$times = 35;
$i = 0; while ($i<$times){
$this->heart->test();
//10 newest
$query = $this->db->get('test', 10, 20);
//set variables from query
foreach ($query->result() as $getrow)
{
$data1 = $getrow->data1;
$data2 = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $id){
$this->heart->go($id,$data2);
}
//increment $i
$i++;
}
}
Hey guys, here is my code. I am trying to get the most newest entries from the database, then setting them as variables. For each variable, I am trying to call a function to it passing $id (data1) and $data2. Will data2 be passed or do I need to do something like $data1 as $id && $data2 as $id2. I need to pass over $data1 and $data2 to go() which should be different everytime.
The problem here is I keep getting 'Invalid argument supplied for foreach()' whenever I try to run the code. Anyone know what the issue is here?
Thank you in advance.
I believe you actually want to be creating arrays of $data1 and $data2.
Currently you are overwriting the values in each of those variable each time through the first foreach loop.
$data1 = array();
$data2 = array();
//set variables from query
foreach ($query->result() as $getrow)
{
$data1[] = $getrow->data1;
$data2[] = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $key => $id){
$this->heart->go($id,$data2[$key]);
}
Alternatively you could use data1 and data2 as the key/value pair of the array
$data = array();
foreach ($query->result() as $getrow)
{
$data[$getrow->data1] = $getrow->data2;
}
//for each data1; do go();
foreach ($data as $key => $value){
$this->heart->go($key, $value);
}
You can do it all with a single foreach loop without building unnecessary data arrays:
foreach($query->result() as $getrow) {
$this->heart->go($getrow->data1, $getrow->data2);
}
Basically, this is what I currently use in an included file:
$sites[0]['url'] = "http://example0.com";
$sites[1]['url'] = "http://example1.com";
$sites[2]['url'] = "http://example2.com";
$sites[3]['url'] = "http://example3.com";
$sites[4]['url'] = "http://example4.com";
$sites[5]['url'] = "http://example5.com";
And so I output it like so:
foreach($sites as $s)
But I want to make it easier to manage via a MySQL database. So my question is, how can I make it automatically add additional "$sites[x]['url'] = "http://examplex.com";" and output it appropriately from my MySQL table?
You can use array_map() for this:
$newSites = array_map('pick_attribute', $sites, 'url');
function pick_attributes($val, $prop) {
return $val[$prop];
}
or a simple loop:
$newSites = array();
foreach ($sites as $v) {
$newSites[] = $v['url'];
}