PHP: Sort Multidimensional Array Based on Term in relation to a value of a key











up vote
0
down vote

favorite












I Have the following Array and I would like to sort it based on the $term = "geo" in relation only to [result_title] ?



Array
(
[0] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[1] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)

[2] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee
)

[3] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)
)


The desired result should be:



Array
(
[0] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee

)

[1] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee


)

[2] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)

[3] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)
)


I have tried various methods such as:



usort($data, function($a, $b) use ($term) {
$x = strpos($a["result_title"], $term) === false;
$y = strpos($b["result_title"], $term) === false;
if ($x && !$y) return 1;
if ($y && !$x) return -1;

// use this if you want to sort alphabetically after the keyword sort:
return strcmp($a["result_title"], $b["result_title"]);

// or if you only want to sort by whether or not the keyword was found:
return 0;
});


and



usort($data, function ($a, $b) use ($term) {
similar_text($term, $a["result_title"], $percentA);
similar_text($term, $b["result_title"], $percentB);

return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
});


and



usort($data, function ($a, $b) use ($term) {
$levA = levenshtein($term, $a["result_title"]);
$levB = levenshtein($term, $b["result_title"]);

return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1);
});


and



usort($data, function($a, $b){ return $a["result_title"] - $b["result_title"]; }); 


and many more without any proper result. Or maybe I cannot understand the method to achieve my result?



I have also checked: php sorting an array based on a string and How to sort an array by similarity in relation to an inputted word. but the answers are giving me the result I'm looking for.










share|improve this question


















  • 1




    can you provide your source array as PHP code?
    – Marcin Orlowski
    Nov 10 at 22:16










  • @MarcinOrlowski you mean how the array is formed at first?
    – jQuerybeast
    Nov 10 at 22:23










  • it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
    – Marcin Orlowski
    Nov 10 at 22:26










  • Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
    – jQuerybeast
    Nov 10 at 22:28










  • @MarcinOrlowski I hope this is what you meant :S
    – jQuerybeast
    Nov 10 at 22:29















up vote
0
down vote

favorite












I Have the following Array and I would like to sort it based on the $term = "geo" in relation only to [result_title] ?



Array
(
[0] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[1] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)

[2] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee
)

[3] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)
)


The desired result should be:



Array
(
[0] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee

)

[1] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee


)

[2] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)

[3] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)
)


I have tried various methods such as:



usort($data, function($a, $b) use ($term) {
$x = strpos($a["result_title"], $term) === false;
$y = strpos($b["result_title"], $term) === false;
if ($x && !$y) return 1;
if ($y && !$x) return -1;

// use this if you want to sort alphabetically after the keyword sort:
return strcmp($a["result_title"], $b["result_title"]);

// or if you only want to sort by whether or not the keyword was found:
return 0;
});


and



usort($data, function ($a, $b) use ($term) {
similar_text($term, $a["result_title"], $percentA);
similar_text($term, $b["result_title"], $percentB);

return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
});


and



usort($data, function ($a, $b) use ($term) {
$levA = levenshtein($term, $a["result_title"]);
$levB = levenshtein($term, $b["result_title"]);

return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1);
});


and



usort($data, function($a, $b){ return $a["result_title"] - $b["result_title"]; }); 


and many more without any proper result. Or maybe I cannot understand the method to achieve my result?



I have also checked: php sorting an array based on a string and How to sort an array by similarity in relation to an inputted word. but the answers are giving me the result I'm looking for.










share|improve this question


















  • 1




    can you provide your source array as PHP code?
    – Marcin Orlowski
    Nov 10 at 22:16










  • @MarcinOrlowski you mean how the array is formed at first?
    – jQuerybeast
    Nov 10 at 22:23










  • it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
    – Marcin Orlowski
    Nov 10 at 22:26










  • Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
    – jQuerybeast
    Nov 10 at 22:28










  • @MarcinOrlowski I hope this is what you meant :S
    – jQuerybeast
    Nov 10 at 22:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I Have the following Array and I would like to sort it based on the $term = "geo" in relation only to [result_title] ?



Array
(
[0] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[1] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)

[2] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee
)

[3] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)
)


The desired result should be:



Array
(
[0] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee

)

[1] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee


)

[2] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)

[3] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)
)


