pretty straight forward question this - I am trying to create an array to store the Model and Cost values taken from my database table. I figured I could begin the array, then create a while loop, and then end the array, and smiles all around. I may be mistaken, or I may have blindly missed something in my code, but could you have a look?
$array = array(
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
$overall_cost["model"] => $overall_cost["cost"],
}
);
var_dump($array);
I think this is what you're looking for:
$array = array();
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
$array[$overall_cost["model"]] = $overall_cost["cost"];
}
var_dump($array);
You can't do it like this. You need to add to the array inside the while loop:
$array = array();
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
$array[$overall_cost["model"]] = $overall_cost["cost"];
}
var_dump($array);
would be one way of doing it.
EDITED to produce simple array.
I don't think that will work. Try something like:
$array = array();
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
$array[$overall_cost["model"]] = $overall_cost["cost"];
}
var_dump($array);
Related
I don't know why I'm having trouble figuring this out, but I'm hoping someone can help.
I want to dynamically add multiple indices to an array. For example, if I have a list of IDs.
$ids = array(1247, 1248);
How can I do this dynamically, depending on the number of ids that are in the array?
$history['transactions'][$id1][$id2]['Thursday'] = 0;
If I am unserstanding you correctly, then your code should work. unless you are trying to use the array your created, take a look. I think a simple change could solve your problem
this would work:
$ids = array(1247, 1248);
$history['transactions'][$ids[0]][$ids[1]]['Thursday'] = 0;
So would this:
$id1 = 1247;
$id2 = 1248;
$history['transactions'][$id1][$id2]['Thursday'] = 0;
From the looks of it, you just aren't calling anything. but without more of your code I can't be more help
As Daedalus points out, to this point the code isn't really dynamic, (I assumed you just needed a snipit in the the middle to help with already dynamic code. but now I will assume the opposite) here is an example of how how to change that:
say you had the array $ids = array(1247, 1248, 1249, 1250, 1251); you would need to loop through the lot of them and deal with them individually. Probably the easiest way to do that would be something like:
$ids = array(1247, 1248, 1249, 1250, 1251);
foreach($ids as $id){
$history['transactions'][$id]['Thursday'] = 0;
}
Make sense?
Third try:
Getting a better understanding of whats going on. (sorry, took a long time to get it through my thick skull)
So if you want an array with all the ids in order of the array $ids = array(1247, 1248, 1249, 1250, 1251); then this is the solution for you:
$ids = array(1247, 1248, 1249, 1250, 1251);
$result = array('Thursday' => 0);
for($i = count($ids) -1; $i >= 0; $i--){
$result = array($ids[$i] => $result);
}
$history = array('transactions' => $result);
var_dump($history) yields:
array(1) {
["transactions"]=>
array(1) {
[1247]=>
array(1) {
[1248]=>
array(1) {
[1249]=>
array(1) {
[1250]=>
array(1) {
[1251]=>
array(1) {
["Thursday"]=>
int(0)
}
}
}
}
}
}
}
Which is now what I believe you are looking for
I think that if you explain better what's your problem, we could help in a better way that explaining you how to create arrays like this:
$history['transactions'][$id1][$id2]['Thursday'] = 0;
depending on how much ids there are in a array. For sure you can do what you want do to in a better way.
Or, just try to think some other way than putting ids as parameters of an array.
You could use references to achieve this very dynamically, but I don't understand the reason to do this...
$ids = array(1247, 1248);
$data = &$history['transactions'];
foreach ($ids as $id) {
$data = &$data[$id];
}
$data['Thursday'] = 0;
I'm certain this is VERY simple, but being new and not knowing what I'm doing I can't quite get it on my own.
I am trying to create an array of wordpress post ID's that is derived from a simple foreach loop.
Basically I have this as my code:
$omit_these_quizzes = array();
foreach ( $filtered_pass as $single_quiz ) {
$quiz_id_number = $single_quiz['quiz'];
$omit_these_quizzes[] = $quiz_id_number;
}
I would like to take each of the resulting $quiz_id_number 's and have then end up in array that looks like this:
$omit_these_quizzes = array(8195,8193);
However, I keep ending up with an array that only contains the variable from the very last foreach loop, instead of them all. What am I doing wrong?
Thanks!
Your code looks fine.But if you want alternative try this:
see here for in_array and array_push.
$omit_these_quizzes = array();
foreach ( $filtered_pass as $single_quiz )
{
$quiz_id_number = $single_quiz['quiz'];
if(!in_array($quiz_id_number ,$omit_these_quizzes))
{
array_push($omit_these_quizzes, $quiz_id_number);
}
}
hope it's help you.
This work?
$omit_these_quizzes = array();
foreach ( $filtered_pass as $single_quiz ) {
$quiz_id_number = $single_quiz['quiz'];
array_push($omit_these_quizzes, $quiz_id_number);
}
Looks like you are creating a new array in each loop, that's why you end up with an array containing just a single element...
Use:
$omit_these_quizzes = array();
foreach ( $filtered_pass as $single_quiz )
{
$quiz_id_number = $single_quiz['quiz'];
array_push($omit_these-quizzes, $quiz_id_number);
}
Your code is correct. Inspect your $filtered_pass variable by calling var_dump($filtered_pass); may be it contain only one subarray.
I have the following code:
$items = array();
foreach($following as $storeOwner)
{
array_push($items, $productRepository->mostRecentItem($storeOwner->getId(), 5));
}
I am trying to append the results of
$productRepository->mostRecentItem($storeOwner->getId(), 5)
to $items. How do I do so? Why doesn't the above code work?
Try this:
$items = array();
foreach($following as $storeOwner)
{
$items[] = $productRepository->mostRecentItem($storeOwner->getId(), 5);
}
Also, take a look to the result of the mostRecentItem Method... .
var_dump you different objects and return values to make sure that contain what you think they should contain. The code looks "correct" so it's probably the objects and values that are not.
If i knew the correct terms to search these would be easy to google this but im not sure on the terminology.
I have an API that returns a big object. There is one particular one i access via:
$bug->fields->customfield_10205[0]->name;
//result is johndoe#gmail.com
There is numerous values and i can access them by changing it from 0 to 1 and so on
But i want to loop through the array (maybe thats not the correct term) and get all the emails in there and add it to a string like this:
implode(',', $array);
//This is private code so not worried too much about escaping
Would have thought i just do something like:
echo implode(',', $bug->fields->customfield_10205->name);
Also tried
echo implode(',', $bug->fields->customfield_10205);
And
echo implode(',', $bug->fields->customfield_10205[]->name);
The output im looking for is:
'johndoe#gmail.com,marydoe#gmail.com,patdoe#gmail.com'
Where am i going wrong and i apologize in advance for the silly question, this is probably so newbie
You need an iteration, such as
# an array to store all the name attribute
$names = array();
foreach ($bug->fields->customfield_10205 as $idx=>$obj)
{
$names[] = $obj->name;
}
# then format it to whatever format your like
$str_names = implode(',', $names);
PS: You should look for attribute email instead of name, however, I just follow your code
use this code ,and loop through the array.
$arr = array();
for($i = 0; $i < count($bug->fields->customfield_10205); $i++)
{
$arr[] = $bug->fields->customfield_10205[$i]->name;
}
$arr = implode(','$arr);
This is not possible in PHP without using an additional loop and a temporary list:
$names = array();
foreach($bug->fields->customfield_10205 as $v)
{
$names[] = $v->name;
}
implode(',', $names);
You can use array_map function like this
function map($item)
{
return $item->fields->customfield_10205[0]->name;
}
implode(',', array_map("map", $bugs)); // the $bugs is the original array
How can you do this? My code seen here doesn't work
for($i=0;i<count($cond);$i++){
$cond[$i] = $cond[$i][0];
}
It can be as simple as this:
$array = array_map('reset', $array);
There could be problems if the source array isn't numerically index. Try this instead:
$destinationArray = array();
for ($sourceArray as $key=>$value) {
$destinationArray[] = $value[0]; //you may want to use a different index than '0'
}
// Make sure you have your first array initialised here!
$array2 = array();
foreach ($array AS $item)
{
$array2[] = $item[0];
}
Assuming you want to have the same variable name afterwards, you can re-assign the new array back to the old one.
$array = $array2;
unset($array2); // Not needed, but helps with keeping memory down
Also, you might be able to, dependant on what is in the array, do something like.
$array = array_merge(array_values($array));
As previously stated, your code will not work properly in various situation.
Try to initialize your array with this values:
$cond = array(5=>array('4','3'),9=>array('3','4'));
A solution, to me better readable also is the following code:
//explain what to do to every single line of the 2d array
function reduceRowToFirstItem($x) { return $x[0]; }
// apply the trasnformation to the array
$a=array_map('reduceRowTofirstItem',$cond);
You can read the reference for array map for a thorough explanation.
You can opt also for a slight variation using array_walk (it operate on the array "in place"). Note that the function doesn't return a value and that his parameter is passed by reference.
function reduceToFirstItem(&$x) { $x=$x[0]; }
array_walk($cond, 'reduceToFirstItem');
That should work. Why does it not work? what error message do you get?
This is the code I would use:
$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
$outArr[$i] = $inArr[$i][0];
}