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);
?>
Related
I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'
If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];
Shortly array merge with array_map
$result=array_map(null,$array1,$array2);
array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];
I'm trying to replace this array:
$lts = ['20', '21'];
with an array from the database. I've tried a few things with no avail. Here's what I've got now:
$query = "SELECT id FROM members WHERE admin = '2'";
$lts = mysqli_fetch_all($con->query($query), MYSQLI_NUM);
When I var_dump it its giving me this:
array (size=2)
0 =>
array (size=1)
0 => string '20' (length=2)
1 =>
array (size=1)
0 => string '21' (length=2)
I need it to give me this:
array (size=2)
0 => string '20' (length=2)
1 => string '21' (length=2)
Any idea on how to get the latter array?
I just notice your returning array is same as the array you got,
never mind run this code
//run loop on your array after your query
for($i=0; $i<count($lts); $i++){
for($j=0; $j<count($lts[$i]); $j++){
$lts[$i]=$lts[$i][$j];
}
}
echo '<pre>';
print_r($lts);
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 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>';
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 4 months ago.
I have following arrays:
$keys
array (size=2)
0 => string 'foo' (length=3)
1 => string 'buz' (length=3)
$data
array (size=3)
'foo' => int 1
'bar' => int 2
'buz' => int 3
How to get $data array filtered by $keys values ? Desired output:
array (size=3)
'foo' => int 1
'buz' => int 3
array_intersect_key should be able to help you out here
array_intersect_key($data, array_flip($keys));
The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.
DEMO: http://codepad.org/AGpDAZtE