0

I want to delete an index from array and insert it into in new array. I want two things which i tried to explain one is

Array
    (
        [index1] => Deleted
        [index4] => Inserted
    )
     Array
(

        [index3] => test
        [index4] => Inserted
    )
     Array
    (

        [index2] => numbers
        [index3] => test
        [index4] => Inserted
    )
     Array
    (
        [index1] => Deleted

    )

now i want if arraysize is 1

 foreach($array as $arrays){
    array_push($array1,($arrays[0]));
      unset ($arrays[0]);

 }

i want to remove

Array
    (
        [index1] => Deleted

    )

from $array and $array to be

 [index1] => Deleted

second is if $array is

Array
(

    [index2_123] => numbers
    [index3_level] => test
    [index4_test] => Inserted
)

i want a new array with $array1 as

Array
(

    [index3_level] => test

)

and $array1 is modified to

Array
(

    [index2_123] => numbers
    [index4_test] => Inserted
)
13
  • $array1=array(); array_merge($array1,$array[2]); unset($array[2]); //try this Commented May 29, 2015 at 9:14
  • should also unset $array[4] Commented May 29, 2015 at 9:15
  • this is not even transfering $array[2] value into $array1 Commented May 29, 2015 at 9:17
  • So you want to split array according to even and odd index?? Commented May 29, 2015 at 9:20
  • no not for odd and even . I want to do it for any index Commented May 29, 2015 at 9:24

4 Answers 4

2

Try this way,

$arr = Array
    (
        'index1' => 'Deleted',
        'index2' => 'numbers',
        'index3' => 'test',
        'index4' => 'Inserted'
    );
$arr1 = $arr2 = array();
$i = 0;
foreach($arr as $key => $value){
    if($i%2 == 0){
        $arr1[$key] = $value;
    }else{
        $arr2[$key] = $value;
    }
    $i++;
}

Output

$arr1
Array
(
    [index1] => Deleted
    [index3] => test
)

$arr2
Array
(
    [index2] => numbers
    [index4] => Inserted
)

And if you don't need that value then you can use it as

$i = 0;
foreach($arr as $key => $value){
    if($i%2 == 0){
        $arr[$key] = $value;
    }else{
        unset($arr[$key]);
    }
    $i++;
}
print_r($arr);

Output:

Array
(
    [index1] => Deleted
    [index3] => test
)
Sign up to request clarification or add additional context in comments.

Comments

1

Loop through them and generate the array -

$new = array();
foreach($yourarray as $key => $val) {
    $index = str_replace('index', '', $key); // get the key index
    if($index % 2 != 0) { // check for odd or even
        $new[$key] = $val; // set the new array
        unset($yourarray[$key]); // delete from the main array
    }
}

Update

For any index use a counter

$i = 0;
$new = array();
foreach($yourarray as $key => $val) {
    if($i % 2 != 0) { // check for odd or even
        $new[$key] = $val; // set the new array
        unset($yourarray[$key]); // delete from the main array
    }
    $i++;
}

1 Comment

hi i don't want to specify it for odd or even . it can be any index
1

You can use a combination of array_flip and array_diff_key to filter the first array, then use array_diff filter the second:

$specificIndex = array('index1', 'index3');
$array1 = array_diff_key($array, array_flip($specificIndex));
$array2 = array_diff($array, $array1);

Demo.

If you want get in an array only certain elements of your choice you can do something like:

$specificIndex = array('index1', 'index3');
$selectedItem = array_intersect_key($array, array_flip($specificIndex));

Demo.

1 Comment

<?php $array = array( 'index1' => 'Deleted', 'index2' => 'numbers', 'index3' => 'test', 'index4' => 'Inserted', ); $specificIndex = array('index1', 'index3'); $array1 = array_diff_key($array, array_flip($specificIndex)); $array2 = array_diff($array, $array1); print_r($array1); print_r($array2); instead if $array2 can we have updated value in $array
0
<?php
$array = array(
        'index1' => 'Deleted',
        'index2' => 'numbers',
        'index3' => 'test',
        'index4' => 'Inserted',
);
$specificIndex = 'index3';
$array1=array();
foreach($array as $key => $value){
if($key==$specificIndex){
 $array1[$key] = $value;
unset($array[$specificIndex]);
}
}
print_r($array);
print_r($array1);

http://3v4l.org/TvZ19

2 Comments

Add more information on how this answers the OP's question.
$specificIndex taking only one value at a time how can more indexes can be given to it at a time $specificIndex = 'index2','index3';

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.