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
Solved

rearrange the php array selectively.

Posted on 2014-09-28
10
129 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

861 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