I have tried various methods such as:



usort($data, function($a, $b) use ($term) {
$x = strpos($a["result_title"], $term) === false;
$y = strpos($b["result_title"], $term) === false;
if ($x && !$y) return 1;
if ($y && !$x) return -1;

// use this if you want to sort alphabetically after the keyword sort:
return strcmp($a["result_title"], $b["result_title"]);

// or if you only want to sort by whether or not the keyword was found:
return 0;
});


and



usort($data, function ($a, $b) use ($term) {
similar_text($term, $a["result_title"], $percentA);
similar_text($term, $b["result_title"], $percentB);

return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
});


and



usort($data, function ($a, $b) use ($term) {
$levA = levenshtein($term, $a["result_title"]);
$levB = levenshtein($term, $b["result_title"]);

return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1);
});


and



usort($data, function($a, $b){ return $a["result_title"] - $b["result_title"]; }); 


and many more without any proper result. Or maybe I cannot understand the method to achieve my result?



I have also checked: php sorting an array based on a string and How to sort an array by similarity in relation to an inputted word. but the answers are giving me the result I'm looking for.










share|improve this question













I Have the following Array and I would like to sort it based on the $term = "geo" in relation only to [result_title] ?



Array
(
[0] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[1] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)

[2] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee
)

[3] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)
)


The desired result should be:



Array
(
[0] => Array
(
[result_title] => George Ellinas
[result_subtext] => Active Employee

)

[1] => Array
(
[result_title] => Georgi Georgiev
[result_subtext] => Active Employee


)

[2] => Array
(
[result_title] => Georgia Kantouna
[result_subtext] => Former Employee
)

[3] => Array
(
[result_title] => Agathoklis Georgiou
[result_subtext] => Active Employee
)

[4] => Array
(
[result_title] => Charalambos Georgiou
[result_subtext] => Former Employee
)

[5] => Array
(
[result_title] => Frixos Georgiou
[result_subtext] => Active Employee
)
)


I have tried various methods such as:



usort($data, function($a, $b) use ($term) {
$x = strpos($a["result_title"], $term) === false;
$y = strpos($b["result_title"], $term) === false;
if ($x && !$y) return 1;
if ($y && !$x) return -1;

// use this if you want to sort alphabetically after the keyword sort:
return strcmp($a["result_title"], $b["result_title"]);

// or if you only want to sort by whether or not the keyword was found:
return 0;
});


and



usort($data, function ($a, $b) use ($term) {
similar_text($term, $a["result_title"], $percentA);
similar_text($term, $b["result_title"], $percentB);

return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
});


and



usort($data, function ($a, $b) use ($term) {
$levA = levenshtein($term, $a["result_title"]);
$levB = levenshtein($term, $b["result_title"]);

return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1);
});


and



usort($data, function($a, $b){ return $a["result_title"] - $b["result_title"]; }); 


and many more without any proper result. Or maybe I cannot understand the method to achieve my result?



I have also checked: php sorting an array based on a string and How to sort an array by similarity in relation to an inputted word. but the answers are giving me the result I'm looking for.







php arrays sorting multidimensional-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:05









jQuerybeast

5,9543294175




5,9543294175








  • 1




    can you provide your source array as PHP code?
    – Marcin Orlowski
    Nov 10 at 22:16










  • @MarcinOrlowski you mean how the array is formed at first?
    – jQuerybeast
    Nov 10 at 22:23










  • it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
    – Marcin Orlowski
    Nov 10 at 22:26










  • Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
    – jQuerybeast
    Nov 10 at 22:28










  • @MarcinOrlowski I hope this is what you meant :S
    – jQuerybeast
    Nov 10 at 22:29














  • 1




    can you provide your source array as PHP code?
    – Marcin Orlowski
    Nov 10 at 22:16










  • @MarcinOrlowski you mean how the array is formed at first?
    – jQuerybeast
    Nov 10 at 22:23










  • it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
    – Marcin Orlowski
    Nov 10 at 22:26










  • Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
    – jQuerybeast
    Nov 10 at 22:28










  • @MarcinOrlowski I hope this is what you meant :S
    – jQuerybeast
    Nov 10 at 22:29








1




1




can you provide your source array as PHP code?
– Marcin Orlowski
Nov 10 at 22:16




