nested array not formatted as expected - php

Hello i have a problem with the logic to have a multidimentional array for each while loops that i had. I do not know why is it not working.
i wanted to have like this as concept
data >
ser_id > 14
org_name > "org a"
ser_id > 15
org_name > "org b"
but the output is like this
Array
(
[data] => Array
(
[ser_id0] => 14
[0] => Array
(
[0] => Gannon University
)
[ser_id1] => 15
[1] => Array
(
[0] => Lions Club
)
[ser_id2] => 16
[2] => Array
(
[0] => Rotatory Club
)
)
)
Can you help me with the logic. Here is the code i worked on with rows loop fetched from db.
$rs = $this->crud->fetchResultSet("services");
$rows = array();
$i=0;
while($row = $rs->fetch_assoc()){
//$rows = arra
$ser_id = $row["ser_id"];
$rows["data"][$i] = $ser_id;
$orgrs = $this->crud->fetchSingleResultSet("organizations","ser_id",$row['ser_id']);
$j=0;
while($innrow = $orgrs->fetch_assoc()){
$rows["data"][$i][$j] = $innrow["org_name"];
$j++;
}
$i++;
}

use multidimensional array like this =>>
$a[$i]['ser_id']=$row["ser_id"];
$a[$i]['org_name']=$innrow["org_name"]
and restore data like this
$max=count($a[$i]);
for($i=0;$i<$max;$i++){
echo 'ser_id =>'.$a[$i]['ser_id'];
echo 'org_name =>'.$a[$i]['org_name'];
}

Related

Merge array with same value [duplicate]

This question already has answers here:
PHP Unique Values from Column in Array
(5 answers)
Closed 5 months ago.
How to marge array result with same value, i was try array_unique but seems does not work for me
Code:
$row_content = mysql_fetch_assoc($sql);
$array = unserialize($row_content['content_id']);
foreach($array as $row) {
print_r($row);
}
Here is result
Array
(
[0] => v
[1] => 2040
)
Array
(
[0] => v
[1] => 526
)
Array
(
[0] => v
[1] => 200
)
Array
(
[0] => p
[1] => 2040
)
Array
(
[0] => p
[1] => 600
)
Need to merge [1] or 2040 in this example, also value V or P it does not matter here
Edit:
This is what i need to get as result
Array
(
[1] => 200
)
Array
(
[1] => 526
)
Array
(
[1] => 600
)
Array
(
[1] => 2040
)
If I don't misunderstood your requirements then this is what you need.
<?php
$ar1 = array(array('v',2040),array('v',526),array('v',200),array('v',2040),array('p',600));
$result = [];
foreach($ar1 as $k=>$v){
$result[]=$v[1];
}
$unique_v = array_unique($result);
print '<pre>';
print_r($unique_v);
print '</pre>';
?>
DEMO: https://3v4l.org/sGLlc
I don't exactly get what you are trying to say. But if you want to merge/add all the values of "p" and "v". You can do it like this:
$array = array(array('v',2040),array('v',526),array('v',200),array('v',2040),array('p',600));
$keeper = array();
foreach($array as $row){
if(array_key_exists($row[0],$keeper)){
$keeper[$row[0]] += $row[1];
}else{
$keeper[$row[0]] = $row[1];
}
}

Organize or group data from multidimensional array

