I want elements of that:
['1','1','2','2','2','3','3','3','3','3','3','3','4']
to be grouped into chunks with unique values, to make it look like that:
['1','2','3','4','1','2','3','2','3','3','3','3','3']
First "chunk" '1','2','3','4' contain all unique values without duplicates, second '1','2','3' etc.
The biggest problem is that my array is not combined from simple numbers, it is two-dimensional associative array, something like that:
[['id'=>'xd1c',...],['id'=>'ab2c',...],['id'=>'xd1c',...],['id'=>'xd1c',...],['id'=>'ab2c',...],['id'=>'xd1c',...],['id'=>'687d',...],...]
I don't have a lot of experience with algorythmics and advanced sorting and I feel a bit overwhelmed. Please if you could point me out in right direction with that.
Try this code :
$current = array('1','1','2','2','2','3','3','3','3','3','3','3','4');
$new = array();
while(!empty($current)){
foreach(array_keys(array_unique($current)) as $index){
$new[] = $current[$index];
unset($current[$index]);
}
}
print_r($new);
I have an array, suppose given below,
$store_array[] = $details_order;
$store_array[] store all arrays ($details_order) like given below
and now on view side in MVC, i want to fetch each value from each array. what i will do,
As you want for loop you may try like this this
foreach($array as $arr):
echo $arr['name'];
endforeach;
PHP has plenty of useful functions and Im wondering if Im overlooking one that has already been built.
Lets say you have an array such as:
$first_array = array("Name"=>"Angela", "Age"=>24);
and you wanted to grab the keys from the first array to create a second array (which could then be pushed into a third array). So you need to create:
$second_array = array("Name", "Age");
Is there a way to achieve this result without this loop?:
foreach($first_array as $k=>$v){
array_push($second_array, $k);
}
This should do it:
array_keys($first_array);
Use array_keys($first_array) to get the array of all the keys in the $first_array
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
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.
You need to combine those two arrays into associative arrays and then use asort() or uasort(), depending on particular sort scenario.
Im some array that's being returned by some query.. and the result is something like this:
array(array('balance_1'=> '-5', 'balance_2'=>'-21'), array('balance_1'=> '-21', 'balance_2'=>'21'), array('balance_1'=> '-50', 'balance_2'=>'40'))
i want to transform this into an array that looks something like this:
array(array(-5,11,-50), array(-21, 21, 40));
basicly i want to join all balance_1, all balance_2, all balance_3 into separated arrays.
any ideas? thanks
You'll just loop over the list, then collect the values. It's most simple if you reuse the existing keys to group:
foreach ($list as $row) {
foreach ($row as $key=>$value) {
$out[$key][] = $value;
}
}
This way you'll get an $out array, with [balance_1] or [balance_2] holding the value lists.
Loop though the array and use "array_key_exists" if the key exists add to the array, if it doesn't build a new array with your index.
For more can be found here:
http://www.php.net/manual/en/function.array-key-exists.php