PHP remove the first index of an array and re-index - php

I have an array like
Array
(
[0] => A
[2] => B
[4] => C
[6] => D
)
I want to remove the first element and then re-index array to get the output
(
[0] => B
[1] => C
[2] => D
)
Which PHP function i need to use?
Update
Input array is
Array
(
[0] => Array
(
[0] => Some Unwanted text
[1] => You crazyy
)
[2] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[10] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
And the output should be like
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)

You can use
array_shift($array)
Documentation for array_shift

With array_splice.
http://www.php.net/manual/en/function.array-splice.php
php > print_r($input);
Array
(
[0] => A
[2] => B
[4] => C
[6] => D
)
php > array_splice($input, 0, 1);
php > print_r($input);
Array
(
[0] => B
[1] => C
[2] => D
)

we can do it with array_shift() which will remove the 1st index of array and after that use array_values() which will re-index the array values as i did not get from the #User123's answer, try below one:
<?php
$array = array(
0 => "A",
2 => "B",
4 => "C",
6 => "D"
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
Output: check the output here https://eval.in/837709
Array
(
[0] => B
[1] => C
[2] => D
)
Same for your Updated Input array
<?php
$array = array(
0 => array(
0 => "Some Unwanted text",
1 => "You crazyy"
),
2 => array(
0 => "My belowed text",
1 => "You crazyy"
),
10 => array(
0 => "My loved quote",
1 => "You crazyy"
)
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
Output: check the output here https://eval.in/837711
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)

You can cut the array as many many index as you want
$newArray = array_splice($oldArray, $startIndex, $lengthToSlice);

$array=array(
0 => 'A',
2 => 'B',
4 => 'C',
6 => 'D'
);
unset($array[0]);
$array = array_values($array);
print_r($array);
This is also another solution to this issue using unset
Output:
Array
(
[0] => B
[1] => C
[2] => D
)

Related

Split string with multiple length into array

I have a list of strings like this
A45618416541548234
A48432185120148084
A15973357048208202
I want to split these strings and put them into arrays like this
Array
(
[0] => Array
(
[0] => A45
[1] => 6184165
[2] => 41548234
)
[1] => Array
(
[0] => A48
[1] => 4321851
[2] => 20148084
)
[2] => Array
(
[0] => A15
[1] => 9733570
[2] => 48208202
)
)
I want to split the strings into 3 parts - 1st to 3rd character, 4th to 10th, and 11th to 18th.
I tried doing this using substr, but I could make an array like above...
How can I accomplish this??
You can achieve what you want with array_map and substr:
$strings = array('A45618416541548234', 'A48432185120148084', 'A15973357048208202');
print_r(array_map(function ($v) {
return array(substr($v, 0, 3), substr($v, 3, 7), substr($v, 10, 8)); }
, $strings));
Output:
Array
(
[0] => Array
(
[0] => A45
[1] => 6184165
[2] => 41548234
)
[1] => Array
(
[0] => A48
[1] => 4321851
[2] => 20148084
)
[2] => Array
(
[0] => A15
[1] => 9733570
[2] => 48208202
)
)
Demo on 3v4l.org

how to find difference in single dimensional array with single dimensional array in php

I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.

set array value as a variable using php

I am try to set one value to another array.
I have this type of two arrays.
Array
(
[0] => test1
[1] => test2
)
Array
(
[0] => 351
[1] => 352
[2] => 353
[3] => 354
[4] => 355
[5] => 356
)
Now I want to do something like set the first three values of the second array on test1, and set another three values from the second array to test2.
test1 = 351,352,353
test2 = 354,355,356
Is it possible?
Try this :
$var = array(0=> "test1",1=> "test2");
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_combine($var,array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3)));
echo "<pre>";
print_r($res);
Output :
Array
(
[test1] => 351,352,353
[test2] => 354,355,356
)
EDIT : As per comment "this type output i need Array ( [0] => 351,352,353 [1] => 354,355,356 )"
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3));
echo "<pre>";
print_r($res);
Output:
Array
(
[0] => 351,352,353
[1] => 354,355,356
)

Merging arrays within an array, preserving numeric index

Array_merge and array_merge_recursive are not working as desired, creating more indexes instead of pushing the arrays together and retaining the index number. See below for input/desired output:
Array (
[0] => array(
[0] => "string1",
[1] => "string2",
[2] => "string3"
),
[1] => array(
[0] => "string4",
[1] => "string5",
[2] => "string6"
),
[2] => array(
[0] => "string7",
[1] => "string8",
[2] => "string9"
)
)
Desired Output:
Array (
[0] => array("string1","string4","string7"),
[1] => array("string2","string5","string8"),
[2] => array("string3","string6","string9")
)
EDIT:
Perhaps not the best example; I want to achieve the same results but with an unequal number of keys in each nested array. See below for a better example:
<?php
$array = Array (
[0] => array(
[0] => "string1",
[1] => "string2",
[2] => "string3"
),
[1] => array(
[0] => "string4",
[1] => "string5",
[2] => "string6"
),
[2] => array(
[0] => "string7",
[1] => "string8",
[2] => "string9"
),
[3] => array(
[0] => "string10",
[1] => "string11",
[2] => "string12"
)
);
$output=array();
for($0=0;$j<count($array[0]);$j++){
$output[$j] = array();
}
for($i=0;$i<count($array);$i++){
for($0=0;$j<count($array[0]);$j++){
$output[$j] = array_push($output[$j],$column_values[$i][$j]);
}
}
?>
But when I do this, I get the correct number of keys in my $output array, but they all contain a bool(false). Any help?
This is the desired output:
Array (
[0] => array("string1","string4","string7","string10"),
[1] => array("string2","string5","string8","string11"),
[2] => array("string3","string6","string9","string12")
)
This is to make an array just for this structure of array, so you may change the code depanding on your needs ...
<pre>
<?php
$array = array(array("string1","string2","string3"),array("string4","string5","string6"),array("string7","string8","string9"));
$output=array();
for($i=0;$i<count($array[0]);$i++){
for($j=0;$j<count($array[0]);$j++){
$output[$i][$j] = $array[$j][$i];
}
}
print_r($output);
?>
</pre>
This is for your 'second' situation:
<?php
$array = array(array("string1","string2","string3"),array("string4","string5","string6"),array("string7","string8","string9"),array("string10","string11","string12"));
$output=array();
for($i=0;$i<count($array[0]);$i++){
for($j=0;$j<=count($array[0]);$j++){
$output[$i][$j] = $array[$j][$i];
}
}
print_r($output);
?>
Got it. Last loop should simply array_push($output[$j],$column_values[$i][$j]); instead of trying to set the variable $output[$j] = array_push(Yadda,yadda).

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories