[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

rearrange the php array selectively.

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
heng tang
Asked:
heng tang
  • 6
  • 3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
heng tangPHP DeveloperAuthor Commented:
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
 
heng tangPHP DeveloperAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
Dave BaldwinFixer of ProblemsCommented:
$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
 
heng tangPHP DeveloperAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
That doesn't make any sense.  If you know what they are because you have "predefined" them, just display them.
0
 
heng tangPHP DeveloperAuthor Commented:
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
 
heng tangPHP DeveloperAuthor Commented:
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
 
Ray PaseurCommented:
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
 
heng tangPHP DeveloperAuthor Commented:
found solution myself
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now