can you provide your source array as PHP code?
– Marcin Orlowski
Nov 10 at 22:16












@MarcinOrlowski you mean how the array is formed at first?
– jQuerybeast
Nov 10 at 22:23




@MarcinOrlowski you mean how the array is formed at first?
– jQuerybeast
Nov 10 at 22:23












it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
– Marcin Orlowski
Nov 10 at 22:26




it does not matter where it comes from. Make it in valid PHP array so I can try w/o wasting time to convert this data into code myself :)
– Marcin Orlowski
Nov 10 at 22:26












Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
– jQuerybeast
Nov 10 at 22:28




Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [result_title] => Frixos Georgiou [result_subtext] => Active Employee ) [2] => Array ( [result_title] => George Ellinas [result_subtext] => Active Employee ) [3] => Array ( [result_title] => Georgi Georgiev [result_subtext] => Active Employee ) [4] => Array ( [result_title] => Charalambos Georgiou [result_subtext] => Former Employee ) )
– jQuerybeast
Nov 10 at 22:28












@MarcinOrlowski I hope this is what you meant :S
– jQuerybeast
Nov 10 at 22:29




@MarcinOrlowski I hope this is what you meant :S
– jQuerybeast
Nov 10 at 22:29












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Here's a function that I think will do what you want. I've presumed you want to find the value of $term at the beginning of a word. This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title.



$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?b($termw*)b.*$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?b($termw*)b.*$/i", $b['result_title'], $matches) ? $matches[1] : '';
// check if the terms were found
if ($t1 == '' && $t2 != '') return 1;
if ($t1 != '' && $t2 == '') return -1;
// found in both - if not the same, just sort on the keyword
if ($t1 != $t2) return strcmp($t1, $t2);
// found the same keyword, sort on the whole title
return strcmp($a['result_title'], $b['result_title']);
});


Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org.






share|improve this answer























  • I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
    – jQuerybeast
    Nov 10 at 22:46










  • Would it make a difference if I set the term string to lower? There is some confusions for example
    – jQuerybeast
    Nov 10 at 22:50










  • made it for your reference here: 3v4l.org/DVSYp - Updated URL
    – jQuerybeast
    Nov 10 at 22:54












  • @jQuerybeast I'm not sure what you mean by "set the term string to lower"?
    – Nick
    Nov 10 at 23:06






  • 1




    @jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
    – Nick
    Nov 10 at 23:18













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243883%2fphp-sort-multidimensional-array-based-on-term-in-relation-to-a-value-of-a-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Here's a function that I think will do what you want. I've presumed you want to find the value of $term at the beginning of a word. This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title.



$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?b($termw*)b.*$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?b($termw*)b.*$/i", $b['result_title'], $matches) ? $matches[1] : '';
// check if the terms were found
if ($t1 == '' && $t2 != '') return 1;
if ($t1 != '' && $t2 == '') return -1;
// found in both - if not the same, just sort on the keyword
if ($t1 != $t2) return strcmp($t1, $t2);
// found the same keyword, sort on the whole title
return strcmp($a['result_title'], $b['result_title']);
});


Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org.






share|improve this answer























  • I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
    – jQuerybeast
    Nov 10 at 22:46










  • Would it make a difference if I set the term string to lower? There is some confusions for example
    – jQuerybeast
    Nov 10 at 22:50










  • made it for your reference here: 3v4l.org/DVSYp - Updated URL
    – jQuerybeast
    Nov 10 at 22:54












  • @jQuerybeast I'm not sure what you mean by "set the term string to lower"?
    – Nick
    Nov 10 at 23:06






  • 1




    @jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
    – Nick
    Nov 10 at 23:18

















up vote
1
down vote



accepted










Here's a function that I think will do what you want. I've presumed you want to find the value of $term at the beginning of a word. This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title.



$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?b($termw*)b.*$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?b($termw*)b.*$/i", $b['result_title'], $matches) ? $matches[1] : '';
// check if the terms were found
if ($t1 == '' && $t2 != '') return 1;
if ($t1 != '' && $t2 == '') return -1;
// found in both - if not the same, just sort on the keyword
if ($t1 != $t2) return strcmp($t1, $t2);
// found the same keyword, sort on the whole title
return strcmp($a['result_title'], $b['result_title']);
});


Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org.






