0

I want do like this:

$m_array = array();

foreach ($values as $key) { <- $key is just string
    array_push ( $m_array, $key => array() );
}

/////result
$m_array = array(
    "key1" => array(),
    "key2" => array(),
    ....
);

How to do this?

Please help me.

I use PHP.

2
  • whats in the $values aray? this question is not clear Commented Dec 13, 2016 at 4:01
  • Im sorry. $value is just array. the $key is "apple", "melon", "banana".... like this each loop Commented Dec 13, 2016 at 4:03

1 Answer 1

1

You don't need to use array_push to append to arrays. array_push is the same as $array[] = $val.

In your case you want to specify keys:

$m_array = array();

foreach ($values as $key) { <- $key is just string
    $m_array[$key] = array();
}

Note that array_push does have a use if you want to push more than one value at once because you can do something like this:

array_push($array, $value1, $value2, $value3)
Sign up to request clarification or add additional context in comments.

2 Comments

$key here is not the array key; which may or may not be an issue
Yes, $key here refers to a value of $values which will be a key in the new array - Not confusing at all! /s

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.