1

I am new to php. I am trying to get key and value of each element in special array. I want to save/push them into another array start from zero index. Of course (foreach loop) is the best tool here. Please help me BY YOUR CODE. I know describing this problem is a bit hard, so I have some drawings for you.

$info = array(
    <--key-->     <--value--> 
    [10] => 'apple',
    [8] => 'orange',
    [2] => 'banana',
    [23] => 'peach',
)

now I want to have an array like this:

$myGoal = array(
  <-key->   <--------value---------> 
    [0] => array('10'=>'apple'),
    [1] => array('8'=>'orange'),
    [2] => array('2'=>'banana'),
    [3] => array('23'=>'peach')
)

I guess the code I need should be something like below:

$info = array(
    [10] => 'apple',
    [8] => 'orange',
    [2] => 'banana',
    [23] => 'peach',
)

$index = 0;
$myGoal = array();
foreach($info as $amount => $friut){
     $myGoal[$index] = [$amount][$friut];
     $index++;
}

I'll be thank of anybody can help me.

4 Answers 4

3

About the syntax $myGoal[$index] = [$amount][$friut];

The part after the equals sign is creating an array with a single value of $amount and then tries to index into the array with the value of $friut which does not exists.

You could update the syntax to add an array to the index with key => value

Change

$myGoal[$index] = [$amount][$friut];

to

$myGoal[$index] = [$amount => $friut];

Output

Array
(
    [0] => Array
        (
            [10] => apple
        )

    [1] => Array
        (
            [8] => orange
        )

    [2] => Array
        (
            [2] => banana
        )

    [3] => Array
        (
            [23] => peach
        )

)

Php demo

As @kaczmen points out, if you only want to append you don't need the index and you could use

$myGoal[] = [$amount => $friut];
Sign up to request clarification or add additional context in comments.

1 Comment

You can also drop $index variable and make it $myGoal[] = ... if you only care about appending.
1

Firstly you intialize an array after that store the key value into a new array.

$info = array("10" => 'apple',"8" => 'orange',"2" => 'banana',"23" => 'peach');
$new=array();
foreach($info as $key=>$info1)
{
    $new[] = [$key=>$info1];
}
echo "<pre>";
print_r($new);

Hope This Helps.

Comments

0

You can do this with array_chunk using a chunk size of 1.

$myGoal = array_chunk($info, 1, true);

Be sure to set the third argument to true to preserve the keys.


Also, not directly related to what you're asking, but I'd like to suggest that using amount as an array key is risky. If you ever have the same amount of two items, the second one will overwrite the first.

1 Comment

your idea was the shortest way and the result is OK, tnx
0

You can use array_walk, You do not need to declare the $index=0 if you want to start it by 0. You can just use $n[] it will automatic start from 0 index

 array_walk($info,function($v,$k)use(&$n){$n[]=[$k=>$v];});
 print_r($n);

Working example :- https://3v4l.org/DTACm

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.