I want to remove a number from an array, for example this array:
Array ( [0] => [1] => 2 [2] => 3 )
I want to remove the number 2 so i will get this:
Array ( [0] => [1] => 3 )
but I get this:
Array ( [0] => [2] => 3 )
What I did is to check if the number 2 is in the array and removing it with unset. Is there a better way to do this?
You can reset the numeric indices of your array after you removed the element with array_values.
unset($myArray[1]);
$myArray = array_values($myArray);
Related
I have this array $data :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
Array
(
[0] =>
)
How do i remove the bottom array that contains no values totally so it only contains :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
I have tried using array_filter($data, strlen) and it just does this :
Array
(
)
array_pop() pops and returns the value of the last element of array, shortening the array by one element and will do exactly what you describe. A.
array_shift() does the opposite (removes first element from array and returns the value)
array_pop() on PHP.net
So you can either do:
$firstVal = array_pop($data)
or just
array_pop($data)
depending on if you want the value back or not.
Might help.
array_values(array_filter($data))
Okay this is probaly an easy fix, but I'm having a bad time trying to get this to work.
Anyway I have an array $tableData which output is:
Array (
[0] => Array ( )
[1] => Array ( [0] => content1 [1] => content1)
[2] => Array( [0] => content2 [1] => content2)
[3] => Array (.... etc etc etc
I want to remove the array[0] because it's always empty
thing's i've tried:
$tableData=array_shift($tableData);
Which gives output:
Array ( )
Also I've tried
$tableData=array_splice($tableData, 0, 1);
Which gives output
Array ( [0] => Array ( ) )
Wanted output:
Array (
[0] => Array ( [0] => content1 [1] => content1)
[2] => Array( [0] => content2 [1] => content2)
[3] => Array (.... etc etc etc
Help much appreciated! :)
Answers from below fixed it, thought you had to reassign the variable but you should not in my case.
Changed
$tableData=array_shift($tableData);
to
array_shift($tableData);
array_shift($tableData);
array_shift returns the shifted element. If you overwrite your $tableData variable with that element, that's what you get. Just shift the array without reassigning it, done.
If you assign a variable to array shift, it will populate with the value that was removed.
If you just do
array_shift($tableData);
Then the first key and value for $tableData should be removed.
I have two separate Arrays
I want to merge these arrays, however I can't seem to do this.
Result from F1
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Result from F2
Array
(
[id] => Array
(
[0] => 2
[1] => 7
[2] => 9
)
)
FINAL RESULT DESIRED
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
[4] => 9
)
)
Note I am reindexing the Values by numerical order.
Many thanks
Here's a way to do it without removing the 'id' keys:
$newArray = array_merge_recursive($joinedIDs, $committeeIDs);
$newArray = array_map(function($e){return array_unique($e);}, $newArray);
Try this,
array_unique(array_merge($array1,$array2), SORT_REGULAR);
http://se2.php.net/manual/en/function.array-unique.php
The issue was that F1 & F2 were assigning ['ID'].
Removing this, allows the array_merge functions to work as expected.
$newArray = array_unique(array_merge_recursive($joinedIDs, $committeeIDs));
I know this must be a fairly simple question, but I haven't managed to stumble across an answer yet.
I have the following array
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
When I use print_r($qid) I get the following
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
I don't understand [1] => 0
in
[0] => Array ( [0] => 1 [1] => 0 )
If someone could explain what [1] => 0 means in this array, I'd greatly appreciate it. Thanks.
EDIT: It turns out that my array was indeed different to what I had written above, because it had been modified later in the code. Thanks everyone for the great answers. I'm still reading over them all and trying to make my mind understand them (Arrays turn my mind to jello).
[1] => 0 denotes an array element with the value 0.
The numbers in [] are array keys. So [1] is the second element of a numerically indexed array, (which starts with [0]), and the value of the second element ([1]) is 0.
PHP uses => as an operator to relate array keys/indices to their values.
So an overall explanation of this structure:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
The outer array is a numerically indexed array, and each of its elements is a sub-array. The first of them ([0]) is an array containing 2 elements, while the rest of them ([1] through [3]) are arrays containing only one single element.
That two-dimensional array is actually a one-dimensional array of arrays, which is why you're getting the nesting. The [x] => y bit simply means that index x of the array has the value y.
Now your output in this case doesn't actually match your code, since
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
produces:
Array (
[0] => Array ( [0] => 1 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
If you wanted to get:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
(with the first array having two elements), you'd actually need:
$qid[0][0]=1;
$qid[0][1]=0;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
You probably added a second item to $qid[0] somewhere ($qid[0][1] = 0). This code
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
outputs the the correct values for me (without [1] => 0:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) )
It means that your index 0 in the original Array contains another Array of 2 items.
Specifically [1] => 0 means that the 2nd item of the "child" Array contains the number 0.
[1] => 0
in this simple way we can say that 1 is your array key and 0 is value for the 1 key
0 is store at the 1 key of the array
thanks
Simply put, you have a numerically indexed multidimensional array. http://php.net/manual/en/language.types.array.php should have all the information you need to read up on this.
As to why you have the [1] => 0, you'll need to look a little deeper into your code to see where it gets assigned.
I got the following result after printing out the array using print_r:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
)
I guess, you might have set a value for $gid[0][1] somewhere in your code.
I have two arrays
Array1:
Array ( [0] => Array ( [0] => 3 [1] => 1 [2] => 4 ) [1] => Array ( [0] => 1 [1] => 6 ) )
Array2:
Array ( [0] => 1 [1] => 3 [2] => 2 )
I used array_diff for comparing and getting the difference values, but the same key is coming ie.,
array_diff(Array1,Array2)
returns Array([0] =>3 [2] => 4)
but is there any other way to get difference and having result like
Array([0] =>3 [1] => 4)..
Assuming you've got array_diff working on the multidimensional array somehow, but from the docs:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.
Use array_values around it.
array_values(array_diff($array1, $array2));