Solved

PHP shift_array multi dimension

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 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 …

635 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