Solved

PHP shift_array multi dimension

Posted on 2008-10-28
4
849 Views
Last Modified: 2010-05-18
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
Comment
Question by:encus
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
shanikawm earned 300 total points
ID: 22819739
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
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 200 total points
ID: 22819741

<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
 
LVL 8

Assisted Solution

by:shanikawm
shanikawm earned 300 total points
ID: 22819758
Refer following too.

http://www.php.net/array_shift
0
 

Author Closing Comment

by:encus
ID: 31510637
Thanks all I have configure it. It was due to my while loop issue and now it works properly
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now