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.
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))
I have array like below. I would like to push it to 0 element array.
$csvdata is contain original array $pushHeaderSpec variable is what i want to push into original array i have also tried array_merge but not work as expected merge well on output only but when i print original data in csv it is not there.i m generating $csvdata array first and then append this array on last.
Array
(
[Ruder] => no value need on this
[Glas] => no value need on this
[Not] => no value need on this
)
My Multidimention array look something like
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
)
)
Many more element on above array so i just want to merge my first array keys to this array on first element that is 0.
I did try using below code but it doesn't give me output what i want.
array_push($csvdata[0],array_keys($pushHeaderSpec));
Output from code
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] =>array (
[0] => Ruder
[1] => Glas
[2] => Not
)
)
)
Expecting Output
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] => Ruder
[4] => Glas
[5] => Not
)
)
It was just
foreach (array_keys($pushHeaderSpec) as $key => $value) {
array_push($csvdata[0],$value);
}
Is that what you are looking for ?
foreach ($pushHeaderSpec as $key => $val) {
$csvdata[0][] = $key;
}
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);
I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.
Try:
unset ($array_name[count($array_name)-1]);
$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.
Try like
$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);
if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);
You are seeing the "popped" element instead of the modified array.
array_pop() returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop(), then print the array.
array_pop($array);
print_r($array);
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.