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.
Related
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));
I have an array that looks like this:
array
(
[0] => personA
[1] => personB
)
and I want to add elements to each person like this:
array
(
[0] => personA
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => personB
)
I'm using this code:
foreach($proj as $key => $cat)
{
$proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);
}
That function returns an array that looks like this:
array
(
[0] => elemA
[1] => elemB
)
But obviously is not working, I get this result:
array
(
[0] => Array
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => Array
)
Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).
You'd have to build a more complex structure:
[0] => array(
'name' => 'personA'
'values' => array('elemA', 'elemB', 'elemC')
)
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.
username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);