Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP shift_array multi dimension

Posted on 2008-10-28
4
Medium Priority
?
891 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 1200 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 800 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 1200 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

Technology Partners: 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!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

705 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