Make array keys to start from 0 again in php [duplicate] - php

This question already has answers here:
Rebase array keys after unsetting elements [duplicate]
(9 answers)
Closed 8 years ago.
I have an array called $Compare, I used the print_r() function in php:
Array ([2] => Broccoli, raw [3] => Candies, butterscotch [4] => Celery, raw [10] => Apricots, raw)
I want it the keys to be in the correct order: 0,1,2,3 not 2,3,4,10!
In the end it should look like this:
Array ([0] => Broccoli, raw [1] => Candies, butterscotch [2] => Celery, raw [3] => Apricots, raw)

Use array_values:
$Compare = array_values($Compare);

Related

Get values from array in php [duplicate]

This question already has answers here:
How to echo single value from multidimensional array when key is numeric?
(4 answers)
Closed 3 years ago.
I have output like this:
[0] => Array
(
[voice] => Napisy
[quality] => 720
[source] => YouTube
)
[16] => Array
(
[voice] => Napisy
[quality] => 720
[source] => Mega
)
And I want to get source to get output [Youtube, Mega]. Anyone know how to get this without foreach?
You can get source from whole array into another array using array_column.
your above array is $array
$source = array_column($array, 'source');
print_r($source);

Is it possible to divide the contents from an array into equal part/variable? [duplicate]

This question already has answers here:
Variable variables: when useful? [duplicate]
(3 answers)
what is "$$" in PHP
(3 answers)
How do I dynamically create the variable name in a PHP loop?
(1 answer)
How do I append a variable to create a new variable name
(2 answers)
Closed 4 years ago.
I am using print_r on a variable which gives me the following output
Array
(
[0] => 95.2
[1] => 94.7
[2] => 95
[3] => 33.6
)
Array
(
[0] => 100
[1] => 95
[2] => 91.90000000000001
[3] => 33.6
)
I want to split them into two variable dynamically. Like $var1 and $var2. If there were three iterations, i would want it to be three variables. Is it possible in php? In the current scenario i want
$var1= (
[0] => 95.2,
[1] => 94.7,
[2] => 95,
[3] => 33.6
)
and
$var2= (
[0] => 100,
[1] => 95,
[2] => 91.90000000000001,
[3] => 33.6
)
and i want to both to be generated dynamically and if possible i want the count of the iterations as well. Thank you in advance
<?php
$data[]=array(1,2,3);
$data[]=array(5,6,7);
$data[]=array(7,8,9);
//you can add many more here and it will still works fine
foreach ($data as $key=>$value){
$variable_name='var_'.$key;
$$variable_name=$value;
}
print_r($var_0);
print_r($var_1);
print_r($var_2);
Sure it is possible.

PHP - Access array in array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Accessing Arrays inside Arrays In PHP
(4 answers)
Closed 7 years ago.
I am working with response from Youtube Data API and I don't know how to read each values in the array.
Array
(
[0] => youtube#channelListResponse
[1] => "X"
[2] => Array (
[totalResults] => 1
[resultsPerPage] => 1
)
[3] => Array (
[0] => Array
(
[kind] => youtube#channel
[etag] => "XX"
[id] => XXX
)
)
)
When I try to access [id] with echo $array[3]['id'] it returns Notice: Undefined offset: 3
AS Max said you can access single value in array in array by
$arrayName[index]['key']
Example You can access 'kind' with
$arrayName[3]['kind']
Since, inner level array is key value paired you need to specify key instead of index.
If you want to access all value, you can do it with foreach loop and check type of each value use'gettype()' method. if type is array then use same function recursively.

strip array with only one item in it without looping thru it and not knowing its index [duplicate]

This question already has answers here:
Reset PHP Array Index
(4 answers)
Closed 8 years ago.
Array
(
[2] => stdClass Object
(
[name] => test-song-poll-03
[description] => test-song-poll-03
[created_at] => 2014-05-02T23:19:06Z
[count] => stdClass Object
(
[approved] => 26638
[pending] => 0
[rejected] => 36923
[total] => 63561
)
[tpm] => 47
[approved_tpm] => 9
[pct] => 2
)
)
I have a function that uses array_filter and it returns what you see above. It will only return one object within the array. I do not know what the array index will be, but I know there will only be one item in the array. Is there an array function that strips down the array and just returns the content of it, since I don't need an array with just one item in it.
You should use:
$x = array_values($yourArrayName);
echo $x[0];
You can also use:
echo current($yourArrayName);

How to remove value and reassign remaining values to keys in array in PHP [duplicate]

This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 9 years ago.
The below code,
$test_array = array("a","b","c","d","e");
echo "<fieldset><pre>";
htmlspecialchars(print_r($test_array));
echo "</pre></fieldset>";
which gives output like,
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
I want to remove a specific entry say from index 2 and re-index the array as below,
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
)
How to do that?
Use array_splice
array_splice($test_array, 2, 1);
The second argument is your index that you want to nix and the third is how many elements you want gone.
Try this
unset($test_array[2]);
$test_array = array_values($test_array);

Categories