0

how do i get the relation between the two array when both of them have values in some relation on the same indexes of both array for example,

i have retrieved "tagname" and "path" from one table of mysql and then i put these two column values in two arrays using loop so "array Tag[]" have vale "Introduction" and "array Path[]" have path value for introduction both values are on index "0" of there respected array and all data is collecten in "arrat Tag[]" and "array Path[]" in this manner after that i sort my "Tag" according to some other array using this code,

$sorted =array_intersection($some_other_array,$array Tag)

now how would i know the related path values for Tag as tag sorted ??

Hopes for your suggestions

Syed Raza
  • 331
  • 2
  • 13
  • 35
  • instead of creating two different array you can create hash array with key as path and tag as value and vice versa – Poonam Aug 08 '12 at 07:44
  • @Poonam can you please give me some example ? – Syed Raza Aug 08 '12 at 07:50
  • @Poonam one more thing i need to sort tag array with some other array thats why i put them in different array would sorting technic will still work after using hash array ? – Syed Raza Aug 08 '12 at 07:53
  • yes it should.But you haven't put the logic,So I am not able to say anything about it.If you are not sorting on index it will work. – Poonam Aug 08 '12 at 08:07
  • @Poonam as i mentioned above i am sorting it on the be half of another array using PHP function "array_intersection" and i think i would use index for sorting – Syed Raza Aug 08 '12 at 08:21
  • array_intersection is not a function.but array_intersect is a function and it will work on key-value pair, also there is array_intersect_assoc function available for sorting which will match exact index position also – Poonam Aug 08 '12 at 08:27

2 Answers2

1

from mysql/DB result set when you are creating array, create as

while($row = mysql_fetch_assoc($query)){
    $array[$row['path']] = $row['tag'];
}

assuming your array as

$array['xyz'] = 'pqr';
$array['abc'] = 'wsx';
$array['poi'] = 'qaz';
$array['lkj'] = 'abc';

sort your array as per need based on tag or sorting methods available. in this case instead of int index it will have key as path sorting with tag also binds it with path.

after sorting your array (assuming sorting with first letter alphabates of tag name)

$array['lkj'] = 'abc';
$array['poi'] = 'qaz';
$array['xyz'] = 'pqr';
$array['abc'] = 'wsx';

so you can easily find path for your tag with foreach loop with key and value or with aray_keys if you want particular tag path and you know the tag value.

Poonam
  • 4,591
  • 1
  • 15
  • 20
  • please check my this thread too http://stackoverflow.com/questions/11877973/how-to-sort-array-on-behalf-of-another-when-one-array-index-is-of-string-m-using#comment15804679_11877973 – Syed Raza Aug 09 '12 at 06:56
0

You need to combine those two arrays into associative arrays and then use asort() or uasort(), depending on particular sort scenario.

jderda
  • 890
  • 6
  • 18