I have an array in php like
array(1,3,0,6,0,6,9,0);
I want to remove '0' elements from this array. and get array like that array(1,3,6,6,9)
I am new user i cannot tell my question very well so sorry in advance please.
Filters out all 0.
$array = array_filter($array);
Another approach could be:
$arr = array(1,3,0,6,0,6,9,0);
$arr = array_filter($arr, function($el) { return $el != 0; });
Do like this
<?php
$arr = array(1,3,0,6,0,6,9,0);
foreach($arr as $k=>$v)
{
if($v==0)
{
unset($arr[$k]);
}
}
print_r($arr);
OUTPUT :
Array
(
[0] => 1
[1] => 3
[3] => 6
[5] => 6
[6] => 9
)
Related
I need to convert array of index array to single dimension array.
$a=[];
$a[0]=[1,2,3];
$a[1]=[4,5,6];
$a[2]=[7,8,9];
$a[3]=[1,4,7];
I need to merge these arrays as single array. Is there any built in function available for this? The expected output is
[1,2,3,4,5,6,7,8,9,1,4,7]
That output must be an single dimension index array
You can simple use foreach like:
$a=[];
$a[0]=[1,2,3];
$a[1]=[4,5,6];
$a[2]=[7,8,9];
$a[3]=[1,4,7];
$b = [];
foreach($a as $array){
foreach($array as $value){
$b[] = $value;
}
}
print_r($b);
Result:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 1
[10] => 4
[11] => 7 )
Suggest from #catcon you can use array_merge like:
$new_array = array_merge(...$a)
Reference:
array_merge
This is the correct solution refereed from How to Flatten a Multidimensional Array?
$a=[];
$a[0]=[1,2,3];
$a[1]=[4,5,6];
$a[2]=[7,8,9];
$a[3]=[1,4,7];
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
$output=iterator_to_array($it, FALSE);
$result = array_merge(...$a);
print_r($result);
The easiest way
From this question link:
<?php
$a=[];
$a[0]=[1,2,3];
$a[1]=[4,5,6];
$a[2]=[7,8,9];
$a[3]=[1,4,7];
$result = [];
array_walk_recursive($a,function($val) use (&$result){ $result[] = $val; });
var_dump($result);
Demo
My numbers is this;
$input =array{1,2,3,4,6,8,11}
I want to make it;
$input =array{1,2,3,4,5,6,7}
*changing 6 to 5, 8 to 6 and 11 to 7
I hope you are getting what I am trying to say.
try this
EDIT
$input =array(1,2,3,4,6,8,11);
for($i=0;$i<count($input);$i++)
{
if($input[$i]!=($i+1))
{
$input[$i] = ($i+1);
}
}
print_r($input);
use this
$input = array_values($input);
array_values() returns all the values from the array and indexes the array numerically.
to make it a base one array:
array_unshift($input, "dummy");
unset($input[0]);
EDIT:
I see, I may have misunderstood the question. try this:
$input_ = array();
for($i=1; $i <= count($input); $i++) {
$input_[] = $i;
}
print_r($input_);
This is an odd request and i'm not sure what the logic or reasoning behind it is. Nethertheless just flipping the array keys with the array values will produce this result. We insert an empty value and then remove it to give the index start of 1.
$input = array(1,2,3,4,6,8,11);
array_unshift($input,"");
unset($input[0]);
$flipped = array_flip($input);
try this
$input =array(1,2,3,4,6,8,11);
array_push($input,1);
$res = array_keys(array_values($input));
$data = array_shift($res);
echo "<pre>";
print_r($res);
result array
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
Say I got this array:
array([0] => 0 [1] => 0 [2] => 1), how can I put this as the value of the nth key of another array, making
$array = array(
[0] => array([0] => 0 [1] => 0 [2] => 1)
)
in this example it is the 0th key of array $array
I had browsed with PHP array functions, but I got tired looking for a function that does the same thing
EDIT.
Doing: $array[0] = array(0, 0, 1); works great for first loop, but if I would like to add another array as subarray, say array(1, 1, 1), my output should be
$array = array(
[0] => array([0] => 0 [1] => 0 [2] => 1 [3] => 1 [4] => 1 [5] => 1)
)
Note that the new array, was added at the end of the last subarray.
Please help! thanks!
EDIT
$x=0;
while($x<count($WOE_CONTROL)) {
$j=0;
while($j<=30) {
if ($WOE_CONTROL[$x+3]&(1<<$j)) {
echo '<br />';
echo '<strong>'.$Castles[$j].'</strong>';
$castle_data_holder[0] = $WOE_CONTROL[$x];
$castle_data_holder[1] = $WOE_CONTROL[$x+1];
$castle_data_holder[2] = $WOE_CONTROL[$x+2];
$castle_db[$j] = $castle_data_holder;
unset ($castle_data_holder);
}
if ($x+4 < count($WOE_CONTROL)) {echo " ";}
$j=$j+1;
}
$x=$x+4;
}
Sorry, there, I am trying to extract all the data in $WOE_CONTROL which actually contains binary data, then store them temporarily to $castle_data_holder then finally, add it to $castle_db
Just set the value of $array[0] to the array. This produces a multidimensional array.
$array[0] = array(0, 0, 1);
Is this u looking for?
$array[] = array(0,0,1);
$array[] = array(1,1,1);
$array[] = array(2,2,2);
// etc...
print_r($array);
EDIT
$array[]['a1'] = array(1,1,1);
$array[]['a2'] = array(2,2,2);
$array[]['a3'] = array(3,3,3);
// etc...
print_r($array);
Finally this is u need
$a = array(1,1,1);
$b = array(2,2,2);
$c = array(3,3,3);
$value = array_merge($a,$b,$c);
print_r($value);
EDIT
$newArray = array();
foreach($yourArray as $key => $value)
{
// I say if $value is an array that u want to merge
$arrayToMerge = $value;
$newArray = array_merge($newArray,$arrayToMerge);
}
print_r($newArray);
This solved my problem:
if (empty($castle_db[$j])) {
$castle_db[$j] = $castle_data_holder;
}
else {
$castle_db[$j] = array_merge($castle_db[$j],$castle_data_holder);
}
it is going to check if the array is empty if not use merge.
Thanks everyone!
Well this has been a headache.
I have two arrays;
$array_1 = Array
(
[0] => Array
(
[id] => 1
[name] => 'john'
[age] => 30
)
[1] => Array
(
[id] => 2
[name] => 'Amma'
[age] => 28
)
[2] => Array
(
[id] => 3
[name] => 'Francis'
[age] => 29
)
)
And another array
array_2 = = Array
(
[0] => Array
(
[id] => 2
[name] => 'Amma'
)
)
How can I tell that the id and name of $array_2 are the same as the id and name of $array_1[1] and return $array_1[1]['age']?
Thanks
foreach($array_1 as $id=>$arr)
{
if($arr["id"]==$array_2[0]["id"] AND $arr["name"]==$array_2[0]["name"])
{
//Do your stuff here
}
}
Well you can do it in a straightforward loop. I am going to write a function that takes the FIRST element in $array_2 that matches something in $array_1 and returns the 'age':
function getField($array_1, $array_2, $field)
{
foreach ($array_2 as $a2) {
foreach ($array_1 as $a1) {
$match = true;
foreach ($a2 as $k => $v) {
if (!isset($a1[$k]) || $a1[$k] != $a2[$k]) {
$match = false;
break;
}
}
if ($match) {
return $a1[$field];
}
}
}
return null;
}
Use array_diff().
In my opinion, using array_diff() is a more generic solution than simply comparing the specific keys.
Array_diff() returns a new array that represents all entries that exists in the first array and DO NOT exist in the second array.
Since your first array contains 3 keys and the seconds array contains 2 keys, when there's 2 matches, array_diff() will return an array containing the extra key (age).
foreach ($array_1 as $arr) {
if (count(array_diff($arr, $array_2[1])) === 1) {//meaning 2 out of 3 were a match
echo $arr['age'];//prints the age
}
}
Hope this helps!
I assume you want to find the age of somebody that has a known id and name.
This will work :
foreach ($array_1 as $val){
if($val['id']==$array_2[0]['id'] && $val['name']==$array_1[0]['name']){
$age = $val['age'];
}
}
echo $age;
Try looking into this.
http://www.w3schools.com/php/func_array_diff.asp
And
comparing two arrays in php
-Best
I got this array:
array (
0 => 'K.',
1 => 'Vrachtschip',
2 => 'L.',
3 => 'Gevechtsschip',
4 => 'Z.',
5 => 'Gevechtsschip',
6 => 'Kruiser',
7 => 'Slagschip',
8 => 'Bommenwerper',
9 => 'Vernietiger',
10 => 'Interceptor.',
)
of can I merge the items [0] with [1], because K. vrachtschip must be together.
same ass [2] and [3]; and [4] with [5]. if there is 1 letter and then a dot (k.) it must be merged with the following array item.
Anyone that can help me :)?
How about:
$arr = array (
'K.',
'Vrachtschip',
'L.',
'Gevechtsschip',
'Z.',
'Gevechtsschip',
'Kruiser',
'Slagschip',
'Bommenwerper',
'Vernietiger',
'Interceptor',
'B.',
);
$concat = '';
$result = array();
foreach ($arr as $elem) {
if (preg_match('/^[A-Z]\.$/', $elem)) {
$concat = $elem;
continue;
}
$result[] = $concat.$elem;
$concat = '';
}
if ($concat) $result[] = $concat;
print_r($result);
output:
Array
(
[0] => K.Vrachtschip
[1] => L.Gevechtsschip
[2] => Z.Gevechtsschip
[3] => Kruiser
[4] => Slagschip
[5] => Bommenwerper
[6] => Vernietiger
[7] => Interceptor
[8] => B.
)
Try to use a regular expression to test all entries of your array.
If an occurence is founded, concat the value of your entrie with the next.
I would try something like this:
for($idx=0, $max = count($array_in); $idx < $max; $idx++)
{
if(preg_match('/^[a-z]\.$/i', $array_in[$idx]))
{
$array_out[] = $array_in[$idx].$array_in[$idx+1];
$idx++;
}
else
{
$array_out[] = $array_in[$idx];
}
}
I'd probably do the following (pseudo code):
Create empty array for result
Iterate the original array
For each value: does it match /^[a-z]\.$/i?
If yes, see if original array contains a next element?
If yes, concatenate the two items and add to resulting array, skip next item in loop
If no (pt. 4 or 5) add directly to resulting array.