0

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;
8
  • 1
    no idea what you are asking Commented Jan 9, 2014 at 22:03
  • 3
    What do you want the resulting array to look like? This doesn't exactly make sense to dynamically create depth in your array with essentially unknown keys (which is what your attempt does) Commented Jan 9, 2014 at 22:04
  • 2
    @Dagon: He wants to dynamically create an array based on how elements are in $ids. For example: $history['transactions'][1247][1248]['Thursday']. There could be more [xxx] based on the number of elements. Commented Jan 9, 2014 at 22:04
  • 1
    We need an example of the result you expect.. Commented Jan 9, 2014 at 22:06
  • 3
    If you second guess the decision to visit an Indian reservation, would that be a Reservation Reservation reservation? Commented Jan 9, 2014 at 22:11

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

This isn't dynamic. $ids could be of any size.
yeah I realize that was wrong... looked at it after posting. Wouldn't it being dynamic depend on the rest of the code?
Dynamic would be the ability to create the depth of the array according to the length of the $ids array, with each sub-array having the key of the requisite id. The question the user is asking is how to create the array dynamically.. in the previously noted way. This is what the OP is asking.
Pretty sure it isn't, unless it outputs array(1247 => array(1248 => array(1249 => array(1250 => array(1251 => array('Thursday'=> 0))))))
This is what the OP has been looking for :D!
1

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;

Comments

0

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.

1 Comment

Answers are reserved for attempts to answer the question. This makes no attempt to answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.