Solved

rearrange the php array selectively.

Posted on 2014-09-28
10
122 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now