This question already has answers here:
Create new variables from array keys in PHP
(5 answers)
Closed 4 years ago.
Is there a way to pass all array's elements to variables with the elements' names?
For example if I have the following array:
$test['name']
$test['phone']
$test['address']
etc..
I want a shortcut for typing this:
$name = $test['name'];
$phone = $test['phone'];
$address = $test['address'];
etc..
Sure you can use $$
$test['name'];
$test['phone'];
$test['address'];
$test['name'] = "John";
$test['phone'] = "987987987";
$test['address'] = "Asheville";
foreach($test as $key=>$val){
$$key = $test[$key];
}
echo $phone;
Related
This question already has answers here:
variable variables
(5 answers)
Closed 1 year ago.
how to get variables from column names
$sq = "select * from arts where id = :aid limit 1";
$st = $db->prepare($sq);
$st->execute([":aid" => $id]);
$row = $st->fetch();
now, instead of:
$cat = $row['cat'];
$title = $row['title'];
$subtitle = $row['subtitle'];
... so on
I need something like:
foreach($column_name as $el){
$var_name = $el;
}
There's rarely a good reason to do this, just use the array. However, you can use variable variables from the keys if there is only one row as you show:
foreach($row as $key => $val){
$$key = $val;
}
There is also extract() but may be even worse practice.
This question already has answers here:
php multi-dimensional array remove duplicate [duplicate]
(6 answers)
Closed 3 years ago.
I have an array who have another one inside, but when i get the response from database i have duplicates, and i don't want to have this, any help?
I have used the solution provided in the link and it doesn't work
"Warning: Illegal offset type in"
$atividadesArray = array();
foreach ($result as $row) {
$idAtividade = $row['idAtividade'];
if (!isset($atividadesArray[$idAtividade])) {
$atividadesArray[$idAtividade]['idAtividade'] = $row['idAtividade'];
$atividadesArray[$idAtividade]['Periodo'] = $row['Periodo'];
$atividadesArray[$idAtividade]['Mes'] = $row['Mes'];
$atividadesArray[$idAtividade]['haveClasses'] = $row['haveClasses'];
$atividadesArray[$idAtividade]['Destinatarios'] = $row['Destinatarios'];
$atividadesArray[$idAtividade]['Nome'] = array();
$atividadesArray[$idAtividade]['Grupo'] = array();
$atividadesArray[$idAtividade]['Departamento'] = array();
}
$atividadesArray[$idAtividade]['Nome'][] = $row['Nome'];
$atividadesArray[$idAtividade]['Grupo'][] = $row['Grupo'];
$atividadesArray[$idAtividade]['Departamento'][] = $row['Departamento'];
}
foreach ($atividadesArray as $idAtividade => $t ) {
$json[]=$t;
}
echo json_encode($json);
Just do not add the item, if it already exists in the array:
...
// Search for the element in the array
if (array_search($row['Departamento'], $atividadesArray[$idAtividade]['Departamento']) === false) {
// Add only if nothing found
$atividadesArray[$idAtividade]['Departamento'][] = $row['Departamento'];
}
Note === operator is required to not mess element with key 0 (the first added) and false boolean value;
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
i have in my row an array
when
i go this result
{"type":"person1","id":"12"}
I want just to get the id as result value 12
Use json_decode:
$value = ' {"type":"person1","id":"12"}';
$result = json_decode($value, true);
echo $result['id']; // 12
$addedBy = json_decode($row['added_by']);
$id = $addedBy['id'];
This question already has answers here:
Creating an array from a MySQL table
(2 answers)
Closed 10 years ago.
I am using PHPlot to make a graph.
I have an issue in generating the array from a MySQL table.
Basivally, I want to array is as follows:
$values = array($arrayx);
array('a',-3),
array('b',5),
array('c',7),
array('d',8),
array('e',12),
array('f',-6),
//);
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
Part of the code where I tried to retrieve values from table to feed the array
$query="SELECT * FROM tasks";
$result=mysql_query($query);
//using a for loop to add values to the array
while ($resource=mysql_fetch_array($result)){
$thedate = $resource["date"];
$title = $resource2["title"];
$innerarray = "array('.$thedate.', $title),";
}
$values = array($innerarray).");";
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
//Draw it
$graph->DrawGraph();
}
The way I'm doing the $innerarray and $values seems wrong. Can you please help me fix it?
Thank you
try replacing
$innerarray = "array('.$thedate.', $title),";
with
$innerarray = array($thedate, $title);
$new = array();
while(for condition ){
$new[] = '\''.thedate[$i].''\','.$title[$i].'\';
}
var_dump($new);
this an idea, you need to edit the code to make it working
I assume it is this that you want:
$sql="SELECT datefield, titlefield FROM tasks";
....
while (list($thedate,$thetitle) = mysql_fetch_array($result)) {
$values[] = array($thedate,$thetitle);
}
echo $values[0][0]; // will output your 1st date
echo $values[0][1]; // will output your 1st title
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Appending a value of a variable to a variable name?
I can't figure out the syntax at all and have searched far and wide.
I would like to do this:
$uni = "ntu";
$selectedntu = "something";
echo $selected$uni;
// output should be the same as
echo $selectedntu;
In other words I'd like to use the contents of the second variable $uni to join onto the first variable's name. $selectedntu has been set with a foreach loop, but I can't figure out how to reference the two variables together in php.
Construct the string and use a variable variable $$
$uni = "ntu";
$selected = "something";
$new_variable = $selected . $uni;
echo $$new_variable;
// Or..per your googleing..purely for reference for others later
$uni = "ntu";
$selected = "something";
echo ${$selected . $uni};