php delete empty array indexes - php

I have two+ Arrays which I set in a while loop.
Every array have a dependence to the other array by the index. And every array have duplications on the same index.
What I want to do is, to delete the duplications and after that to delete the empty indexes of each array, so that all arrays have the same positions like before but without duplications.
What I have tried is this:
$array1 = array_unique($array1);
$array2 = array_unique($array2);
print_r($array1);
echo "<br>";
print_r($array2);
echo "<br>";
sort($array1);
sort($array2);
print_r($artNrArray);
echo "<br>";
print_r($pnameArray);
echo "<br>";
This is the output:
Array (
[0] => 0100_64
[9] => 1999_13
[18] => 5999_12
[19] => 0204_22
[21] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[9] => Käbel
[18] => Kabel_test123
[19] => K-A21-V-IBIS-13-3-4-0-0-0m
[21] => K-CAN-17-2m )
Array (
[0] => 0100_64
[1] => 0204_22
[2] => 0241_75
[3] => 1999_13
[4] => 5999_12 )
Array (
[0] => K-A21-V-IBIS-13-3-4-0-0-0m
[1] => K-CAN-17-2m
[2] => Kabel_test123
[3] => Käbel
[4] => intKab-4xAWG22-S-oE-oE-K3 )
The problem if I sort the arrays is, that the dependence of the array contet is changed. What I want to have is this:
Array (
[0] => 0100_64
[1] => 1999_13
[2] => 5999_12
[3] => 0204_22
[4] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[1] => Käbel
[2] => Kabel_test123
[3] => K-A21-V-IBIS-13-3-4-0-0-0m
[4] => K-CAN-17-2m )
I have to use array_unique() to delete the duplications.
So how can I delete the array indexes with empty value and contraxt the array from 0 - 4?

Try the next:
$array1 = array_values(array_unique($array1));
$array2 = array_values(array_unique($array2));

Related

Remove duplicate data in multidimension array

I want to remove duplicate data in my array.
That's my array :
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
My multidimensionnal array :
Array
(
[0] => Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
)
[1] => Array
(
[0] => fraise
[1] => pomme
[2] => framboise
)
[2] => Array
(
[0] => ananas
[1] => citron
[2] => raisin
)
[3] => Array
(
[0] => pêche
[1] => pruneau
[2] => pomme
)
)
and that's my code to try remove duplicate data:
$monTableau = array_map("unserialize", array_unique(array_map("serialize", $monTableau)));
Don't work unfortunately :(
Advance Thanks,
Your code does work properly, but there is no duplicates in the main array. If you want one single array as output that contains only unique elements of the whole list take a look at this code:
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
$merged = call_user_func_array('array_merge', $monTableau);
$unique = array_unique($merged);
Output of $unique:
Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
[3] => fraise
[5] => framboise
[6] => ananas
[7] => citron
[8] => raisin
[10] => pruneau
)
Duplicates of pomme have been removed

PHP check if array value occurs in all multidimensional arrays

I have the following multi dimensional PHP array. What I'm trying to do is return only the value which occurs in all of the arrays.
So in the array below the only value which occurs in every array is "2018-02-22", so I want to create a new array with only this value.
I feel like it can't be too difficult, but I just can't get my head around how to do this. If any one can help it would be much appreciated!
Array
(
[0] => Array
(
[0] => 2018-02-22
)
[1] => Array
(
[0] => 2018-02-22
[1] => 2018-02-21
[2] => 2018-02-20
[3] => 2018-02-16
[4] => 2018-02-14
)
[2] => Array
(
[0] => 2018-02-20
[1] => 2018-02-19
[2] => 2018-02-21
[3] => 2018-02-22
[4] => 2018-02-14
)
[3] => Array
(
[0] => 2018-02-22
[1] => 2018-02-12
[2] => 2018-02-01
)
)
So to clarify the output I'm aiming for is:
Array
(
[0] => 2018-02-22
)
You can do it by array_intersect, to get common value 2018-02-22.
<?php
$dates = [
["2018-02-22","2018-02-23"],
["2018-02-22","2018-02-24"],
["2018-02-22","2018-02-25"],
];
$common = array_shift($dates);
foreach($dates as $key=>$date){
$common = array_intersect($common, $date);
}
print_r($common);
?>
Live Demo
Output is :
Array (
[0] => 2018-02-22
)

print out values from a php array

Array (
[0] => Array ([username] => khaled [0] => khaled)
[1] => Array ([username] => Nariman [0] => Nariman)
[2] => Array ([username] => test1 [0] => test1)
[3] => Array ([username] => jack [0] => jack)
[4] => Array ([username] => mmmm [0] => mmmm)
[5] => Array ([username] => qqq [0] => qqq)
[6] => Array ([username] => wwwdwd [0] => wwwdwd)
[7] => Array ([username] => wddww [0] => wddww)
[8] => Array ([username] => maxa [0] => maxa)
)
I tried $posts['username'][0]/[0]['username'] ... didnt work!
I want to print out some values of the array, like the username for example. How to do it?
To get a single variable (so your username in your case), you do the following:
$username = $posts[0]['username'];
This will get the username from the first nested Array in your actual array. You can modify this to get every username from each nested array by making a for loop so you get the rest of the usernames.
You can use the following solution using foreach:
foreach ($arr as $arrItem) {
echo $arrItem['username'];
echo $arrItem[0];
}
You can also use a for loop:
for ($i = 0; $i < count($arr); $i++) {
echo $arr[$i]['username'];
echo $arr[$i][0];
}
demo: https://ideone.com/he6h7r
On you array, key ['username'] does not exist.
If you indent the array, they read as this:
Array
[0]
[username] => khaled
[0] => khaled
[1]
[username] => Nariman
[0] => Nariman
...
Because this, you can read $posts[0][0] or $posts[0][username], but, you cant read $posts[username] because didn't exist.

get count of entries in multi-dimensional array with comma separated values- php

How to get count of entries in an array below?
I tried Count arrays comma separated values and didnot get desired solution.Please suggest a method.
For below sample count is 11.
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
A simple recursive function should work that uses explode() and array_merge():
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));

Build array by string name OR make multiple array by string values

I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it

Categories