Solved

rearrange the php array selectively.

Posted on 2014-09-28
10
126 Views
Last Modified: 2014-10-04
I have the following PHP array and want to rearrange their array based on the selection of the value.
I want to name for these 3 become the first element of the array and keep the rest of the elements as it is.
Whitesoot , Seqoci, Kei Mag


Array
(
    [1] => Array
        (
            [merchant_id] => 1
            [name] => Poplook
        )

    [3] => Array
        (
            [merchant_id] => 3
            [name] => Phatculture
        )

    [4] => Array
        (
            [merchant_id] => 4
            [name] => PrettyChase
        )

    [6] => Array
        (
            [merchant_id] => 6
            [name] => Seqoci
                    )

    [8] => Array
        (
            [merchant_id] => 8
            [name] => Whitesoot
        )

    [10] => Array
        (
            [merchant_id] => 10
            [name] => Su Estilo
        )

    [12] => Array
        (
            [merchant_id] => 12
            [name] => Kei Mag
        )

    [13] => Array
        (
            [merchant_id] => 13
            [name] => Emcee Couture
        )

    [17] => Array
        (
            [merchant_id] => 17
            [name] => Nile
        )

    [14] => Array
        (
            [merchant_id] => 14
            [name] => Zalora
        )

    [22] => Array
        (
            [merchant_id] => 22
            [name] => Tuesday Couture
        )

    [18] => Array
        (
            [merchant_id] => 18
            [name] => Say Fashion
        )

    [23] => Array
        (
            [merchant_id] => 23
            [name] => Tudung2u


)

Open in new window


Example result i want is as below whereby u can see the first 3 elements are Whitesoot , Seqoci and Kei Mag.
I am thinking to use in_array, example $first3element = array("Whitesoot", "Seqoci", "Kei Mag") and then use in_array for comparisoon. But I don't know how to sort them out.:
Array
(
    [1] => Array
        (
            [merchant_id] => 1
            [name] => Whitesoot
        )

    [3] => Array
        (
            [merchant_id] => 3
            [name] => Seqoci
        )

    [4] => Array
        (
            [merchant_id] => 4
            [name] => Kei Mag
        )

    [6] => Array
        (
            [merchant_id] => 6
            [name] => Phatculture
                    )

    [8] => Array
        (
            [merchant_id] => 8
            [name] => Poplook
        )

    [10] => Array
        (
            [merchant_id] => 10
            [name] => Su Estilo
        )

    [12] => Array
        (
            [merchant_id] => 12
            [name] => PrettyChase
        )

    [13] => Array
        (
            [merchant_id] => 13
            [name] => Emcee Couture
        )

    [17] => Array
        (
            [merchant_id] => 17
            [name] => Nile
        )

    [14] => Array
        (
            [merchant_id] => 14
            [name] => Zalora
        )

    [22] => Array
        (
            [merchant_id] => 22
            [name] => Tuesday Couture
        )

    [18] => Array
        (
            [merchant_id] => 18
            [name] => Say Fashion
        )

    [23] => Array
        (
            [merchant_id] => 23
            [name] => Tudung2u


)

Open in new window

0
Comment
Question by:heng tang
  • 6
  • 3
10 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40349305
Your original array is a two-dimensional array and your $first3element = array("Whitesoot", "Seqoci", "Kei Mag") is a one dimensional array which will have the 'keys' "0,1,2".  You can't really "get there from here".

What is it you're trying to do (other than rearrange things)?
0
 

Author Comment

by:heng tang
ID: 40349317
Hi Dave,

I am thinking to compare the element of 'name' from each array element with the $first3element .If found in $first3element  array, array_unshift them to the begining of the arrary to produce the desire result.
0
 

Author Comment

by:heng tang
ID: 40349336
I don't mind if we need to change the $first3element to $first3element = array(0=>'Whitesoot', 1 => 'Secoqi', 2 => 'Kei Mag') as  a 2 dimensional array as long as it can produce the desire result;
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40349346
$first3element = array(0=>'Whitesoot', 1 => 'Secoqi', 2 => 'Kei Mag') is still one dimensional and exactly the same as $first3element = array("Whitesoot", "Seqoci", "Kei Mag").  The default 'keys' are numbers starting with 0.

Forget about rearranging them for a moment.  What is the purpose or 'business' result you're trying to accomplish?
0
 

Author Comment

by:heng tang
ID: 40349376
I want to be able to display the predefined 3 elements as the top 3 of the array out of the original array.And the predefined top 3 element is selectable based on the name element of the arrays
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40349392
That doesn't make any sense.  If you know what they are because you have "predefined" them, just display them.
0
 

Author Comment

by:heng tang
ID: 40349427
you will know what I intend to do if you look carefully at the code above, comparing the original array vs the desire array.
0
 

Accepted Solution

by:
heng tang earned 0 total points
ID: 40349453
its OK David, i got the idea already..
I will create 2 empty array. If the element value is in_array of the $first3element , then will push to the 1st array, else will push to the 2nd array. In the end I will array_merge these two arrays.

Settled..
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40349885
Nobody does this - it's not a design pattern in programing.  The data would probably be in a database anyway.  That's why Dave Baldwin was trying to get to the underlying thinking with this question: "What is the purpose or 'business' result you're trying to accomplish?"

The expected design pattern would be a generalized lookup function, something like this code snippet.  But that said, you might want to look for an introductory programming class at a nearby college.  If you want to try to learn PHP programming yourself (and it will take a lot longer this way) you might find some useful guidance in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

<?php // demo/temp_heng_tang.php
error_reporting(E_ALL);

function lookup ($name=NULL)
{
    static
    $array
    = array
    ( array( 'merchant_id' => '1', 'name' => 'Poplook'      )
    , array( 'merchant_id' => '3', 'name' => 'Phatculture'  )
    , array( 'merchant_id' => '4', 'name' => 'PrettyChase'  )
    , array( 'merchant_id' => '6', 'name' => 'Seqoci'       )
    )
    ;
    foreach ($array as $sub_array)
    {
        if ($name == $sub_array['name']) return $sub_array['merchant_id'];
    }
    return FALSE;
}

var_dump( lookup('Seqoci') );
var_dump( lookup('Foobar') );

Open in new window

Good luck with your project, ~Ray
0
 

Author Closing Comment

by:heng tang
ID: 40361034
found solution myself
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

776 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