Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

PHP shift_array multi dimension

Hi All,

Just to check if anyone has an idea on how to do array_shift() function in php for multi dimension. For example:

$tmpArr[0][name] = 'myname'
$tmpArr[0][gender] = 'male'

In this situation if I use array_shift(), I would only get 'myname' instead of both value. Could somebody help me?
0
encus
Asked:
encus
  • 2
3 Solutions
 
shanikawmCommented:
Look at this simple example code. You may get a clear idea. This prints

Original Array
--------------

Array
(
    [0] => Array
        (
            [name] => myname
            [gender] => male
        )

    [1] => Array
        (
            [name] => friendname
            [gender] => female
        )

)

Shifted Part
---------------

Array
(
    [name] => myname
    [gender] => male
)

After Shifting
----------------

Array
(
    [0] => Array
        (
            [name] => friendname
            [gender] => female
        )

)

<?php
$tmpArr[0]['name'] = 'myname';
$tmpArr[0]['gender'] = 'male';
$tmpArr[1]['name'] = 'friendname';
$tmpArr[1]['gender'] = 'female';
 
echo "Original Array\n--------------\n\n";
 
print_r($tmpArr);
 
echo "\nShifted Part\n---------------\n\n";
 
print_r(array_shift($tmpArr));
 
echo "\nAfter Shifting\n----------------\n\n";
 
print_r($tmpArr);
?>

Open in new window

0
 
ddrudikCommented:

<pre>
<?php
$tmpArr[0]['name'] = 'myname0';
$tmpArr[0]['gender'] = 'male0';
$tmpArr[1]['name'] = 'myname1';
$tmpArr[1]['gender'] = 'male1';
echo '$tmpArr '.print_r($tmpArr,true);
echo 'first array_shift '.print_r(array_shift($tmpArr),true);
echo 'second array_shift '.print_r(array_shift($tmpArr),true);
?>
 
output:
$tmpArr Array
(
    [0] => Array
        (
            [name] => myname0
            [gender] => male0
        )
 
    [1] => Array
        (
            [name] => myname1
            [gender] => male1
        )
 
)
first array_shift Array
(
    [name] => myname0
    [gender] => male0
)
second array_shift Array
(
    [name] => myname1
    [gender] => male1
)

Open in new window

0
 
shanikawmCommented:
Refer following too.

http://www.php.net/array_shift
0
 
encusAuthor Commented:
Thanks all I have configure it. It was due to my while loop issue and now it works properly
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now