foreach(get_field('color') as $singleColorCP2) {
$new_fields = get_fields($singleColorCP2->ID);
foreach($new_fields as $name => $value)
echo $value;
}
this is my code and it is running it prints like,
firstfield_valueasecondfield_valuea<br>
firstfield_valuebsecondfield_valueb<br>
firstfield_valuecsecondfield_valuec<br>
I'm getting crazy how to print just:
firstfield_valuea
firstfield_valueb
firstfield_valuec
I'm interested only in one column.
("name" prints the correct name of the field, but still with the same problem, in couple with the second field, I can't divide)
foreach(get_field('color') as $singleColorCP2) {
$new_fields = get_fields($singleColorCP2->ID);
foreach($new_fields as $name => $value)
if($name == 'field_name'){
$val[] = $value;
}
}
echo $val[0];
echo $val[1];
echo $val[2];
I have found this solution in the end...with if. That's not what I would have liked but it works at least and it hasnt much code.
Related
I have the following JSON string, I'm wanting to cycle through the 'sub' items and print them out as HTML Select Options but for some reason I can't quite get hold of them.
Only used JSON a couple times before so it's probably a rookie mistake somewhere.
{
"1":{
"question":"How happy are you with your car?",
"sub":[
"Very Happy",
"Not So Happy",
"Unhappy",
"Very Unhappy"
]
}
}
I have the following which let's me echo out the Question Value but how would I loop through each of the 'sub' arrays? (There's only ever going to be 1 question which is why I'm storing it in a single variable)
$questionAnswer = json_decode($data->question,true);
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
}
Add another loop inside the existing one:
$questionAnswer = json_decode($data->question,true);
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
echo $question."<br>";
// value['sub'] contains the array of 'subs', so you can loop through that the same way
foreach ($value['sub'] as $sub) { // since the key will be 0,1,2 you might not need it here, so I omitted it.
echo $sub."<br>";
}
}
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
foreach ($value['sub'] as $answer) {
echo $answer;
}
}
I want to change the value of the $key because I have array_splice inside the loop which change the position of my values so - it mess up the value I need in a specific place.
I tried $key-- but it doesn't work.
for example when I print the $key after I do echo $key it's fine but when I echo $key just after the foreach loop I get the worng value.
Any ideas?
foreach ($cut as $key => $value) {
echo "foreach key:".$key."<br>";
if(in_array($value,$operators))
{
if($value == '||')
{
echo "found || in position:".$key."<br>";
if(($key+1<sizeof($cut)))
{
$multi = new multi;
echo "<br>"."key-1: ";
print_r($cut[$key-1]);
echo"<br>";
echo "<br>"."key+1: ";
print_r($cut[$key+1]);
echo"<br>";
$res = $multi->orex($cut[$key-1],$cut[$key+1],$numString);
$cut[$key-1]= $res;
array_splice($cut,$key,1);
array_splice($cut,$key,1);
$key--; //here trying to change the key
echo "new string:";
print_r($cut);
echo "<br>";
echo "key:".$key."<br>";
}
}
}
}
Updated
I don't think it is a good idea to change the array itself inside the foreach loop. So please crete another array and fill data into it, which will be your result array. This method works well when your array data is not big, in other words, most situations.
Origin
I don't know what do you mean. Let me give it a guess...
You want:
foreach($arr as $key=>$val){
$newkey = /* what new key do you want? */
$arr[$newkey] = $arr[$key];
unset($arr[$key]);
}
HTML example:
<form method="post" id="form" action="form_action.php">
<input name="email" type="text" />
</form>
User fills input field with: dfg#dfg.com
echo $_POST['email']; //output: dfg#dfg.com
The name and value of each input within the form is send to the server.
Is there a way to get the name property?
So something like..
echo $_POST['email'].name; //output: email
EDIT:
To clarify some confusion about my question;
The idea was to validate each input dynamically using a switch. I use a separate validation class to keep everything clean. This is a short example of my end result:
//Main php page
if (is_validForm()) {
//All input is valid, do something..
}
//Separate validation class
function is_validForm () {
foreach ($_POST as $name => $value) {
if (!is_validInput($name, $value)) return false;
}
return true;
}
function is_validInput($name, $value) {
if (!$this->is_input($value)) return false;
switch($name) {
case email: return $this->is_email($value);
break;
case password: return $this->is_password($value);
break;
//and all other inputs
}
}
Thanks to raina77ow and everyone else!
You can process $_POST in foreach loop to get both names and their values, like this:
foreach ($_POST as $name => $value) {
echo $name; // email, for example
echo $value; // the same as echo $_POST['email'], in this case
}
But you're not able to fetch the name of property from $_POST['email'] value - it's a simple string, and it does not store its "origin".
foreach($_POST as $key => $value)
{
echo 'Key is: '.$key;
echo 'Value is: '.$value;
}
If you wanted to do it dynamically though, you could do it like this:
foreach ($_POST as $key => $value)
{
echo 'Name: ', $key, "\nValue: ", $value, "\n";
}
Loop your object/array with foreach:
foreach($_POST as $key => $items) {
echo $key . "<br />";
}
Or you can use var_dump or print_r to debug large variables like arrays or objects:
echo '<pre>' . print_r($_POST, true) . '</pre>';
Or
echo '<pre>';
var_dump($_POST);
echo '</pre>';
Actually I found something that might work for you, have a look at this-
http://php.net/manual/en/function.key.php
This page says that the code below,
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
would give you the output,
fruit1
fruit4
fruit5
As simple as that.
current(array_keys($_POST))
should give you what you are looking for. Haven't tested though.
Nice neat way to see the form names that are, or will be submitted
<?php
$i=1;
foreach ($_POST as $name => $value) {
echo "</b><p>".$i." ".$name;
echo " = <b>".$value;
$i++;
}
?>
Just send your form to this script and it will show you what is being submitted.
You can use a foreach Loop to get all values that are set.
foreach ($_POST AS $k=>$v){
echo "$k is $v";
}
Your example
echo $_POST['email'].name; //output: email
wouldnt make sense, since you already know the name of the value you are accessing?
while($fetchres = mysql_fetch_array($searchquery)) {
$v1 = $fetchres['v1']; $v2 = $fetchres['v2']; $v3 = $fetchres['v3'];
$v4 = $fetchres['v4']; $v5 = $fetchres['v5'];
$v6 = $fetchres['v6'];
$v7 = $fetchres['v7'];
}
Hi! How can I set this columns automatically? I mean my system automatically adds a column. For example my system adds a new column 'v8'. But in my code, I only inputted until v7. How can I automatically code this fetching of query by depending on how many columns my system automatically made? Thanks.
Use extract.
while ($row = mysql_fetch_assoc($searchQuery))
{
// Extract the keys of the array into the local symbol table.
extract($row);
// Now, all variables exist and you can echo them like this:
echo $v1;
echo $v8;
}
You still need to know the names of the variables. If you just want to echo everything inside, use foreach with a key assingnment:
while ($row = mysql_fetch_assoc($searchQuery))
{
foreach($row as $key => $value)
{
echo "$key: $value\n"; // Will output something like 'v1: foo' and 'v2: bar', separated by newlines.
}
}
If you really want to do this, you can use extract($fetchres).
More info about extract
Example:
$fetchres = array('v1'=>'foo', 'v2'=>'bar');
extract($fetchres);
echo '$v1 is ' . $v1 . 'and $v2 is ' . $v2;
You could use variable variables for that, although I think this is a really bad idea.
I think you actually want this:
while ($fetchres = mysql_fetch_array($searchquery)) {
foreach ($fetchres as $key => $value) {
echo $key . ': ' . $value . PHP_EOL;
}
}
I have an indexed array which I've generated from an associative array with this code
$index_arr = array();
foreach($assoc_arr as $key => $val ){
$index_arr .= $val;
}
when I print it with print_r($index_arr); it works fine. But when I try to print it with foreach I get an error "Invalid argument supplied for foreach()"
foreach($index_arr as $one){
echo "one: $one<br />";
}
I'm pretty sure this is the right syntax or am I too tired at this time of day?
You turn the array into a string by using .= operator on it. You want to use:
$index_arr[] = $val;
To append to the end.
Also in this particular case, you can just do:
$index_arr = array_values($assoc_arr);
This does exactly what your loop does.
When you did $index_arr .= $val; PHP did a String operation. You need to do $index_arr[]=$val;
Needs to be this:
$index_arr = array();
foreach($assoc_arr as $key => $val ){
$index_arr[] = $val;
}
Also
foreach($index_arr as $key=>$data){
echo "Key: ".$key." Data: ".$data."<br />";
}
$index_arr .= $val;
should be
$index_arr[] = $val;