I have a multidimensional array that I'm having difficulty trying to group and sort for a particular need. Here is the array:
Array (
[0] => Array (
[0] => Joe Smith
[1] => Array (
[0] => 3
[1] => 9
)
)
[1] => Array (
[0] => John Doe
[1] => Array (
[0] => 6
[1] => 12
)
)
[2] => Array (
[0] => Jack Frost
[1] => Array (
[0] => 2
[1] => 4
)
)
)
What I want to do is sort the numbers from smallest to shortest (i.e. 2,3,4,6,9,12), but also keep the names associated with those numbers. For example:
2 (Jack Frost),
3 (Joe Smith),
4 (Jack Frost),
6 (John Doe),
9 (Joe Smith),
12 (John Doe)
Any ideas how to sort by number and keep the names together? Thanks
UPDATE 1
Here is the PHP code I've used to list the numbers in order:
$users = get_users();
$names = array();
$days = array();
foreach( $users as $user ) {
$names[] = $user->display_name;
$days[] = $user->member_day;
}
$result = array_map( null, $names, $days );
$mdays = array();
foreach( $days as $d ) {
foreach( $d as $d2) {
$mdays[] = $d2;
}
}
for( $i; $i<=31; $i++ ) {
if( in_array($i, $mdays) ) {
echo $i . '<br>';
}
}
In the above code, $result prints out the above Array. Also, the for loop sorts the "days".
The end goal is to have 31 blocks and fill in the block by number with the name.
You can do something like as
$result = [];
foreach ($arr as $key => $value) {
foreach ($value[1] as $v) {
$result[$v] = $value[0];
}
}
ksort($result);
print_r($result);
Output:
Array
(
[2] => Jack Frost
[3] => Joe Smith
[4] => Jack Frost
[6] => John Doe
[9] => Joe Smith
[12] => John Doe
)
Note: This'll work fine till none of the array array contains the same key
something like this should work, and at the end you can soert you array
$final_array=array();
foreach (array as $arr){
foreach($arr as $aaa){
$final_array[]=array($aaa,$arr[0])
}
}

php mysqli result into an array not working as expected

I have a php /mysqli query and I want to populate an array with the results:
$query3 ="SELECT * FROM conditions";
$results = array();
if ($result = mysqli_query($conn, $query3)){
while($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
}
print_r($results);
Something is wrong here -its making arrays within arrays I think. (to be honest I am confused by this result)
How do I do this correctly!
Array (
[0] => Array ( [condition_id] => 1 [condition_name] => Epilepsy )
[1] => Array ( [condition_id] => 2 [condition_name] => ASD )
[2] => Array ( [condition_id] => 3 [condition_name] => BESD )
[3] => Array ( [condition_id] => 4 [condition_name] => HI )
[4] => Array ( [condition_id] => 5 [condition_name] => Medical )
[5] => Array ( ...
Thanks for all the help - now how should I create what I actually want which is one array with key=>value like this:
array (1=>epilepsy, 2=>ASd...) - the numbers refer to the primary key.
How do I populate an array from this query please?
Change your code as below :
while($row = mysqli_fetch_assoc($result))
{
$results[$row['condition_id']] = $row['condition_name'];
}
Move to PDO, Luke.
$results = $pdo->query("SELECT FROM conditions")->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($results);
Whoops! Is that all the code?

Show Store name by group in Alphabetical - php

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

Getting parent key in a nested array

I'm building a "nested" array fetching from database; here's my script:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$numbers[] = array("Page ");
}
I'd like to obtain the following array (with print_r() function), but I'm totally stuck on how to get the page number:
Array
(
[0] => Array
(
[0] => Page 1
[1] => 1
)
[1] => Array
(
[0] => Page 2
[1] => 2
)
[2] => Array
(
[0] => Page 3
[1] => 3
)
[3] => Array
(
[0] => Page 4
[1] => 4
)
)
I tried:
$numbers[] = array("Pagina " . key($numbers)+1, key($numbers)+1);
but it didn't lead to expected results (in my mind, it should get the current key number of the "parent" array and increment by 1)
Please, any help?
Thanks in advance
Just count by your own:
$n = 0;
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$n++;
$numbers[] = array("Page ".$n, $n);
}
Or, use count($numbers)+1 in your code:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$numbers[] = array("Page ".(count($numbers)+1), count($numbers)+1);
}
Thanks to datacompboy I finally came to this:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$counter = count($numbers)+1;
$numbers[] = array("Page " . $counter, $counter);
}

Categories