I made a function:
function xyz( $user_id_array = NULL ){
if ( ! $user_id_array ) {
$result = mysqli_query($con, "SELECT DISTINCT(benutzer_id) FROM anstellung;");
$user_id_array = mysqli_fetch_all($result);
}
print("<pre>".print_r($user_id_array,true)."</pre>");
}
Now my question is if I call the function like this:
xyz( array(6, 2, 3) );
xyz( );
The output looks like this:
Array
(
[0] => 6
[1] => 2
[2] => 3
)
Array
(
[0] => Array
(
[0] => 9
)
[1] => Array
(
[0] => 13
)
)
How is it possible to have the same dimensions of the arrays?
The mysqli_fetch_all should look like this as well:
Array
(
[0] => 9
[1] => 13
)
You can use mysqli_fetch_assoc() with a loop to construct the desired array:
if (!$user_id_array) {
$user_id_array = Array();
$result = mysqli_query($con, "SELECT DISTINCT(benutzer_id) FROM anstellung;");
while ($row = mysqli_fetch_assoc($result)) {
$user_id_array[] = $row['benutzer_id'];
}
}
I have the following array i want to show the count of inner array values Please help me out.
Array
(
[e1549b20-4cad-11e6-85b4-73d5cb14d4fe] => Array
(
[a029e160-4337-11e6-8db4-ad7de57838b4] => Array
(
[0] => b46b70a2-481a-11e6-8b19-00262d644487
[1] => b4696a1e-481a-11e6-8b19-00262d644487
)
[40eca780-48ef-11e6-8a04-eb9fe0a25fc5] => Array
(
[0] => b46b70a2-481a-11e6-8b19-00262d644487
[1] => b4696a1e-481a-11e6-8b19-00262d644487
)
[e5926390-44cf-11e6-bc85-19a184fbd10f] => Array
(
[0] => b4696a1e-481a-11e6-8b19-00262d644487
)
[51a44c00-4a53-11e6-81fe-313fe319f95b] => Array
(
[0] => b4696a1e-481a-11e6-8b19-00262d644487
)
)
)
Please Try with this functionality :
function getCount($arr, $count = 0) {
foreach ($arr as $value) {
if (is_array($value)) {
$count = getCount($value, $count);
} else {
$count = $count + 1;
}
}
return $count;
}
echo getCount($arr);
I want to group an array by a subarray's value. If I have an array like this:
Array
(
[0] => Array
(
[userID] => 591407753
[propertyA] => 'text1'
[propertyB] => 205
)
[1] => Array
(
[userID] => 989201004
[propertyA] =>'text2'
[propertyB] => 1407
)
[2] => Array
(
[userID] => 989201004
[propertyA] => 'text3'
[propertyB] => 1407
)
)
I want to sort to group this array by a subarray's value so I can have an array like this:
Array
(
[0]=>Array
(
[userID]=>59140775
[properties]=>Array
(
[0]=>text1
)
[propertyB]=>205
)
[1]=>Array
(
[userID]=>989201004
[properties]=>Array
(
[0]=>'text2'
[1]=>'text3'
)
[propertyB]=>1047
)
)
How can I make this?
Before I had tried this:
$result = array();
foreach ($userArray as $record)
{
$id=$record['userID'];
if(isset($result[$id]))
{
$result[$id]['propertyA'][]=array($record['propertyA']);
}
else {
$record["propertyA"]=array($record['propertyA']);
unset($record['tweet']);
$result[$id]=$record;
}
}
the problem was for the propertyA. I was an the result an additional property propertyA with the table like this:
Array
(
[0]=>Array (
[userID]=>989201004
[propertyA]=>'text2'
[properties]=>Array(
[0]=>'text2'
[1]=>'text3'
)
)
)
The following code should do the job. I hope it is self-explanatory:
$result = array();
foreach ($array as $record) {
if (!isset($result[$record['userID']])) {
$result[$record['userID']] = array(
'userID' => $record['userID'],
'properties' => array($record['propertyA']),
'propertyB' => $record['propertyB'],
);
}
else {
$result[$record['userID']]['properties'][] = $record['propertyA'];
}
}
$result = array_values($result);
i want to show stores name in alphabetically format. I have a store table where I am storing all details about a store including its name, logo, discription etc. I want the name will appear as a alphabetical group wise. Such as shown in example. and also show stores by clicking on [A] [B] [C] .... [Z]
A
--> 'Amazon'
--> 'Apple'
B
--> 'Ba...'
--> 'Be...'
<?php
$temp = array(); // would also generate a dynamic array
$result = mysql_query("SELECT LEFT(`name_en-GB`, 1 ) AS FirstLetter, `name_en-GB` FROM `store` ORDER BY FirstLetter, `name_en-GB`");
while ($row = mysql_fetch_array($result)) {
$temp[$row['FirstLetter']][] = $row['name_en-GB'];
}
Output:
Array
(
[1] => Array
(
[0] => 100bestbuy.com
)
[A] => Array
(
[0] => Adlabsimagica
[1] => Airasiago
[2] => Airtel
)
[B] => Array
(
[0] => Babyhugz
[1] => Babyoye
[2] => Bagskart
[3] => Basicslife
[4] => Bata
)
[C] => Array
(
[0] => Clifton
[1] => Coke2Home
[2] => Condompoint
[3] => Croma
)
[D] => Array
(
[0] => Dabur
[1] => Dealofthedayindia.com
[2] => Dhamaal
[3] => Dominos
)
You can use another array to save position where you are.
Something like this:
<?php
$temp = array(); // would also generate a dynamic array
$position = array();
foreach (range('0', '9') as $i) {
$position [$i] = 0;
}
foreach (range('a', 'z') as $i) {
$position [$i] = 0;
}
$result = mysql_query("SELECT LEFT(`name_en-GB`, 1 ) AS FirstLetter, `name_en-GB` FROM `store` ORDER BY FirstLetter, `name_en-GB`");
while ($row = mysql_fetch_array($result)) {
$pos = $position[$row['FirstLetter']];
$position ['FirstLetter'] ++;
$temp[$row['FirstLetter']][$pos] = $row['name_en-GB'];
}
?>
Now you have an array with positions in subarray.
Should be working, just be carefully to have all firstletters uppercase/downcase or verifiy before
I'm busy with sorting the structure of a menu in my application. Once the menu is reorderd by the user, the values (say; Menu item 1, Menu item 2, etc) are still in the same place.
Now I have two arrays, one that holds the way they are sorted (Array 1) and one that holds the values of the menu items. (Array 2)
Example of both arrays;
(Array 1, that holds the keys)
Array
(
[0] => 1
[1] => 2
[2] => 0
)
The above array's values are the keys for the new array.
(Array 2, holds the values)
Array
(
[0] => value_0
[1] => value_1
[2] => value_2
)
So I thought it would be best to create a new array which consist out of;
The values of Array 1
The values of Array 2
However, i'm running into a problem. I want the values in array 2 to stick to their keys. So lets say I change the position of value_0 to the last, the new array would look like this;
Array
(
[1] => value_1
[2] => value_2
[0] => value_0
)
Is there a way to achieve this or am I doing it completely wrong?
Edit
Ok, so multidemensional array it is. However i'm having problems creating one.
Array 1 and Array 2 both come from the database. Array 1 with the sorting order and Array 2 contains the values. Now, the values in array 2 are stored like this; value1,value2,value3. So to be able to work with them I explode on , (comma).
The results on the fetchs are both different;
For the first array it returns as many as how many values there are.
(So if there are 3 values, it will return 3 different positions.)
For the second array it will return 18 records, since this is tied to
other menu items (sub menu's etc).
So for the first array I do;
while ($row = mysql_fetch_assoc($result_query_test)) {
$positions[] = $row['position'];
}
For the second array I do;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
}
From then on i'm having problems creating the multidimensinonal array;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
foreach ($positions as $new_key) {
foreach ($array_values as $value) {
$new_array[] = array('key' => $new_key, 'value' => $value);
}
}
}
Edit two:
This is what I use now;
(Since $all_values is a multidimensional array because I have to explode on the values beforehand.)
foreach ($all_values as $values) {
foreach ($values as $key => $value) {
$new_array[] = array('key' => $positions[$key], 'value' => $value);
}
}
This is what the $new_array returns;
Array
(
[0] => Array
(
[key] => 0
[value] => value_0
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 1
[value] => value_1
)
[3] => Array
(
[key] => 0
[value] => value_0
)
[4] => Array
(
[key] => 2
[value] => value_2
)
[5] => Array
(
[key] => 1
[value] => value_1
)
Now I need to get the values and implode them with comma's. However, since not every 3 values (value_0, value_1, value_3) are together I can't do that now.
In this example there are 3 keys, (0,1,2) which should be a different array along with their values, like you did in your example:
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)
Why not make a multidimensional array?
$arrayThree =
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)
No matter what order they're in, the key and value are always the set.
foreach ($arrayThree as $tempArray)
{
echo $tempArray['key'];
echo $tempArray['value'];
}
Create Array
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
$query = 'SELECT key FROM table1 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayOne[] = $data['key'];
}
$query = 'SELECT value FROM table2 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayTwo[] = $data['value'];
}
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
You can always use the mysqli or PDO versions, if you're using them.
Example Data
//THESE MIMIC YOUR SELECT RESULTS
$test_keys = array(1,2,3);
$test_values = array('value_1', 'value_2', 'value_3');
//DEFAULTS
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
//WHILE FIRST SELECT
for($i=0;$i<count($test_keys);$i++)
{
$arrayOne[] = $test_keys[$i];
}
//WHILE SECOND SELECT
for($i=0;$i<count($test_values);$i++)
{
$arrayTwo[] = $test_values[$i];
}
//MAKE THE FINAL ARRAY
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
//CHECK THE OUTPUT FOR THE NEW ARRAY
echo '<pre>'.print_r($arrayThree,true).'</pre>';
Example Output
Array
(
[0] => Array
(
[key] => 1
[value] => value_1
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 3
[value] => value_3
)
)
Imploded List
$implodeValues = array_map(function($item) { return $item['value']; }, $arrayThree);
$implodeVariable = implode(',', $implodeValues);
echo $implodeVariable;
Implode Output
value_1,value_2,value_3
I think you can obtain what you want doing this:
$new = array();
for($i = 0, $len = count($array1), $i < $len, ++$i) {
$new[$array1[$i]] = $array2[$i];
}
now $new contains the values in the order you want them