-2

I need small doubt in PHP. how to get below PHP array data in to variables.

$arraydata = Array
(
    [366] => 1084569.5892969
    [181TO365] => -2128157.619635
    [121TO180] => -59235.780429687
    [91TO120] => -266089.29
    [61TO90] => -56390
    [0TO60] => 8212872.9800098
)

This is my array output data.i need to get these data in to as variables like below.

$366data= 1084569.5892969;

$181TO365data = -2128157.619635;

$121TO180data = -59235.780429687;

$91TO120data = -266089.29;

$61TO90data = -56390;

$0TO60data = 8212872.9800098; Like this variables get data . Can anyone help

2

1 Answer 1

1

Apart from the fact that PHP variables cannot start with a number (_ or letter) you can just loop over it like so:

<?php

foreach( $array as $key => $value ) {
  $$key = $value;
}  

The $$ before key means that you assign the value of $key to a variable with that name. But you probably to prefix it with an _ to make this work.

For example:

<?php

foreach( $array as $key => $value ) {
  $prefixed = '_' . $key;
  $$prefixed = $value;
}

Why do you want this by the way?

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

1 Comment

From this code how can I take each variable separately.

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.