Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answeranswer might also interest you.


Would something like this work?
function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

This was the best I could come up with:

$a = array
(
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
);

ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2

function array_insert(&$array, $key, $data)
{
    $k = key($array);

    if (array_key_exists($key, $array) === true)
    {
        $key = array_search($key, array_keys($array)) + 1;
        $array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true);

        while ($k != key($array))
        {
            next($array);
        }
    }
}

ph()->Dump($a);

/*
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [new_item] => 100
    [five] => 5
    [six] => 6
)
*/

I don't think it's possible to set an array internal pointer without looping.

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?
function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

This was the best I could come up with:

$a = array
(
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
);

ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2

function array_insert(&$array, $key, $data)
{
    $k = key($array);

    if (array_key_exists($key, $array) === true)
    {
        $key = array_search($key, array_keys($array)) + 1;
        $array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true);

        while ($k != key($array))
        {
            next($array);
        }
    }
}

ph()->Dump($a);

/*
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [new_item] => 100
    [five] => 5
    [six] => 6
)
*/

I don't think it's possible to set an array internal pointer without looping.

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?
function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

This was the best I could come up with:

$a = array
(
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
);

ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2

function array_insert(&$array, $key, $data)
{
    $k = key($array);

    if (array_key_exists($key, $array) === true)
    {
        $key = array_search($key, array_keys($array)) + 1;
        $array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true);

        while ($k != key($array))
        {
            next($array);
        }
    }
}

ph()->Dump($a);

/*
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [new_item] => 100
    [five] => 5
    [six] => 6
)
*/

I don't think it's possible to set an array internal pointer without looping.

added 1067 characters in body; added 51 characters in body
Source Link
Alix Axel
  • 153.6k
  • 99
  • 399
  • 504

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?
function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

Would something like this work?This was the best I could come up with:

$a = array
(
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
);

ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2

function array_insert($input&$array, $key, $value$data)
{
    $k = key($array);

    if (array_key_exists($key, $array) === true)
    {
        $key = array_search($key, array_keys($input)$array)) !==+ false)1;
    {
    $array = array_slice($array, null, return$key, array_splicetrue) + $data + array_slice($input$array, $key, 1null, $valuetrue); 

    }    while ($k != key($array))
        {
    return $input;       next($array);
        }
    }
}

ph()->Dump($a);

/*
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [new_item] => 100
    [five] => 5
    [six] => 6
)
*/

I don't think it's possible to set an array internal pointer without looping.

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?

function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?
function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

This was the best I could come up with:

$a = array
(
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
);

ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2

function array_insert(&$array, $key, $data)
{
    $k = key($array);

    if (array_key_exists($key, $array) === true)
    {
        $key = array_search($key, array_keys($array)) + 1;
        $array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true); 

        while ($k != key($array))
        {
            next($array);
        }
    }
}

ph()->Dump($a);

/*
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [new_item] => 100
    [five] => 5
    [six] => 6
)
*/

I don't think it's possible to set an array internal pointer without looping.

added 189 characters in body; added 11 characters in body; added 79 characters in body
Source Link
Alix Axel
  • 153.6k
  • 99
  • 399
  • 504

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?

function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.

Maybe I'm not understanding you correctly but have you looked into array_splice()?

This answer might also interest you.


Would something like this work?

function array_insert($input, $key, $value)
{
    if (($key = array_search($key, array_keys($input))) !== false)
    {
        return array_splice($input, $key, 1, $value);
    }

    return $input;
}
Source Link
Alix Axel
  • 153.6k
  • 99
  • 399
  • 504
Loading