I have multidimensional array as below where cid as an option and values are defined as its attributes. Such array is generating based on selection from O1, O2, O3 and so on.
Array
(
[0] => Array
(
[cid] => O1
[values] => Array
(
[0] => O1A1
[1] => O1A2
)
)
[1] => Array
(
[cid] => O2
[values] => Array
(
[0] => O2A1
[1] => O2A2
[2] => O2A3
)
)
)
Now I need to generate a matrix in HTML table as below:
+--------+------------+-----------------------------+
| O1 | O2 | Some other columns |
+--------+------------+-----------------------------+
| O1A1 | O2A1 | Some other column values |
| O1A1 | O2A2 | Some other column values |
| O1A1 | O2A3 | Some other column values |
| O1A2 | O2A1 | Some other column values |
| O1A2 | O2A2 | Some other column values |
| O1A2 | O2A3 | Some other column values |
+--------+------------+-----------------------------+
There are total 6 rows ( creating like 2 X 3 (option O1 and O2 values)). I have tried with nested for..loop but array have dynamic values ( it is based on selection).
Thanks in advance for any help.
Here is the solution :)
<?php
$input = array(0 => array('cid' => 'O1', 'values' => array (0 => 'O1A1',1 => 'O1A2')), 1 => array('cid' => 'O2', 'values' => array (0 => 'O2A1', 1 => 'O2A2', 2=>'O2A3')));
echo "<pre>"; print_r($input);
///make an array of matrix
$matrixArr =array();
for( $i= 0; $i< 2; $i++ )
{
for( $j=0; $j <3; $j++ )
{
$matrixArr[$input[0]['values'][$i]][]= $input[1]['values'][$j];
}
}
echo "<pre>"; print_r($matrixArr);
///now print that array
foreach($matrixArr as $key =>$val)
{
foreach($val as $v)
{
echo $key." | ".$v.' | Some other column values </br>';
}
?>
you will get this
O1A1 | O2A1 | Some other column values
O1A1 | O2A2 | Some other column values
O1A1 | O2A3 | Some other column values
O1A2 | O2A1 | Some other column values
O1A2 | O2A2 | Some other column values
O1A2 | O2A3 | Some other column values
Sir, check this one, if it is helpful for you ...
<?php
$input = array(
0 => array(
'cid' => 'O1',
'values' => array (0 => 'O1A1',1 => 'O1A2', 2=>'01A3',3=>'01A4')),
1 => array(
'cid' => 'O2',
'values' => array (0 => 'O2A1', 1 => 'O2A2', 2=>'O2A3',3=>'O2A4',4=>'O2A5'))
);
$matrixArr =array();
for( $i= 0; $i < count($input[0]['values']); $i++ )
{
for( $j=0; $j < count($input[1]['values']); $j++ )
{
$matrixArr[$input[0]['values'][$i]][]= $input[1]['values'][$j];
}
}
foreach($matrixArr as $key =>$val)
{
foreach($val as $v)
{
echo $key." | ".$v.' | Some other column values </br>';
}
}
?>
Related
I need help.
I have data with commas (,). And I want to convert it to array or json.
But when looping through it, the data in the first and last elements is duplicated.
Data from database on column additional. with NULL as default.
example:
+----+------------+
| id | addtional |
+----+------------+
| 1 | ada |
| 2 | NULL |
| 3 | kursi,meja |
| 4 | NULL |
+----+------------+
then call for looping
if( $p->additional !== NULL ) {
$gmb = explode(',', $p->additional);
$gmbbb = array();
foreach ($gmb as $gbr) {
$gmbbb[] = $gbr;
}
$keterangan['i'] = $gmbbb;
}
And the final result from $keterangan is:
Array
(
[i] => Array
(
[0] => ada
)
)
Array
(
[i] => Array
(
[0] => ada
)
)
Array
(
[i] => Array
(
[0] => kursi
[1] => meja
)
)
Array
(
[i] => Array
(
[0] => kursi
[1] => meja
)
)
How to stop duplicate if it's NULL, I need data from additional when it's NOT NULL.
This is different approach for you.Hope this will help you.But i dont know your table name and all.Make the sql query like below:
mysql>SELECT addtional FROM table_name WHERE addtional IS NOT NULL;
two tables Location and Routes. Location table contains following fields and values
id | name
1 | bangalore
2 | mumbai
3 | kolkatta
4 | delhi
Routes table contains following fields and values
id | source | desination
1 | 1 | 4
2 | 1 | 2
3 | 1 | 3
4 | 2 | 4
5 | 2 | 3
6 | 3 | 4
want to find all possible routes from source to destination like
bangalore-delhi
bangalore-mumbai-delhi
bangalore-mumbai-kolkatta-delhi
bangalore-kolkatta-delhi
please help me to achieve this result
Using your existing data from your two tables, you could LEFT JOIN columns source and destination from Routes meaning your INT values will match up to the Location.name column. Then make sure you order the data by source column for so it's easier to sort with PHP later on.
MySQL:
SELECT locStart.name as start, locFinish.name as finish FROM `routes`
LEFT JOIN location as locStart
ON routes.source = locStart.id
LEFT JOIN location as locFinish
ON routes.destination = locFinish.id
ORDER BY routes.source
Output:
array (
0 =>
array (
'start' => ' bangalore',
'finish' => 'delhi',
),
1 =>
array (
'start' => ' bangalore',
'finish' => 'mumbai',
),
2 =>
array (
'start' => ' bangalore',
'finish' => 'kolkatta',
),
//Etc....
Then add your DB results into multidimensional array using the Source name as the key.
$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$newArr;
foreach ($result as $v) {
$newArr[$v['start']][] = $v['finish'];
}
Output:
array (
' bangalore' =>
array (
0 => 'delhi',
1 => 'mumbai',
2 => 'kolkatta',
),
'mumbai' =>
array (
0 => 'delhi',
1 => 'kolkatta',
),
'kolkatta' =>
array (
0 => 'delhi',
),
)
For outputting the new array, you can use a recursive function:
function recurse($newArr, $key=NULL) {
if($key !== NULL) {
foreach ($newArr[$key] as $k=>$v) {
echo '<li>Destination: ' . $v . '</li>';
}
echo '</ul>';
} else {
foreach ($newArr as $k=>$v) {
echo 'Source: ' . $k . '<br><ul>';
recurse($newArr, $k);
}
}
}
Output:
Source: bangalore
Destination: delhi
Destination: mumbai
Destination: kolkatta
Source: mumbai
Destination: delhi
Destination: kolkatta
Source: kolkatta
Destination: delhi
Using the query builder to try and select rows where a value in the HSTORE key $type is present.
My Where Statement:
->where("meta_data_fields->'$type'", 'like', '%'.$query.'%')
Not sure what I'm doing wrong. I get this error:
Undefined column: 7 ERROR: column "meta_data_fields->'Entity::hstore'" does not exist
Any ideas?
to get all records in table that have HSTORE
Model::where("meta_data_fields", 'like', '%'.$query.'%')->get();
This should do normally do the trick. In your case, data in meta_data_fields is a json string. if the search is performed on all rows in the table, will select all. So here is What I did,
I created a queryScope in the model
1 - fetch all data
2 - extract meta_data_field and decode it
3 - loop through meta_data_field
4 - select only that match the criteria and build the array
here is a table
+----+-------------+------------------------------------+-----------+
| id | name | desc | vendor_id |
+----+-------------+------------------------------------+-----------+
| 1 | apples | {"type":"fruit","origin":"mexico"} | 1 |
| 2 | oranges | {"type":"fruit","origin":"peru"} | 1 |
| 3 | Peaches | {"type":"fruit","origin":"mexico"} | 2 |
| 4 | Cherries | {"type":"fruit","origin":"us"} | 1 |
| 5 | banans | {"type":"fruit","origin":"brazil"} | NULL |
| 6 | Water Melon | {"type":"fruit","origin":"mexico"} | 1 |
+----+-------------+------------------------------------+-----------+
public function scopeItems($name ="mexico"){
$items = array();
$data = Self::get();
$meta_fields = json_decode($data -> pluck('desc') ,true);
foreach ($meta_fields as $key => $value) {
if (isset($value['origin']) && $value['origin'] == $name ){
$items[$key] = $data[$key] -> toArray();
}
}
return $items;
}
// output
Array
(
[0] => Array
(
[id] => 1
[name] => apples
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Peaches
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 2
)
[5] => Array
(
[id] => 6
[name] => Water Melon
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
)
Hope this works for you this time.
So here's what worked in my situation with Laravel.
$result = \DB::select(\DB::raw("SELECT hstore_fields, id from table where lower(hstore_fields->'$type') like lower('%$query%')"));
Please can I have some help on this. I'm trying to delete SQL row in a table that are not match the data in an array.
SQL Table:
| id | Name | No
------------------
| 1 | john | 14
| 2 | rui | 156
| 3 | ted | 148
| 4 | alex | 123
| 5 | depay | 56
Array:
Array
(
[0] => Array
(
[name] => john
[no] => 14
)
[1] => Array
(
[name] => ted
[no] => 148
)
[2] => Array
(
[name] => depay
[no] => 56
)
)
This may help you
[akshay#localhost tmp]$ cat test.php
<?php
$array = array (
array (
'name' => 'john',
'no' => '14'
),
array (
'name' => 'ted',
'no' => '148'
),
array (
'name' => 'depay',
'no' => '56'
)
);
$table = "some_table";
$sql = "DELETE FROM ".$table." WHERE (NAME,NO) NOT IN (". implode(",",array_map(function($_){ return sprintf("('%s',%d)",$_['name'],$_['no']);},$array)).")";
print $sql."\n";
?>
Output
[akshay#localhost tmp]$ php test.php
DELETE FROM some_table WHERE (NAME,NO) NOT IN (('john',14),('ted',148),('depay',56))
It would be much easier if you can return "id" aka primary key of the table as array. In that case you can just grab those ids and during delete operation exclude those from the equation.
$selectQ = 'select id from table_name'; // and no>10
$result = mysql_result($selectQ);
$ids = array();
while ($info = mysql_fetch_assoc($result)) {
$ids[] = $info['id'];
}
if($ids) {
$deleteQ = 'delete from table_name where id not in ('.implode(",", array_values($ids)).')';
mysql_query($deleteQ);
}
P.S : During select query make sure to include logic for finding those users. For example, who's "no" is greater than X.Otherwise this query in default will return all ids and eventually nothing will be deleted.
I have the array like
array(
[0] => array(
[a] => r1,
[b] => c1,
[c] => d1,
),
[1] => array(
[a] => r1,
[b] => c1,
[c] => d2,
),
[2] => array(
[a] => r1,
[b] => c1,
[c] => d3,
),
[3] => array(
[a] => r1,
[b] => c2,
[c] => d1,
),
[4] => array(
[a] => r1,
[b] => c2,
[c] => d3,
),
[5] => array(
[a] => r1,
[b] => c3,
[c] => d1,
)
)
Currently I am getting the output like
-------------------------------------
| C1,D1 | C1,D2 | C1,D3 |
-------------------------------------
| C2,D2 | C3,D1 | - |
-------------------------------------
| - | - | - |
-------------------------------------
But I need the output must be displayed by 3x3 matrix Like
-------------------------------------
| C1,D1 | C1,D2 | C1,D3 |
-------------------------------------
| - | C2,D2 | - |
-------------------------------------
| C3,D1 | - | - |
-------------------------------------
Please help me to fill the missing values with empty values
My code :
for($i=1; $i<=3; $i++){
for($j=1; $j<=3; $j++){
for($r=0; $r<9; $r++){
if(isset($rows[$r]) && $rows[$r]['b'] == 'C'.$i && $rows[$r]['c'] == 'D'.$j) {
//Store data to array
$data[] = array(
'a' => $rows[$r]['a'],
'b' => $rows[$r]['b'],
'c' => $rows[$r]['c']
);
}
}
}
}
Option => Fill the array with empty values then fill in the values you got.
Option => Remember which index positions you fill and then fill the rest by excluding the ones u filled.
Number 1 is easier.
Number 2 has better performance.
You can use array_fill
or do a loop through the array:
for( $i = 0 ; $i < MAX ; $i++ )
arr[i] = ""; // fill with whatever value you like