I have two-dimensional array and I want replace second of two-dimensional array on random number of second array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 500
[4] => 600
[5] => 700
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
I want get
Array (
[1] => 5 (<- random from first array)
[2] => 6 (<- random from second array)
)
I tried to do:
foreach($variables as $key => $val) {
$variables = str_replace($val, $val[array_rand($val)], $variables);
}
Why it doesnt work?
foreach($variables as $key => $val) {
$variables[$key] = $val[array_rand($val)];
}
foreach ($variables as &$var) {
$var = array_rand($variables[$var]);
}
Related
I don't really know how to explain what I would like to do, so here is an example. I have an 2D array, like this one :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
)
[1] => Array
(
[1] => value 1
[2] => value 2
)
[2] => Array
(
[1] => value 1
[2] => value 2
)
)
And I would like to have this :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[1] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[2] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
)
Can someone help me ? Thanks very much.
Just loop it and make sure to reference & the $val to update the original array. Then just append the new item:
foreach($array as &$val) {
$val[] = 'value 3';
}
The other way:
foreach($array as $key => $val) {
$array[$key][] = 'value 3';
}
I have the next probleme, i want to filter a multidimensional and multi level array with for the uniqe one.
An example:
Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
The befored array i want to make it with the uniqe values.
I don't know if this is the fastest / shortest answer, but the code below might work for you:
#Function to make a multidimensional array unique
function makeUnique(&$array)
{
foreach($array as $key => &$value)
{
if(is_array($value))
{
makeUnique($value);
$value = array_unique($value);
}
}
return $array;
}
#Example of your array
$exampleArray = Array(
'Home' => Array(
'Kids' => Array(
'For sleeping' => Array(
0 => 'Sleeping Bags',
1 =>'mattress',
2 =>'mattress')
)
)
);
#Make the array unique and print the results
makeUnique($exampleArray);
print_r($exampleArray);
I have resolved this problem. The solution for this problem is:
$test = Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
foreach ($test as $key=>$value){
foreach ($value as $key2 => $value2) {
foreach ($value2 as $key3=>$value3) {
$cat[$key][$key2][$key3]= array_unique($value3);
}
}
}
I have this arbitrary multi dimensional array.
Array (
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[5] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[10] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[15] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[1] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
)
I wanna run a for loop to extract the data of each subarray.
But I cannot do a simple for loop because the index (0,5,10,15,1) is arbitrary.
Is there a way to run a for loop then skip the sub array if it is empty?
Thanks!
This will take $array and loop though it, echoing the keys.
You have an array in an array, you can place a foreach in a foreach:
// First we take the main array ($array) and loop though its values
foreach( $array as $main_key =>$sub_array){
echo $main_key.": <br />\n"; // echo the key, some extra html to format
// the values of the mainarray are arrays themselves, just loop again:
foreach($subarray as $sub_key =>$subvalue){
echo '- '.$subvalue."<br />\n";
}
}
There's a bit of a trap here if you foreach in a foreach:
foreach($array as $key =>$value){
foreach($value as $key=>$value){ /* ... */; }
}
This will create very weird results. The inner foreach uses the same parameter-names and will mess everything up.
I have an array, let call it $mainArray, which looks like this: -
Array
(
[1] => Array
(
)
[5] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
[80] => Array
(
[0] => 20
[1] => 40
[2] => 50
[3] => 60
)
[777] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
[666] => Array
(
[0] => 1234
[1] => 5678
[2] => 20
[3] => 9865
)
[555] => Array
(
[0] => 111
[1] => 222
[2] => 333
[3] => 444
)
)
What I want to do is create 2 new arrays: -
1) Where values are equal to the key names of $mainArray, but only those where the sub-array (if there is one) contains the value "20" somewhere in it. For example my new array (call it $arrayOne) will be [0] => 5, [1] => 80, [2] => 666.
2) Similar to above, but where there's either no sub-array or, if there is, it doesn't include "20" as a value. So that (call it $arrayTwo) would be [0] => 1, [1] => 777, [2] =>555.
I've tried loads of for each loops and even a little RecursiveIteratorIterator (whatever that is!) but can't seem to reference keys and values in the way that I need to. Any help would be much appreciated!
Will this do?:
<?php
foreach( $mainArray as $mKey => &$mVal )
{
if( in_array( 20, $mVal ) )
{
$arrayOne[] = $mKey;
}
else
{
$arrayTwo[] = $mKey;
}
}
I trust you can create a function which would check if array contains 20 as it's value or not. Let's call this function has20.
You two new arrays would then be array_filter($mainArray, 'has20') and array_filter($mainArray, function ($x) {return !has20($x);})
You can do it like this:
$newArray = array();
foreach($mainArray as $key => $subArray) {
if (in_array(20, $subArray)) {
$newArray[] = $key;
}
}
I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}