can i showing only one value in foreach array having multiple same value, without grouping the array firstly in the query like this :
0 =>
array (size=10)
'id' => string '1' (length=1)
'questionname' => string 'question 01' (length=36)
'answerspossible' =>
array (size=3)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
2 =>
array (size=2)
...
'answer' => string 'YES' (length=3)
'answer2' => string '' (length=0)
1 =>
array (size=10)
'id' => string '1' (length=1)
'questionname' => string 'question 01' (length=36)
'answerspossible' =>
array (size=3)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
2 =>
array (size=2)
...
'answer' => string 'YES' (length=3)
'answer2' => string 'test answer' (length=0)
Result i want in the view is to group by the questioname inside the foreach :
question 01 :
- answer & answer 2
- answer & answer 2
My code is :
foreach ($Questions as $Key => $Question) {
echo $question['questionname'];
echo $Question['answer']." & ".$Question['answer2'];
}
thnx for help :)
$justblank = ''; // just a blank variable we will use it later.
foreach ($Questions as $Key => $Question) {
echo $question['questionname'];
$questionanswers = $Question['answer']." & ".$Question['answer2'];
if($justblank == $questionanswers){
break;
}else{
echo $questionanswers;
}
$justblank .= $questionanswers;
}
Hi Mohammed, I hope this help you :).
a blank var work
$justblank = -1;
foreach ($Questions as $Key => $Question) {
if($Question['id']!=$justblank){
echo $Question['questionname'];
$justblank=$Question['id'];
}
....
}
I want to display array 1 by 1.. 1st array value display thnn 2nd array value
I want to display array data using loop and display data separatelylike 1st array data display then 2nd array data will display. and i want to display data only entrydate n description. with loop count
$svs= $user_posts->getvalue('service', $dsplylimit );
var_dump($svs);
My array is like this
array (size=2)
0 =>
array (size=46)
0 => string '74' (length=2)
'id' => string '74' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 07:32:08' (length=19)
'entrydate' => string '2016-02-08 07:32:08' (length=19)
3 => string '2016-02-08 01:32:08' (length=19)
'date_gmt' => string '2016-02-08 01:32:08' (length=19)
4 => string 'nice email' (length=10)
'description' => string 'nice email' (length=10)
1 =>
array (size=46)
0 => string '75' (length=2)
'id' => string '75' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 11:15:40' (length=19)
'entrydate' => string '2016-02-08 11:15:40' (length=19)
3 => string '2016-02-08 05:15:40' (length=19)
'date_gmt' => string '2016-02-08 05:15:40' (length=19)
4 => string 'hiiiiiii' (length=8)
'description' => string 'hiiiiiii' (length=8)
It isn't very clear what you want to do, but you can probably display the data with a foreach loop. Just loop through each array, and then print out the values from each iteration that you want.
For example for the following array
$data = [
['entrydate' => '2016-01-01', 'description' => 'Some description here'],
['entrydate' => '2016-01-02', 'description' => 'Another description here']
];
You could print out the data like so:
$html = '';
foreach ($data as $array) {
$html .= '<h1>' . htmlspecialchars($array['entrydate']) . '</h1>';
$html .= '<p>' . htmlspecialchars($array['description']) . '</p>';
}
echo $html;
For the following output:
2016-01-01
Some description here
2016-01-02
Another description here
There's a chance I'm not understanding your requirements, if so just let me know, but I'm not 100% sure what it is you're after.
I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)
Array 1: $tags_result
array (size=4)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
2 =>
object(stdClass)[10]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)
Array 2: $post_tags_result
array (size=2)
0 =>
object(stdClass)[5]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[6]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
I'm trying to extract the values of the array 1 that do not appear in the array 2:
function foo($tags_result, $post_tags_result){
return $tags_result->id_tag != $post_tags_result->id_tag;
}
$difference_tags = array_udiff($tags_result, $post_tags_result, 'foo');
But the result return a common value: tag 1. I expect just tag 4 and tag 9.
array (size=3)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)
As an alternative, you could gather all the tags that needed to be excluded first. Then after that you could now filter it thru array_filter and get the desired result. Rough example:
$tags = array();
foreach($post_tags_result as $t) {
$tags[] = $t->tag; // gather all tags
}
// filter array using gathered tags
$result = array_filter($tags_result, function($v) use($tags){
return !in_array($v->tag, $tags);
});
Sample Output
From the manual:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
So, instead of a Boolean comparison you should do a diff to delete equal items. Since you are comparing numeric values, you should do something like:
$ php -a
php > $a = [['id'=>1],['id'=>2],['id'=>3],['id'=>4]];
php > $b = [['id'=>1],['id'=>4]];
php > $c = array_udiff($a, $b, function($a, $b){
return $a['id'] - $b['id'];
});
php > print_r($c);
Array
(
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
)
In your case:
$difference_tags = array_udiff($tags_result, $post_tags_result, function(($a1, $a2){
return $a1->id_tag - $q2->id_tag;
}));
I have been stumped on this for awhile and was wondering if there was a way to turn this data into a custom multidimensional array.
I run a query on my table and it spits out an array like this
SELECT id_cms_category, name FROM cms_category_lang ORDER BY id_cms_category ASC
The resulted array looks like this.
array
0=>
array
'id_cms_category'=> 1
'name'=>'Home'
1=>
array
'id_cms_category'=> 2
'name'=>'Test'
So then I run my query on my cms table
SELECT cl.id_cms, c.id_cms_category, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci ON (ci.id_cms = cl.id_cms)
ORDER BY c.id_cms_category, cl.meta_title ASC
and I get an array like this
array
0 =>
array
'id_cms' => string '4' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'About us' (length=8)
1 =>
array
'id_cms' => string '6' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Contact Us' (length=10)
2 =>
array
'id_cms' => string '1' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Delivery' (length=8)
3 =>
array
'id_cms' => string '2' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Legal Notice' (length=12)
4 =>
array
'id_cms' => string '5' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Secure payment' (length=14)
5 =>
array
'id_cms' => string '3' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Terms and conditions of use' (length=27)
6 =>
array
'id_cms' => string '10' (length=2)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech IBC Spill Pallet Plus' (length=37)
7 =>
array
'id_cms' => string '9' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech P2 Plus 2-Drum Hard Top Spill Pallet' (length=52)
8 =>
array
'id_cms' => string '7' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'XR-5, Urethane & Copolymer 2000' (length=31)
I am wanting to try and take all this data and somehow create a multidimensional array like so;
array
0 => 'Home'
array
id_cms => meta_title
1 => 'Test'
array
id_cms => meta_title
that way I should be able to run something like this
foreach($title as $key => $value)
{
<li>$value</li>
foreach($value as $id_cms => $page)
{
<li>$page</li>
}
}
to get the output on the page to look something like this
Home
home 1
home 2
home 3
Test
test 1
test 2
test 3
I wouldn't do two queries for this.
I would alter your second query so you've got both the category and page name in the same result set. It should look something like:
SELECT cl.id_cms, c.id_cms_category, catlang.name, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c
ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci
ON (ci.id_cms = cl.id_cms)
LEFT JOIN cms_category_lang catlang
ON (catlang.id_cms_category = cl.id_cms_category)
ORDER BY c.id_cms_category, catlang.name, cl.meta_title ASC;
Then you could create a loop that simply checked the category in each iteration. If it finds a new one, end the previous block and start a new one:
$currentCategory = '';
$output = '';
foreach ($rows as $row) {
if ($currentCategory != $row['name']) {
$output .= '</li><li>' . $row['name'];
}
$currentCategory = $row['name'];
$output .= '<li><a href="' . $row['id_cms'] . '">' . $row['meta_title'] . '</li>';
}
// Trim off our unnecessary </li>
$output = substr($output, 4);
If your CMS returns a numerically indexed array (and that's what it sounds like from your post), you can re-index the array using whatever key you want.
$array = array(0 => array('id_cms' => 4, 'id_cms_category' => 1, 'meta_title' => 'About us'));
$newArray = array();
foreach ( $array as $v ) {
$newArray[$v['meta_title']] = $v;
}
var_dump($newArray);
Arrays are vital in PHP programming, so you'd be doing yourself a favor to learn them before going any further.
Create a new empty array: $newArray = array();
Go through your result with a foreach, then as you go, check if the category of the current item already exists in your new array: if (isset($newArray[$row['id_cms_category']))
If it doesn't, create it and initialize it with a new array containing the item as a first element: $newArray[$row['id_cms_category'] = array($row);
If it already existed (i.e. you already went through an item belonging to this category), then add it: $newArray[$row['id_cms_category'] []= $row;
That's it!