e.g. I have this Product Id values 31,32 from database. I want to put them on array. So, I can use the values for foreach.
What I want to achieve:
I want to get the stock of each product from database according to the given values (31,32).
What I tried:
$product_values = '31,32'; //this values can be different, sample values only
$arr_product_values = array();
$arr_product_values[] = $product_values;
foreach ($arr_product_values as $prod_id) {
echo $prod_id;
}
Expected output:
31 & 32
$arr_product_values[] = $product_values;
That doesnt mean you have a new array with those 2 values now. You just took a comma separated string and assigned it to an array element, that doesnt make it an array itself.
$arr_product_values = array(31,32);
Does.
Now you can loop over it
Related
Lets say I have an array looking like this:
$sql = array("name"=>"Peter", "active"=>1 , "age"=>30)
and a loop looking like this:
for($i=0;$i<count($sql);$i++){
$value[$i] = ($sql[$i]);
echo $value[$i];
}
I want the loop to iterate through the array and assign each value to a new variable.
In this code i tried to make it store the values in:
value1
value2
value3
But sadly this doesnt work, thus I am here seeking help.
Or is it a problem that i got an associative array instead of a numeric one?
I dont want to use this loop on this array only but on other arrays with different keys and length aswell.
Edit: I think I may have not wrote it cleary enough to tell you what i want to achieve:
I want to have three string values at the end of the loop not stored in an array:
Variable1 should contain "Peter"
Variable2 should contain "1"
Variable3 should contain "30"
Plus I want this loop to be dynamic, not only accepting this specific array but if I were to give it an array with 100 Values, I would want to have 100 different variables in which the values are stored.
Sorry for not being clear enough, I am still new at stackoverflow.
Going by your condition, assign each value to a new variable, I think what you want would be to use Variable variables. Here is an example:
<?php
$sql = array("name"=>"Peter", "active"=>1 , "age"=>30);
$count = 1;
foreach ($sql as $value) {
$x = 'value'.$count;
$$x = $value; //here's the usage of Variable variables
$count++;
}
echo $value1.'<br/>';
echo $value2.'<br/>';
echo $value3.'<br/>';
I went to your sample variables ($value1, $value2, etc.). I also changed your loop to foreach to easily loop the array. And I also added a $count that will serve as the number of the $value variable.
The $count wouldn't be necessary if your index are numeric, but since its an associative array, something like this is needed to differentiate the variables created
A brief explanation as requested:
$x contains the name of the variable you want to create (in this case, value1), then when you add another $ to $x (which becomes $$x), you are assigning value to the current value of $x (this equals to $value1='Peter')
To dynamically define a variable use $$. Demo
$sql = array("name"=>"Peter", "active"=>1 , "age"=>30);
$index = 1;
foreach($sql as $value){
${"value" . $index++} = $value;
}
i want to display in my system just one record with the same id.
for example: 01,01,02,02,03,03.
I just want to display 01,02,03.
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
$number = "01,01,02,02,03,03";
$result = implode(',',array_unique(explode(',', $number )));
echo $result; // output 01,02,03
Happy Programming
I'm attempting to make 1 array out of 2 existing arrays (which cannot be modified). In order to do this I'm creating the array in a foreach which is nested in another foreach.
The code I used:
$language_option = array();
foreach(Languages::getFullSelectOptionsList() as $country_description_1 => $country_code){
foreach(Languages::getFullSelectOptionsList(TRUE) as $country_description_2 => $country_code){
$language_option[$country_code] = $country_description_1.' - '.$country_description_2;
}
}
In this code "Languages::getFullSelectOptionsList()" returns an array with the 1st country descriptions.
And "Languages::getFullSelectOptionsList(TRUE)" returns an array with the 2nd country descriptions.
This is what my code does:
dropdown results
But what I'd like it to do is:
dropdown wished results
As you can see in the first picture only the last array value of "country_description_1" is used instead of using them all.
Are there any errors in my code, is this not possible to do or is there an easier way of doing this?
Thanks.
Here you can get reference of this code.
But This will not work because you need to specify the values where $first_array[$i]
$language_option = array();
$first_array = Languages::getFullSelectOptionsList();
$second_array = Languages::getFullSelectOptionsList(TRUE);
for($i=0;$i<count($first_array); $i++){
$language_option[$country_code] = $first_array[$i].' - '.$second_array[$i];
}
Instead of $first_array[$i].' - '.$second_array[$i] put code according to your array structure to get description or code (key value).
How can I list all array items except that there is another Array ?
Example:
First array:
$idea_id[$row["id"]] = $row["id"];
Second array:
$m_voted[$votedke] = $row["ideaid"];
Keeping this datas in separate SQL tables.
And how to list all $idea_id but if
$idea_id[this_item] == $m_voted[any_item]
then this item not listing.
From your question what i understand is you want to check the data exists in the second array.
You can use in_array function which check if the idea_id is in m_voted array
foreach($idea_id as $id){
if(in_array($id,$m_voted)){
echo "{$id}<br/>";
}
}
I Have to iterate through an array, such that I can get all values of the array inside my desired variable in csv format?
My basic aim is to insert these csv values in to a column of db table.
Please guide to acheive this..
Thanks
use 'implode' in combination with a function dependant on your array structure.
i.e. implode(",", $array);
eg:
$array=array("value1", "value2", "value3");
echo implode(",",$array);
Would give you:
value1,value2, value3
Ideally your dataset will look like this:
$arrayofdata[rownumber][fieldnumber];
So rownumber 1 would have fieldnumber (or name) 1, 2, 3 etc..
You would then do:
// Loop through each row
foreach ($arrayofdata as $key){
echo implode(",", $arrayofdata[$key])."\r";
// joining each field with a comma, then moving onto a new line
}