This question already has answers here:
How to reindex an array?
(6 answers)
Closed 6 years ago.
array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
If delete second element, you have
array(
[0]=>1
[2]=>3
[3]=>4
)
Now how to change indexes to
array(
[0]=>1
[1]=>3
[2]=>4
)
If you want to re-index your array starting to zero, simply do the following:
$myNewArray = array_values($myOldArry);
From the php docs:
http://php.net/manual/en/function.array-splice.php
array_splice: Removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.
So, you would use:
$arr = array(1,2,3,4);
array_splice($arr, 2, 1); //Will give you an array: [1, 2, 4]
Related
This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 4 years ago.
I have an array like this
$arr = ['Hello', 'World'];
What I am trying to achieve is
$arr [
'Hello' => 'Hello',
'World' => 'World'
]
Is there a array method to achieve this or should I run a foreach loop and do it manually? I am just thinking if there is a more elegant way
You could use array_combine for this
$new = array_combine($arr, $arr);
print_r($new);
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 8 months ago.
I have an array as shown bottom
array (
[det1] => 1,2,3,4
[det2] => 5,6
);
So i want to join items of this array and convert array to an string like bottom
$uru = 1,2,3,4,5,6
How can i do this work?
Did you try like this with implode()
<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>
DEMO: https://3v4l.org/UBQrv
You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.
In that case loop the array and merge the new array with the subarray.
$arr = array (
"det1" => [1,2,3,4],
"det2" => [5,6]
);
$new= [];
foreach($arr as $sub){
$new = array_merge($new, $sub);
}
echo implode(",",$new); // 1,2,3,4,5,6
https://3v4l.org/NaDXN
This can be easily done by extracting into array_merge and then joining the resulting array.
$data = [
[1,2,3,4,5],
[6,7,8],
];
echo join(',', array_merge(... $data));
Output:
1,2,3,4,5,6,7,8
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I am trying to display an array without key item. Just want to display array values without key.
Here is my sample code I tried
$myList = array(
0 =>array(
"product_id"=> 8085
),
1 =>array(
"product_id"=> 8087
),
2 =>array(
"product_id"=> 8086
),
3 =>array(
"product_id"=> 8042
),
);
$newList = array();
foreach($myList as $listItem) {
$newList[] = $listItem['product_id'];
}
$a=array();
$a= array_values($newList);
print_r($a);
I want my array like this
$productIds = array(8085,8087,8086,8042);
Here is my sample code link
You're looking for array_column (which is available as of PHP 5.5):
$productIds = array_column($myList, 'product_id');
This gives you:
var_export($productIds);
array (
0 => 8085,
1 => 8087,
2 => 8086,
3 => 8042,
)
Which is exactly what you want:
var_dump($productIds === array(8085,8087,8086,8042)); // bool(true)
print_r function will output the keys. even if you use array_values the array still have indexes as keys.
Just output the the array manually using echo and implode (implode will join array values into a single string using the first parameter character):
echo implode(',', $newList);
Arrays will always have keys. If you want an array, you can get all the values, turn them into one comma separated string, and place that into an array:
$productIds = [implode(',', array_column($myList, 'product_id'))];
var_dump($productIds);
// RESULT:
// array (size=1)
// 0 => string '8085,8087,8086,8042' (length=19)
This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
Closed 9 years ago.
I have got an array
$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);
Now, i am going to unset the 1 and 2 keys.
unset($array[1]); unset($array[2]);
How to do if i want an array
array(0=>'c', 1=>'d');
Try This:
$array = array_values($array);
Try
unset($array[1]);
unset($array[2]);
$array = array_values($array);
This works because array_values returns the array you gave, but with increasing keys starting at 0.
Have you tired to use something like that ?
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
array_values
$myarray = array_values($myarray);
btw here is a good answer posted
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Merge 2 Arrays and Sum the Values (Numeric Keys)
I have two arrays both have same keys, i want to add key values,
Example
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
want to create new array which add these two array values
like
$arr3 = array("first"=>30,"second"=>50);
How can i do that in one line, i can do it using forach or other technique, but wondering if some one know a good way of doing?
Example for string keys (note that no error checking is done!):
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
$arr3 = $arr1;
array_walk($arr3, function (&$val, $key, $foo) { $val += $foo[$key]; }, $arr2);
print_r($arr3);
/*Array
(
[first] => 30
[second] => 50
)*/