I have an array with the following values
array (size=7)
0 => string '020120140759' (length=12)
1 => string '020120140759' (length=12)
2 => string '020120140759' (length=12)
3 => string '020220140759' (length=12)
4 => string '020220140759' (length=12)
5 => string '020320140759' (length=12)
6 => string '020320140759' (length=12)
You will notice that the value of certain numbers are the same, I want to extract the last value of each kind that occurs in the array so that a new array will look something likes this.
array (size=2)
2 (this will change to 0 ) => string '020120140759' (length=12)
6 (this will change to 1 ) => string '020320140759' (length=12)
I have tried quite a couple of things but i haven't been successful . Any help would be really appreciated
array_unique is the way to go.
var_dump( array_unique( array_reverse($yourOriginalArrayHere) ) );
you can use array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
the Sorting flags will be
SORT_REGULAR
SORT_NUMERIC
SORT_STRING
SORT_LOCALE_STRING
example
$result = array_unique($input);
print_r($result);
<?
//first step : Exchanges all keys with their associated values in an array $newArray
//second step: get $newArray's key
[array_flip][1]
[array_keys][2]
$array = array(
0 => '020120140759' ,
1 => '020120140759' ,
2 => '020120140759' ,
3 => '020220140759' ,
4 => '020220140759' ,
5 => '020320140759' ,
6 => '020320140759'
);
$newArray = array_keys( array_flip($array) );
echo '<pre>';
var_dump($newArray);
echo '</pre>';
Related
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 3 years ago.
I have an array whith the following content:
array (size=5)
0 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.facebook.com' (length=24)
2 => string '4' (length=1) // order number
1 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.twiiter.com' (length=23)
2 => string '7' (length=1) // order number
2 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.instagram.com' (length=25)
2 => string '9' (length=1) // order number
3 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.linkedin.com' (length=24)
2 => string '2' (length=1) // order number
4 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.pinterest.com' (length=25)
2 => string '1' (length=1) // order number
I want to sort this array based on the number in the above code. (where is a written comment).
How can I do this?
So far I have written the following code but I do not know how to make it properly.
$arrMerge = array_merge($facebookURL, $twitterURL, $instagramURL, $linkedinURL, $pinterestURL);
$splitArr = array_chunk($arrMerge, 3);
You can use array_multisort with array_column
array_multisort(array_column($arr, 2), SORT_ASC,$arr)
You can use SORT_ASC OR SORT_DESC as you required
Live DEMO
You can use usort.
usort($data, function($a, $b) {
return $a[2] - $b[2];
});
You might also want to type-cast the result to an integer before-hand as it looks like your data is treated as a string.
usort($data, function($a, $b) {
return (int)$a[2] - (int)$b[2];
});
This is also a possible duplicate of this question.
i have a dynamically created multidimensional array with 2 arrays inside, but note it could also be 4 arrays inside. depends on the user.
The problem is that i dont know how to merge the values into 1 array because i only have 1 multidimensional array, i looked into array_merge but that requires 2 arrays. Please point me in the right direction.
it looks like this:
array (size=2)
'standaard' =>
array (size=4)
0 => float 0.42
1 => float 0.74
2 => float 0.39
3 => float 0.44
'natugro' =>
array (size=4)
0 => float 0.44
1 => float 0.8
2 => float 0.33
3 => float 0.36
What i want is to merge the 2 arrays values togetter in 1 array seperated with ':'
Like this:
array (size=1)
array (size=4)
0 => string '0.42:0.44' (length=4)
1 => string '0.74:0.8' (length=4)
2 => string '0.39:0.33' (length=4)
3 => string '0.44:0.36' (length=4)
What i tried:
but this only returns 1 array with the last values.
$test = array_merge($fruitHarvest);
var_dump($test);
This is the code that created the array.
$dbKenmerk = mysqli_query($conn, 'SELECT kenmerk FROM kenmerken WHERE user_id = '.$user.' AND jaar = '.$_SESSION["jaar"].'');
while($getKenmerk = mysqli_fetch_assoc($dbKenmerk)){
$dbFruitHarvest = mysqli_query($conn, 'SELECT vruchten_geoogst FROM gewasregistratie WHERE user_id = '.$user.' AND jaar = '.$_SESSION["jaar"].' AND kenmerk = "'.$getKenmerk["kenmerk"].'"');
foreach($dbFruitHarvest as $key => $innArr){
foreach($innArr as $val){
$fruitHarvest[$getKenmerk["kenmerk"]][] = $val;
}
}
}
var_dump($fruitHarvest);
This should work for you:
Just implode your elements with a colon.
<?php
array_unshift($fruitHarvest, function(){return implode(":", func_get_args());});
$result = call_user_func_array("array_map", $fruitHarvest);
print_r($result);
?>
I have a multidimensional array like so:
array (size=4)
0 =>
array (size=2)
'term' => string 'news-article' (length=12)
'count' => int 139
1 =>
array (size=2)
'term' => string 'industry-resource' (length=17)
'count' => int 37
2 =>
array (size=2)
'term' => string 'editorial' (length=9)
'count' => int 33
3 =>
array (size=2)
'term' => string 'bulletin' (length=8)
'count' => int 12
and I'm trying create a function that searches for a term and returns it's neighboring value, count.
My inclination was to use array_search(), however using this returns false, I'm guessin because it's only searching the first layer of the array (0,1,2,3).
I'm not so much looking for an exact answer but a nudge in the right direction. I'm guessing it will require looping through the array, but I do not know how to approach getting the neighboring count value once the term value is located. Any help is appreciated!
You can just loop through the array and access them directly.
$search_term = "news-article";
$count = 0;
foreach($array as $element) {
if($element['term'] == $search_term) {
$count = $element['count'];
break;
}
}
I have a string
$style = "font-color:#000;font-weight:bold;background-color:#fff";
I need only
font-color
font-weight
background-color
I have tried
preg_match_all('/(?<names>[a-z\-]+:)/', $style, $matches);
var_dump($matches);
it gives me following output
array
0 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
'names' =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
1 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
There are three problems with this output
1. It is two or three dimensional array, I need one dimensional array.
2. It is repeating the information
3. It is appending ":" at the end of each element.
I need a single array like this
array
0 => 'font-color'
1 => 'font-weight'
2 => 'background-color'
Take out the colon:
$style = "font-color:#000;font-weight:bold;background-color:#fff";
preg_match_all('/(?<names>[a-z\-]+):/', $style, $matches);
var_dump($matches['names']);
Then use $matches['names'], since you named it, so you dont have redundant informations
take this string as an example: "will see you in London tomorrow and Kent the day after tomorrow".
How would I convert this to an associative array that contains the keywords as keys, whilst preferably missing out the common words, like this:
Array ( [tomorrow] => 2 [London] => 1 [Kent] => 1)
Any help greatly appreciated.
I would say you could :
split the string into an array of words
with explode
or preg_split
depending on the complexity you'll accept for your words separators
use array_filter to only keep the lines (i.e. words) you want
the callback function will have to return false for all non-valid-words
and, then, use array_count_values on the resulting list of words
which will count how many times each words is present in the array of words
EDIT : and, just for fun, here's a quick example :
First of all, the string, that gets exploded into words :
$str = "will see you in London tomorrow and Kent the day after tomorrow";
$words = preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY);
var_dump($words);
Which gets you :
array
0 => string 'will' (length=4)
1 => string 'see' (length=3)
2 => string 'you' (length=3)
3 => string 'in' (length=2)
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
6 => string 'and' (length=3)
7 => string 'Kent' (length=4)
8 => string 'the' (length=3)
9 => string 'day' (length=3)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
Then, the filteting :
function filter_words($word) {
// a pretty simple filter ^^
if (strlen($word) >= 5) {
return true;
} else {
return false;
}
}
$words_filtered = array_filter($words, 'filter_words');
var_dump($words_filtered);
Which outputs :
array
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
And, finally, the counting :
$counts = array_count_values($words_filtered);
var_dump($counts);
And the final result :
array
'London' => int 1
'tomorrow' => int 2
'after' => int 1
Now, up to you to build up from here ;-)
Mainly, you'll have to work on :
A better exploding function, that deals with ponctuation (or deal with that during filtering)
An "intelligent" filtering function, that suits your needs better than mine
Have fun !
You could have a table of common words, then go through your string one word at a time, checking if it exists in the table, if not, then add it to your associative array, or +1 to it if it already exists.
using a blacklist of words not to be included
$str = 'will see you in London tomorrow and Kent the day after tomorrow';
$skip_words = array( 'in', 'the', 'will', 'see', 'and', 'day', 'you', 'after' );
// get words in sentence that aren't to be skipped and count their values
$words = array_count_values( array_diff( explode( ' ', $str ), $skip_words ) );
print_r( $words );