Solved

PHP shift_array multi dimension

Posted on 2008-10-28
4
840 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

12 Experts available now in Live!

Get 1:1 Help Now