2

I have the below code in a for..loop is there a way I can add values to the beginning of the array?

    $data = array();

    $initial = strtotime('11:00:00');
    for (; $initial < strtotime("23:00:59");  $initial = strtotime("+15 minutes", $initial)) {
        if ($initial > strtotime("+45 minutes", time())) {
            $row['value'] = date('Hi', $initial);
            $row['label'] = date('H:i', $initial);
            $data['data'][] = $row;
        }
    }

I want to add the below values to the top of the array. I have tried using array_unshift but I don't think it supports key-value pairs.

    if(!isBetween('22:00', '09:59', date('H:i'))) {
        $row['value'] = "asap";
        $row['label'] = "ASAP";
    }

My array output

{
  "data": [
    {
      "value": "1145",
      "label": "11:45"
    }
  ]
}

I want to get this

{
  "data": [
    {
      "value": "asap",
      "label": "ASAP"
    },{
      "value": "1145",
      "label": "11:45"
    },
  ]
}
moh_abk
  • 2,064
  • 7
  • 36
  • 65
  • 1
    Possible duplicate of [PHP prepend associative array with literal keys?](http://stackoverflow.com/questions/1371016/php-prepend-associative-array-with-literal-keys) – Chetan Ameta Dec 22 '15 at 10:49
  • It's not a duplicate.. Tried everything there – moh_abk Dec 22 '15 at 10:49
  • Can you post your array structure along with expected output – Narendrasingh Sisodia Dec 22 '15 at 10:51
  • "I want to add the below values to the top of the array." - that would take place instead of `$data['data'][] = $row;`? – VolkerK Dec 22 '15 at 10:54
  • I have posted the array @Uchiha – moh_abk Dec 22 '15 at 10:55
  • `date('H:i')` - that uses the current time, not the value of $initial – VolkerK Dec 22 '15 at 11:00
  • My code has no problem. I just simply want to add to the top of my array @VolkerK – moh_abk Dec 22 '15 at 11:02
  • "have tried using array_unshift but I don't think it supports key-value pairs." - there doesn't seem to be any key/value-pair involved in the actual shift operation. The _element_ you want to unshift being an array with key/value pairs is irrelevant. – VolkerK Dec 22 '15 at 11:03
  • So you're saying `"value": "asap", "label": "ASAP"` is not key-value pair? @VolkerK – moh_abk Dec 22 '15 at 11:09
  • I say you're not unshifting key/value pairs, but an _array_ _containing_ key/value pairs. And therfore: [norepo](https://3v4l.org/Kalm0) – VolkerK Dec 22 '15 at 11:11

2 Answers2

3

Un-shift should work if you pass the arguments correctly:

array_unshift($data["data"], $prepend);

Alternatively, you could use array_merge, like this:

$data["data"] = array_merge(array($prepend), $data["data"]);

With the following example data:

$data = [
    "data" => [
        [
            "value" => "1145",
            "label" => "11:45"
        ]
    ]
];

$prepend = [
    "value" => "asap",
    "label" => "ASAP"
];

$data["data"] = array_merge(array($prepend), $data["data"]);
print_r($data);

You would get this output (with both solutions):

Array (
    [data] => Array (
            [0] => Array (
                    [value] => asap
                    [label] => ASAP
                )
            [1] => Array (
                    [value] => 1145
                    [label] => 11:45
                )
        )
)
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    NB: But you could also use unshift still. For me this works: `array_unshift($data["data"], $prepend); ` – trincot Dec 22 '15 at 11:14
0

If you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:

function array_unshift_assoc(&$arr, $key, $val) { 
    $arr = array_reverse($arr, true); 
    $arr[$key] = $val; 
    return array_reverse($arr, true); 
} 

Source: http://php.net/manual/en/function.array-unshift.php

Miro
  • 8,402
  • 3
  • 34
  • 72
Tredged
  • 586
  • 2
  • 17