This is test code designed to take an array of arrays, shuffle them, and assign the values to a ['data'] element in a parent array, then to delete one element. It's very basic just to illustrate a point:
$g = //some array;
shuffle($g);
foreach ($g as &$r) {
shuffle($r);
}
$tables[] = array('name' => 'table1', 'data' => $g);
shuffle($g);
foreach ($g as &$r) {
shuffle($r);
}
$tables[] = array('name' => 'table2', 'data' => $g);
shuffle($g);
foreach ($g as &$r) {
shuffle($r);
}
$tables[] = array('name' => 'table3', 'data' => $g);
unset($tables[0]['data'][0][0]);
print_r($tables);
When $g is an array with more than one element, and the nested arrays have more than one element, it works fine. The first value in the first nested array in the data element is deleted:
$g = array('12' => array('11111', '22222'), '56' => array('55555', '66666'));
// Output
Array
[0] => Array
[name] => table1
[data] => Array
[0] => Array
[1] => 66666
[1] => Array
[0] => 11111
[1] => 22222
[1] => Array
[name] => table2
[data] => Array
[0] => Array
[0] => 11111
[1] => 22222
[1] => Array
[0] => 55555
[1] => 66666
[2] => Array
[name] => table3
[data] => Array
[0] => Array
[0] => 55555
[1] => 66666
[1] => Array
[0] => 11111
[1] => 22222
When $g is an array with one element, the first element in all data elements is removed, which is not expected:
$g = array('12' => array('11111', '22222'));
// Output
Array
[0] => Array
[name] => table1
[data] => Array
[0] => Array
[1] => 22222
[1] => Array
[name] => table2
[data] => Array
[0] => Array
[1] => 22222
[2] => Array
[name] => table3
[data] => Array
[0] => Array
[1] => 22222
$g = array('12' => array('11111'));
// Output
Array
[0] => Array
[name] => table1
[data] => Array
[0] => Array
(Empty)
[1] => Array
[name] => table2
[data] => Array
[0] => Array
(Empty)
[2] => Array
[name] => table3
[data] => Array
[0] => Array
(Empty)
I can't see how this could be expected behavior. And it's been a long day. So can someone put me out of my misery and tell me what I'm missing here?
Thanks.
Later:
Just saw this which may be my answer:
PHP shuffle not working like expected on my nested array
Later again:
Yes, it was. Added unset($r) after the foreach() loops and it works fine. No replies needed!
Just saw this which provided the answer:
PHP shuffle not working like expected on my nested array
So just added unset($r) after the foreach() loops and it works fine.
Related
I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ
So I got an array like:
Array
(
[0] => Array
(
[ids] => Array
(
[id] => id1
)
[name] => name1
[number] => 1
)
[1] => Array
(
[ids] => Array
(
[id] => id2
)
[name] => name2
[number] => 2
)
)
And I want to construct new multidimensional array based on the elements of it, but adding some new keys with empty values like(all the keys will have other names in new array, it's just simplified):
Array
(
[0] => Array
(
[id] => id1
[firstname] => name1
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 1
)
[1] => Array
(
[id] => id2
[firstname] => name2
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 2
)
)
How do I do it? Was thinking about array_push inside foreach loop, but it's not gonna do the job because of the empty keys I want and different order of elements. I also know how to access the nested value of [id] but still no idea about how to construct and move values to the new array for each element.
You can do it like below:-
$final_array = array();
foreach($array as $arr){
$final_array[] = array('id'=>$arr['ids']['id'],'firstname'=>$arr['name'],'lastname'=>'','somedata'=>'','somemoredata'=>'','ordernumber'=>$arr['number']);
}
print_r($final_array);
Output:-https://eval.in/831090
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.
This question already has answers here:
multi dimensional array in random order
(4 answers)
Closed 6 years ago.
Is it possible to get random data from an array?
See My Array:
Array
(
[0] => Array
(
[0] => My Data
[1] => Airport
[2] => Md
)
[1] => Array
(
[0] => Live in fear
[1] => Science
[2] => Sc
)
[2] => Array
(
[0] => State History
[1] => Government
[2] => MP
)
[3] => Array
(
[0] => Real Estate
[1] => Other
[2] => Property
)
[4] => Array
(
[0] => Real State
[1] => Not Sure
[2] => NoData
)
)
I need this type of random output...
Array
(
[0] => Array
(
[0] => My Data
[1] => Airport
[2] => Md
)
[1] => Array
(
[0] => Real State
[1] => Not Sure
[2] => NoData
)
[2] => Array
(
[0] => My Data
[1] => Airport
[2] => Md
)
[3] => Array
(
[0] => State History
[1] => Government
[2] => MP
)
[4] => Array
(
[0] => Live in fear
[1] => Science
[2] => Sc
)
)
Try the following shuffle function.Hope it would help you.
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[] = $list[$key];
}
return $random;
}
$arr = array();
$arr[] = array('id' => 50, 'foo' => 'hello');
$arr[] = array('id' => 17, 'foo' => 'byebye');
$arr[] = array('id' => 19, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
You could simply use shuffle()
bool shuffle ( array &$array )
This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.
shuffle($array); // Shuffles your array keys randomly every time.
shuffle() will be a better option in getting out the random value from multi dimensional array.
Reference: http://php.net/manual/en/function.shuffle.php
shuffle() Example:
The shuffle() function randomizes the order of the elements in the array.
This function assigns new keys for the elements in the array. Existing keys will be removed
<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
shuffle($my_array);
print_r($my_array);
?>
Output:
Array ( [0] => red [1] => yellow [2] => green [3] => blue [4] => purple )
//The Output will keep shuffling if you refresh the browser.
I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}