0

i have an array like this

Array
(
    [0] => Array
        (
            [s_id] => 4
            [si_id] => sec_1
            [d_id] => dep_4
            [s_name] => sec1
            [s_location] => LA
            [s_visibility] => yes
            [s_created_date] => 1273639343
            [s_last_updated_date] => 1273639343
            [s_created_by] => someone
            [s_last_updated_by] => everyone
        )

)

now i want to extract array[0] into an array... means i want this

Array
(
            [s_id] => 4
            [si_id] => sec_1
            [d_id] => dep_4
            [s_name] => sec1
            [s_location] => LA
            [s_visibility] => yes
            [s_created_date] => 1273639343
            [s_last_updated_date] => 1273639343
            [s_created_by] => someone
            [s_last_updated_by] => everyone

)

how do i get above results?

2 Answers 2

5

You can do:

$newArray = $oldArray[0];

This will create a new array with the same key-value pairs.

If you do not want to create a new array and want the new array to refer to the existing array in the $oldArray you can do:

$newArray = &$oldArray[0];

Any changes made to $newArray will also change $oldArray in this case.

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

2 Comments

Thanks for quick reply.is it not possible by refernecing?
No need to add another answer... $newArray = &$oldArray[0];
0

See this it may useful for you,

$sss = array () ;
$sss['sadness']['info'] = "some info";
$sss['sadness']['info2'] = "more info";
$sss['sadness']['value'] = "value";
$sss['happiness']['info'] = "some info";
$sss['happiness']['info2'] = "more info";
$sss['happiness']['value'] = "value";
$sss['peace']['info'] = "some info";
$sss['peace']['info2'] = "more info";
$sss['peace']['value'] = "value";

print_r($sss['sadness']);
echo "<br>";

print_r($sss);
echo "<br>";

Output 1 :

Array ( [info] => some info [info2] => more info [value] => value )

Output 2 :

Array ( [sadness] => Array ( [info] => some info [info2] => more info [value] => value ) [happiness] => Array ( [info] => some info [info2] => more info [value] => value ) [peace] => Array ( [info] => some info [info2] => more info [value] => value ) ) 

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.