1

This is not the explode can solve

I have several array

$order data: {5,3,2} $title, data: {USA, England, China} $attribute Same idea $type Same idea 

The $order has already exist some value
{5,3,2} and for each has its $title,$attribute,$type value correspondingly

eg. 5 USA att5 type5 

I would like to sort the order to {2,3,5} and the corresponding data in other array will be sorted too.

eg. {2,3,5} 

For the array $title afterwards

is

{China, England,USA} 

How to implement this for all arrays? Thank you

My idea is using an associative array , and i can sort the key, and everything is done. However, I can not generate the array

My idea array:

$records = array(5 => array("title" => "USA", "att" => "add5"), 3 => array("title" => "England", "att" => "add3"), 2 => array("title" => "China", "att" => "add2")); 
0

5 Answers 5

1
<?php $order = array(5, 3, 2); // or $order = array("5", "3", "2"); $title = array("USA", "England", "China"); $att = array("Att 1", "Att 2", "Att 3"); $type = array("Type 1", "Type 2", "Type 3"); $records = array(); foreach ($order as $i => $o) $records[$o] = array("title" => $title[$i], "att" => $att[$i], "type" => $type[$i]); ksort($records, SORT_NUMERIC); print_r($records); ?> 
Sign up to request clarification or add additional context in comments.

Comments

1

After these lines on your script

$string = substr($string, 0, -1); $records="array (".$string.");"; 

You can add

eval("\$records = $records;"); 

You can read about the eval function at http://php.net/manual/es/function.eval.php and why you should be really careful when using it

7 Comments

first of all, it looks like this will not work with the input OP shown. secondly, when in doubt - do not use eval
"looks like" => enough to vote down? And if eval is a practical solution, why not point it out? Especially if a warning accompanies it's use...as it does.
@Stefan: if you look at the input (above), you might see what i mean by "looks like"
@Stefan and regarding eval being practical, please, reconsider... can you be sure what outcome will it do when input (even slightly) changes?
@poncha: "looks like" means "looks like" in my dictionary - it doesn't mean "confirmed wrong". And with "practical" I mean "working in a specific case", and if it works in this case, why not point it out? That said, I wouldn't do it myself and furthermore it "looks like" the quotes and double quotes in the string will raise a parse error (unconfirmed though).
|
1

where one explode is not enough, two explodes can help ;)

$records = Array(); foreach(explode("\n", trim($string)) as $line) { list($order,$title,$attribute,$type) = explode(",", $line); $records[$order] = Array("title" => $title, "attribute" => $attribute, "type" => $type); } ksort($records); 

3 Comments

that's assuming the input is the string like you shown - a lot of lines in format $order,$title,$attribute,$type. if not, then consider a different approach.
now that your intentions are clear, please, look at @Tarik's answer about multisort.
Thanks, but it seems i need not using this if i have created an array
1

things are now more clear for this i recommend using array_multisort http://php.net/manual/en/function.array-multisort.php

2 Comments

My idea was using assositive array, wouldn't it easier to implement, all i need is ksort, but the problem is I still can not generate the array .
:) yes, and You will get the same output from the code in the link
1

instead of converting from a string to array you can build it

$result = array(); $countItem=0; foreach ($order as $itemID) { $result [$countItem] = array('id' => $itemID, 'title' => $title[$countItem], 'attribute' => $att[$countItem],'type'=>$type[$countItem]); $countItem++; } 

and then sort it by id

8 Comments

one comment: single quotes will not work here. you will get a lot of lines with literal values like $title[$countItem]...
You will have an array $order which you can return
what did it return? and did you initialize the $countItem
sure , and i tried type in the countItem manually, it return Array ( [0] => Array ( 'title' =>'title1', 'attribute' => 'att1' 'type' => 'txt' ) ) but it different from what i need.
It would be appreciated if you clear up what is the purpose of this code so we can understand and help better
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.