I don't know how to say it in the title, but you'll understand with an example.
I've a database, where I store data in column named propriety and another column value. When I do my SQL request, I get this:
Array
(
[0] => Array
(
[propriety] => propriety1
[value] => value1
)
[1] => Array
(
[propriety] => propriety2
[value] => value2
)
[2] => Array
(
[propriety] => propriety3
[value] => value3
)
I would like to display it like this one:
Propriety : Value
But when I do my 2 foreach:
foreach ($array as $key => $value) {
foreach ($value as $k => $v) {
echo $k . ' : ' . $v . '<br>';
}
}
It returns:
Propriety : Propriety1
Value : Value1
etc ..
You don't need two foreach loops to do this, it can be done with one:
foreach ($array as $row) {
echo $row['propriety'].' : '.$row['value'].'<br>';
}
Assuming you actually want the values returned by the database.
Here's an alternative extracting value and re-indexing by propriety:
foreach(array_column($array, 'value', 'propriety') as $propriety => $value) {
echo "$propriety : $value<br>";
}
This assumes that each propriety will be unique.
Related
My array
$key1=>
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 11
[4] => 12
[5] => 17
[6] => 18
)
$_POST['name']=>
Array
(
[0] => General
[1] => General
[2] => Outdoors
[3] => Dining
[4] => Kitchen
)
Here is my code,
foreach ($key1 as $key => $value) {
// echo $value;
foreach ($_POST['name'] as $key => $value1) {
//echo $value;
$subQueryCond .=' AND '.$value1.' LIKE ' .$value ;
}
}
While my Ajax calls this nested loop occurs..
Inside this I wrote a query..
If one value is passed.
The query is in the format of AND 'General' LIKE 1.
And if another value is passed in the $key1 it pass the query two times.
It's like How many arrays are given that much time that query was passed..
So,here I would like to restrict the $value if it already came..
if two values were given,it pass the query in the following manner
AND General LIKE 1
AND Outdoors LIKE 1
AND General LIKE 7
AND Outdoors LIKE 7
And my desired query must be in the form of
AND General LIKE 1
AND General LIKE 7
AND Outdoors LIKE 7
can someone help me..
Use this code to remove duplicates from an array:
$uniqueKey1 = array_unique($key1);
And then you regular code:
foreach ($uniqueKey1 as $key => $value) {
// echo $value;
foreach ($_POST['name'] as $key => $value1) {
//echo $value;
$subQueryCond .=' AND '.$value1.' LIKE ' .$value ;
}
}
This will work for you...
<?php
$subQueryCond= '';
foreach ($key1 as $key => $value)
{
foreach ($_POST['name'] as $key => $value1)
{
$subQueryCond['AND '.$value1.' LIKE ' .$value] = ' AND '.$value1.' LIKE ' .$value ;
}
}
echo "<pre>"; print_r($subQueryCond);
$query = implode('',$subQueryCond) ;
print_r($query);
?>
just make an array with unique keys to value, then use implode() function to make query string...
I am a little confused about the array_search function (or maybe I am trying to use the incorrect thing.) I have a bunch of transaction objects, (transactions about a customer) each is an array of values. After passing in an email address, I get an object that is one transaction where that email address was used. Example is below. I get it from the command print_r($results):
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 4564564654-65465464565-4654654
[Results] => Array
(
[0] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 5555555555
[email_addy] => someaddy#something.com
)
[1] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => someaddy#something.com
)
[2] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => someaddy#something.com
)
)
)
I get this output with no trouble at all. My issue is that I need to identify when someone has a specific ID. I was trying to use a foreach but I am not getting back what I need. The code and output is below.
foreach ($results as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Output is
Key: OverallStatus; Value: OK
Key: RequestID; Value: 4564564654-65465464565-4654654
Key: Results; Value: Array
All I really need to know is if the customer has an ID of 5555555555. This number will always stay the same. Am I going in the wrong direction here?
You have array of object , so you should get first the array results , and then iterate over it,try this :
foreach ($results->Results as $key => $value) {
if($value->ID == 55555555) echo 'found at position'.$key;//if id is unique , add a break;
}
You need to iterate through $results->Results.
foreach ($results->Results as $key => $value) {
if($value->ID == 5555555555)
print "Match found";
}
You should loop again for results array. Try following foreach
foreach ($results as $key => $value) {
if($key == "Results") {
foreach($value as $v) {
if($v->ID == "5555555555") {
echo "I found you";
break;
}
}
} else {
echo "Key: $key; Value: $value<br />\n";
}
}
Loop through '$results->Results' and check in loop if the ID=5555555555 like this:
foreach ($results->Results as $value) {
if($value->ID == 5555555555) {
//do something
}
}
It would be something like the following:
$transactions = $results->Results; //get results array
foreach ($transactions as $transaction) { // loop through each transaction object.
echo $transaction->ID . '<br />'; // print out the id of each transaction.
}
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
Example: key['user_id'] this will print all user_id from array. is it possible? please help me thanks
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 2
)
)
[Post3] => Array
(
[0] => Array
(
[user_name] => 1
)
)
)
Here is my PHP code:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
You can print_r to achive the same results you want with your triple for each.
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
You can easily use recursion for such a problem. You can use something along the lines of:
function printValuesByKey($array, $key) {
if (!is_array($array)) return;
if (isset($array[$key]))
echo $key .': '. $array[$key] .'<br>';
else
foreach ($array as $v)
printValuesByKey($v, $key);
}
In your example:
printValuesByKey($array, 'user_id');
will print:
user_id: 1
user_id: 2
I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";
foreach ($_GET as $field => $label) {
$datarray[]=$_GET[$field];
echo "$_GET[$field]";
echo "<br>";
This is the output I am getting. I see the data is there in datarray but when I echo $_GET[$field] I only get "Array"
But print_r($datarray) prints all the data. Any idea how I pull those values?
OUTPUT:
Array (
[0] => Array (
[0] => Grade1
[1] => ln
[2] => North America
[3] => yuiyyu
[4] => iuy
[5] => uiyui
[6] => yui
[7] => uiy
[8] => 0:0:5
)
)
foreach ($_GET as $key => $value)
{
if(is_array($value))
{
foreach($value as $childKey => $childValue)
{
echo $childKey." ".$childValue; // or assign them to an array
}
}
else
echo $key." ".$value; // or assign them to an array
}
Seems like $_GET[$field] is basically $_GET[0], which is an array:
You'll have to loop through $_GET[$field] with a forloop to get the content to echo out. By the way you can't echo array you'll have to use print_r
something like this:
foreach ($_GET as $field => $label) {
$datarray[]=$_GET[$field];
for($i=0; $i<$_GET[$field]; $i++){
echo $_GET[$field][$i];
}
echo "<br>";
}
EDIT: When I completed your test, here was the final URL:
http://hofstrateach.org/Roberto/process.php?keys=Grade1&keys=Nathan&keys=North%20America&keys=5&keys=3&keys=no&keys=foo&keys=blat&keys=0%3A0%3A24>
This is probably a malformed URL. When you pass duplicate keys in a query, PHP makes them an array. The above URL should probably be something like:
http://hofstrateach.org/Roberto/process.php?grade=Grade1&schoolname=Nathan®ion=North%20America&answer[]=5&answer[]=3&answer[]=no&answer[]=foo&answer[]=blat&time=0%3A0%3A24
This will create individual entries for most of the fields, and make $_GET['answer'] be an array of the answers provided by the user.
Bottom line: fix your Flash file.