Show duplicate value in array only one but show all others - php

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'];
}
....
}

Related

Store unique values within foreach loop

Im trying to get value of ids for different values of a foreach loop but im confused how to go about it.
$revenue = array('pseudo1', 'pseudo2');
foreach ($revenue as $value) {
if (!$revenue_type) {
$st = getValueDescription($value);
foreach ($st as $stype) {
$tpe[] = $stype->id;
}
$rev[$value] = $tpe;
}
}
when i dump $rev this is what i get
array (size=1)
0 =>
array (size=3)
'pseudo1' =>
array (size=2)
0 => string '9' (length=1)
1 => string '19' (length=2)
'pseudo2' =>
array (size=4)
0 => string '9' (length=1)
1 => string '19' (length=2)
2 => string '1' (length=1)
3 => string '35' (length=2)
what i actually expect
array (size=1)
0 =>
array (size=3)
'pseudo1' =>
array (size=2)
0 => string '9' (length=1)
1 => string '19' (length=2)
'pseudo2' =>
array (size=4)
2 => string '1' (length=1)
3 => string '35' (length=2)
I need the result of my $rev to contain $value as keys but previous values of $tpe keeps adding up with each iteration, im confused how to achieve this.
Does this solve your problem?
$revenue = array('pseudo1', 'pseudo2');
$uniqueValues = [];
foreach ($revenue as $value) {
if (!$revenue_type) {
$st = getValueDescription($value);
$tpe = [];
foreach ($st as $stype) {
if (!in_array($stype->id, $uniqueValues)) {
$tpe[] = $stype->id;
$uniqueValues[] = $stype->id;
}
}
$rev[$value] = $tpe;
}
}
$uniqueValues holds the IDS you already added to the $rev variable, and the $tpe gets empty after every iteration

Sorting 'key=>value' pair multi-dimensional arrays in PHP

I am creating a site and I am trying to do a few complicated things with arrays to get it to work.
It is for an e-commerce site and each product on my site can have a number of attributes. I am trying to get each combination of attributes to do the pricing for them seperately.
First I get an array of the attribute ids ($attribute_id_array) and the query the database for the options for that array.
So if one attribute was colors the options here would be red,green,blue,etc,. or size would be small,medium,large,etc,. These are then stored in a new array ($attribute_arrays).
I then go through these to get every combination of attributes the product can have and sort these into a new array again ($new_attributes_array).
I then loop through this and create a price form for each combination.
$attribute_arrays = [];
foreach($attribute_id_array as $attribute_id){
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
array_push($attribute_arrays,$row);
}
}
var_dump($attribute_arrays);
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
$tmp = combinations($arrays, $i + 1);
$result = array();
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
$new_attributes_array = combinations($attribute_arrays);
var_dump($new_attributes_array);
This is all working fine except I want to be able to get the keys for all of the key value pairs so I can reference it back to my database.
The way it comes out at the moment is like this:
$attribute_id_array:
array (size=2)
1 => string '5' (length=1)
2 => string '7' (length=1)
$attribute_arrays:
0 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
1 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
$new_attributes_array:
0 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '3 metres' (length=8)
1 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '6 metres' (length=8)
2 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '3 metres' (length=8)
3 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '6 metres' (length=8)
Is there a way to get it so that the key will be similar in format to:
0 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute1 => string '3 metres' (length=8)
1 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute2 => string '6 metres' (length=8)
Edit
So I changed the line array_push($attribute_arrays,$row); to $attribute_arrays[$attribute_id] = $row;.
This now means that $attribute_arrays now has the$attribute_id variable as the key like so:
array (size=2)
5 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
7 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
This now means my other function for getting the combinations won't work as it is using the $i variable as the index for the array starting at '0'.
Found another function online to sort it here How to generate in PHP all combinations of items in multiple arrays:
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $property => $property_values) {
$tmp = array();
foreach ($result as $result_item) {
foreach ($property_values as $property_key => $property_value) {
$tmp[] = $result_item + array($property_key => $property_value);
}
}
$result = $tmp;
}
return $result;
}
However, this doesn't do exactly as I want and I end up with this:
array (size=4)
0 =>
array (size=1)
'attribute1' => string 'Step Through Bars' (length=17)
1 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string '6 metres' (length=8)
2 =>
array (size=2)
'attribute2' => string 'Gated' (length=5)
'attribute1' => string '3 metres' (length=8)
3 =>
array (size=1)
'attribute2' => string 'Gated' (length=5)
try this as your combinations function
modified code taken from here
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $key => $values) {
$tmp = array();
foreach ($result as $item) {
foreach ($values as $k=>$value) {
$tmp[] = array_merge($item, array($key.'-'.$k => $value));
}
}
$result = $tmp;
}
return $result;
}
Instead of using while on following line
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
Use foreach loop to get key paired value. e.g
foreach($databaseFetchedRecord as $mykey=>$myvalue)