share|improve this answer























  • I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
    – jQuerybeast
    Nov 10 at 22:46










  • Would it make a difference if I set the term string to lower? There is some confusions for example
    – jQuerybeast
    Nov 10 at 22:50










  • made it for your reference here: 3v4l.org/DVSYp - Updated URL
    – jQuerybeast
    Nov 10 at 22:54












  • @jQuerybeast I'm not sure what you mean by "set the term string to lower"?
    – Nick
    Nov 10 at 23:06






  • 1




    @jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
    – Nick
    Nov 10 at 23:18















up vote
1
down vote



accepted







up vote
1
down vote



accepted






Here's a function that I think will do what you want. I've presumed you want to find the value of $term at the beginning of a word. This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title.



$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?b($termw*)b.*$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?b($termw*)b.*$/i", $b['result_title'], $matches) ? $matches[1] : '';
// check if the terms were found
if ($t1 == '' && $t2 != '') return 1;
if ($t1 != '' && $t2 == '') return -1;
// found in both - if not the same, just sort on the keyword
if ($t1 != $t2) return strcmp($t1, $t2);
// found the same keyword, sort on the whole title
return strcmp($a['result_title'], $b['result_title']);
});


Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org.






share|improve this answer














Here's a function that I think will do what you want. I've presumed you want to find the value of $term at the beginning of a word. This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title.



$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?b($termw*)b.*$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?b($termw*)b.*$/i", $b['result_title'], $matches) ? $matches[1] : '';
// check if the terms were found
if ($t1 == '' && $t2 != '') return 1;
if ($t1 != '' && $t2 == '') return -1;
// found in both - if not the same, just sort on the keyword
if ($t1 != $t2) return strcmp($t1, $t2);
// found the same keyword, sort on the whole title
return strcmp($a['result_title'], $b['result_title']);
});


Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 23:10

























answered Nov 10 at 22:42









Nick

18.8k41433




18.8k41433












  • I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
    – jQuerybeast
    Nov 10 at 22:46










  • Would it make a difference if I set the term string to lower? There is some confusions for example
    – jQuerybeast
    Nov 10 at 22:50










  • made it for your reference here: 3v4l.org/DVSYp - Updated URL
    – jQuerybeast
    Nov 10 at 22:54












  • @jQuerybeast I'm not sure what you mean by "set the term string to lower"?
    – Nick
    Nov 10 at 23:06






  • 1




    @jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
    – Nick
    Nov 10 at 23:18




















  • I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
    – jQuerybeast
    Nov 10 at 22:46










  • Would it make a difference if I set the term string to lower? There is some confusions for example
    – jQuerybeast
    Nov 10 at 22:50










  • made it for your reference here: 3v4l.org/DVSYp - Updated URL
    – jQuerybeast
    Nov 10 at 22:54












  • @jQuerybeast I'm not sure what you mean by "set the term string to lower"?
    – Nick
    Nov 10 at 23:06






  • 1




    @jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
    – Nick
    Nov 10 at 23:18


















I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
– jQuerybeast
Nov 10 at 22:46




I dont know how to thank you enough. I've been trying so much the past 48 hours. Thank you!
– jQuerybeast
Nov 10 at 22:46












Would it make a difference if I set the term string to lower? There is some confusions for example
– jQuerybeast
Nov 10 at 22:50




Would it make a difference if I set the term string to lower? There is some confusions for example
– jQuerybeast
Nov 10 at 22:50












made it for your reference here: 3v4l.org/DVSYp - Updated URL
– jQuerybeast
Nov 10 at 22:54






made it for your reference here: 3v4l.org/DVSYp - Updated URL
– jQuerybeast
Nov 10 at 22:54














@jQuerybeast I'm not sure what you mean by "set the term string to lower"?
– Nick
Nov 10 at 23:06




@jQuerybeast I'm not sure what you mean by "set the term string to lower"?
– Nick
Nov 10 at 23:06




1




1




@jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
– Nick
Nov 10 at 23:18






@jQuerybeast the way the code works it won't sort that way because 'LIA' < 'Lia'. If you want it to be case-insensitive change the strcmp to strcasecmp
– Nick
Nov 10 at 23:18




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243883%2fphp-sort-multidimensional-array-based-on-term-in-relation-to-a-value-of-a-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine