Solved

rearrange the php array selectively.

Posted on 2014-09-28
10
119 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 82

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 82

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 40
How can I expand text with explaination 5 21
sql sentence 2 12
SQL inner join confusion 15 21
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

23 Experts available now in Live!

Get 1:1 Help Now