I've a problem to unerstand correctly array_values, when I do:
$array[] = 'data1'; // I want [0 => 'data1']
unset($array[0]); // I want []
$array = array_values($array); // I want [] but keys resetted
$array[] = 'data2'; // I want [0 => 'data2']
$array[] = 'data3'; // I want [0 => 'data2', 1 => 'data3']
dump($array);
I've the result:
array:2 [▼
1 => "data2"
2 => "data3"
]
But I'll want:
array:2 [▼
0 => "data2"
1 => "data3"
]
Maybe someone can explain it to me ? Because I'm a little lost :-/
For exemple, if I've an array with 10 values in, remove the 3rd value, and do an array_values on, it works well.
But if I remove the last value from an array, then when I do a array_value, the next value I add, always have id 1 and not 0.
This behaviour has been already reported as a bug: https://bugs.php.net/bug.php?id=75433 and (apparently as the result of this post) also: https://bugs.php.net/bug.php?id=75653
Related
I have an array that looks like below:
$var[] = ^ array:4 [▼
0 => array:1 [▼
0 => "apple"
]
1 => array:1 [▼
0 => "apple"
1 => "grape"
]
2 => array:1 [▼
0 => "orange"
]
3 => array:1 [▼
0 => "banana"
]
]
Is there a way to convert this array into comma separated list with unique values as below?
$var = "apple,grape,orange,banana"
I tried implode(', ',$var) but it's not working as expected. Any help pls?
Update: My sub-array may have more than 1 element. It's dynamic.
This produces the results given the expected input. Basically array_column get's the first item from each item of the array (0 based). Then array_unique on that will simply get the values without duplicates.
$arr = [
['apple'],
['apple'],
['banana'],
['pie'],
];
print_r(array_unique(array_column($arr, 0)));
EDIT: since there might be more than one item in the inner arrays, there's no shame in doing a non one-liner solution:
$result = [];
foreach ($arr as $row) {
foreach ($row as $item) {
if (array_search($item, $result) === false) {
$result[] = $item;
}
}
}
print_r($result);
There's also a one-liner solution:
$result= array_unique(call_user_func_array('array_merge', $arr));
cf. https://stackoverflow.com/a/14972714/13720553
I am trying to add 2 associative arrays, but the new array only comes with an index for the second array.
This is what I am doing
$array = ['name' => 'address', 'value' => 'us'];
$arr = ['name' => 'joe', 'value' => 'doe'];
$arr[] = $array;
This is the result
array:3 [▼
"name" => "joe"
"value" => "doe"
0 => array:2 [▶]
]
I am expecting something like this
array:2 [▼
0 => array:2 [▶]
1 => array:2 [▶]
]
As you can see, there is no index for the first array, thus making the count 3 instead of 2. Please how do I fix this?
Just create another array and add the 2 arrays to it:
$newAr = [];
$newAr[] = $array;
$newAr[] = $arr;
var_dump($newAr);
I don't even know what should I call this type of data but I need to get array out of it.
Code
$folderss = $ssh->exec($command);
$folders = explode(',', $folderss);
DD result
array:1 [▼
0 => """
DO_NOT_UPLOAD_HERE\n
domains\n
public_html\n
"""
]
What I need
array:3 [▼
0 => "DO_NOT_UPLOAD_HERE",
1 => "domains",
2 => "public_html",
]
any idea?
Update
I changed my code to:
$folders = preg_split("/[\s,]+/", $folderss);
Now I'm getting:
array:4 [▼
0 => "DO_NOT_UPLOAD_HERE"
1 => "domains"
2 => "public_html"
3 => ""
]
I have 1 extra row there, how to remove it?
Update 2
If I use
$folders = explode("\n", $folderss);
same result as using preg_split happens (extra line)
array:4 [▼
0 => "DO_NOT_UPLOAD_HERE"
1 => "domains"
2 => "public_html"
3 => ""
]
Try this to remove empty values from array
$folders = preg_split("/[\s,]+/", $folderss);
$folders=array_filter($folders);
Replace this line:
$folders = explode(',', $folderss);
by:
$folders = explode('\n', trim($folderss, '"');
You can use array_filter to remove the empty values from an array
$folders = array_filter(preg_split("/[\s,]+/", $folderss));
I have a multi-dimensional array that is keyed by uuid's and need to slice/pop/unset an element by uuid (i.e., if I had a410463e-7fe2-4fba-8733-a812c0ee8c54 and wanted to remove that item by that uuid) so that the result is essentially the same minus the one item that was removed:
array:5 [
"5fc29794-9e08-4944-ba6d-4a5fcde5c88b" => array:3 [
"id" => "5fc29794-9e08-4944-ba6d-4a5fcde5c88b"
"name" => "fuga"
"value" => 0
]
"a410463e-7fe2-4fba-8733-a812c0ee8c54" => array:3 [
"id" => "a410463e-7fe2-4fba-8733-a812c0ee8c54"
"name" => "nihil"
"value" => 0
]
"c141d973-91fe-4227-8985-04bd0665f4a8" => array:3 [
"id" => "c141d973-91fe-4227-8985-04bd0665f4a8"
"name" => "eaque"
"value" => 0
]
"17030897-1aa9-487d-a4be-d574dd0c9d9b" => array:3 [
"id" => "17030897-1aa9-487d-a4be-d574dd0c9d9b"
"name" => "eveniet"
"value" => 3
]
"901d9f8f-573f-444f-8562-0cdf5888ba6e" => array:3 [
"id" => "901d9f8f-573f-444f-8562-0cdf5888ba6e"
"name" => "in"
"value" => 6
]
]
I know how to slice by index, but am having trouble finding resources on how this might be achieved. This is for a phpunit test. I've tried unset, but can't seem to store that in a variable or just call it in an assertion:
unset($array1[$id]);
unset($array2[$id]);
Does not persist the change.
$newUnchanged = unset($array1[$id]);
$oldUnchanged = unset($array2[$id]);
Throws syntax error, unexpected 'unset' error. Ultimately I want to assert that all of the unchanged items remained the same as prior to a single item being updated. I've also tried this ugly business which is removing a single item, but not the correct one:
$keyOne = array_search($id, array_keys($array1), true);
$oldUnchanged = array_slice($array1, $keyOne, null, true);
$keyTwo = array_search($id, array_keys($array2), true);
$newUnchanged = array_slice($array2, $keyTwo, null, true);
// Shows that the item that I wanted to slice still exists in both arrays
dd($id, $oldUnchanged, $newUnchanged);
// ^ Causes this test to fail
$this->assertEquals($oldUnchanged, $newUnchaged);
I figured out I have to clone the arrays before I can unset them
$oldUnchanged = $array1;
unset($oldUnchanged[$id]);
$newUnchanged = $array2;
unset($newUnchanged[$id]);
I have and array and i need to push a value into the first index(0).
array:3 [▼
4 => "test1"
5 => "test2"
6 => "test3"
]
I need the indexes to stay like this, so the array will become like below. Since the indexes are the IDS of the values.
array:3 [▼
0 => "None selected"
4 => "test1"
5 => "test2"
6 => "test3"
]
To populate array:
$accuGroups = UpselGroup::where('accu_group','1')->with('UpselProducts.products')->pluck('naam','id')->toArray();
What i tried:
$accuGroups = array_merge([0 => 'None selected'], $accuGroups);
outcome (not what i want):
array:4 [▼
0 => "None selected"
1 => "test1"
2 => "test2"
3 => "test3"
]
Any help is appreciated
thanks
array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value
so use like this :
$accuGroups[0]="None selected";
Try this
$none_selected = array(
0 => 'None selected');
$accuGroups = $none_selected + $accuGroups;
you can try this
<?php
$queue = array("test1", "test2","test3", "test4");
array_unshift($queue, "None selected");
echo "<pre>";
print_r($queue);
?>
$accuGroups[0]="Not Selected.";
$accuGroups[6]="Not Selected.";
The array will reset its index value with given Value;
Keep another value which you have to merge as array and add it to first array and you will get result.
$a=['4' => "test1", '5' => "test2",'6' => "test3"];
$b=['0'=>'Not Selected'];
$c=$b+$a;
in $c you will get result as per your requirement.
You can try this
<?php
$array = array('4' => 'test1','5' => 'test2', '6' => 'test3');
$add = array('0'=>'None selected');
$final = $add + $array;
echo "<pre>";print_r($final);die;
?>