0

i have two arrays and i need to extract the values of the 2nd array depending on the value of $arr[0]["num"]

$arr = array(
0 => array(
    "id" => 24,
    "num" => 2
),
1 => array(
    "id" => 25,
    "num" => 5
)
2 => array(
    "id" => 26,
    "num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
    for($i=0;$i<$key['num'];$i++){
        $new[$key['id']][$i] = $array[$i];
    }
}

is it possible to remove the values of the 2nd array and transfer it into a new array?

what my loop does is just copying the values from the start after each loop. i want to remove the copied values from the 2nd array.

The output should be like this:

Array
(
[24] => Array
    (
        [0] => 1
        [1] => 2
    )

[25] => Array
    (
        [0] => 3
        [1] => 4
        [2] => 5
        [3] => 6
        [4] => 7
    )
[26] => Array
    (
        [0] => 8
        [1] => 9
        [2] => 10
    )
)

2 Answers 2

1

I'd suggest using array_shift

$arr = array(
  array(
    "id" => 24,
    "num" => 2
),
  array(
    "id" => 25,
    "num" => 5
),
  array(
    "id" => 26,
    "num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
    for($i=0;$i<$key['num'];$i++){
        $new[$key['id']][$i] = $array[0]; // *1
        array_shift($array);
    }
}
echo '<pre>';
print_r($new);

*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].

Output:

Array
(
    [24] => Array
        (
            [0] => 1
            [1] => 2
        )

    [25] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 6
            [4] => 7
        )

    [26] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 10
        )

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

Comments

1

Try this

foreach($arr as $key){
    for($i=0;$i<$key['num'];$i++){
        $new[$key['id']][$i] = $array[$i];

        // unset previous values, in first iteration it will remove 0, 1
        unset($array[$i]);
    }
    // reset the array keys, so for loop $i will start from 0
    $array = array_values($array);  
}

Output:

array (size=3)
  24 => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
  25 => 
    array (size=5)
      0 => string '3' (length=1)
      1 => string '4' (length=1)
      2 => string '5' (length=1)
      3 => string '6' (length=1)
      4 => string '7' (length=1)
  26 => 
    array (size=3)
      0 => string '8' (length=1)
      1 => string '9' (length=1)
      2 => string '10' (length=2)

Comments

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.