Convert array index to custom value?

I have an array $indexedarray
printr($indexedarray) gives something like this
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
I want to change this arrays index also same as value, like
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
is it posssible??
You can use array_combine:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
Gives:
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
But be aware that your duplicate values will be dropped. Keys will be unique!
Try This :
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);

Laravel 5 adding an element in an array does not work

I am getting an array from table questions
$questions = Category::where('slug', $slug)->first()->questions->toArray();
then using foreach I iterate over array and push an array element during each iteration like below
foreach ($questions as $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
Ideally I should get response like below
array (size=5)
0 =>
array (size=6)
'id' => int 1
'category_id' => int 1
'questions' => string 'The ozone layer restricts' (length=25)
'answer_id' => int 4
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
1 =>
array (size=6)
'id' => int 2
'category_id' => int 1
'questions' => string 'Ecology deals with' (length=18)
'answer_id' => int 10
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
but Actually I am not getting options key into my questions array
The issue here is when you do a foreach, you are referring to the array by value - that is, under the hood PHP will create a copy of the variable inside the array. What you need is to access each element in the foreach by reference (note the & next to $q)
foreach ($questions as & $q) {
$q['options'] = Option::where('question_id', '=', $q['id'])->get()->toArray();
}
See:
Passing by references and the PHP docs on foreach.
Note: This is not really a Laravel issue, just PHP.
Try this (& in foreach):
foreach ($questions as & $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
You can try:
foreach ($questions as $key=>$question) {
$questions[$key]['options'] = Option::where('question_id',$question['id'])->get()->toArray();
}

manipulate php array or object

I've a problem with my php object.
explain:
I have this:
array (size=7)
'1stObject' =>
array (size=3)
0 =>
array (size=3)
'from' => string '1168498' (length=7)
'to' => string '0' (length=1)
'inner' => string '0' (length=1)
1 =>
array (size=3)
'from' => string '0' (length=1)
'to' => string '2379217' (length=7)
'inner' => string '0' (length=1)
2 =>
array (size=3)
'from' => string '0' (length=1)
'to' => string '0' (length=1)
'inner' => string '249111' (length=6)
But my problem is I want this:
array (size=7)
'1stObject' =>
array (size=3)
'from' => string '1168498' (length=7)
'to' => string '2379217' (length=7)
'inner' => string '249111' (length=6)
How can I do this?
I tested with array_merge, and other array function, but nothing help me..
Anyone have a function or a solution for me?
Thank you !
Alternatively, you could use array_filter and clear the unneeded. Sample: Output
foreach($values as &$object) {
$temp = array();
foreach($object as $key => $value) {
$value = array_filter($value); // clear zeroes
$temp[key($value)] = current($value); // transfer
}
$object = $temp;
}
i am not sure what is your actual array complete structure, this may help you
foreach($yourarray['1stObject'] as $subarray)
{
if($subarray['from']!=0)
$newarray['1stObject']['from'] = $subarray['from'];
if($subarray['to']!=0)
$newarray['1stObject']['to'] = $subarray['to'];
if($subarray['inner']!=0)
$newarray['1stObject']['inner'] = $subarray['inner'];
}
print_r($newarray);

Categories