I have a JSON record I converted to a PHP array.
$db=array(
array("Name"=>"Beans","Price"=>"10"),
array("Name"=>"Salad","Price"=>"5"),
array("Name"=>"Bread","Price"=>"2"),
array("Name"=>"Lime","Price"=>"1")
);
The real table of data is this.
Index Name Price
0 Beans 10
1 Salad 5
2 Bread 2
3 Lime 1
When I want to retrieve the first item name, I can easily do it like this:
$item=$db[0]['Name'];
That's until I decide to sort the array by a column like so:
array_multisort(array_column($db,'Price'),$db);
Then PHP thinks of the array this way:
$db=array(
array("Name"=>"Lime","Price"=>"1"),
array("Name"=>"Bread","Price"=>"2"),
array("Name"=>"Salad","Price"=>"5"),
array("Name"=>"Beans","Price"=>"10"),
);
Which completely messes the internal indexing because now:
$item=$db[0]['Name'];
equals 'Lime'
Is there a way to retrieve the previous index before the sorting so I can still reference the records properly.
For example, can I somehow use sorting functions to turn:
$db=array(
array("Name"=>"Beans","Price"=>"10"),
array("Name"=>"Salad","Price"=>"5"),
array("Name"=>"Bread","Price"=>"2"),
array("Name"=>"Lime","Price"=>"1")
);
to something like:
$db=array(
array("Name"=>"Lime","Price"=>"1","index"=>3),
array("Name"=>"Lime","Price"=>"1","index"=>2),
array("Name"=>"Salad","Price"=>"5","index"=>1),
array("Name"=>"Beans","Price"=>"10","index"=>0),
);
but here, index will be automatically added to the array then my program can reference that when editing the correct information. Ideas?