Solved

PHP shift_array multi dimension

Posted on 2008-10-28
4
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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