storing db contents in array PHP - php

I am not sure how to do this, but if it can be done can anyone please help me in this.
Basically I have 3 columns in my table:
ID Set Result Case
1 Set1 PASS 101
2 Set2 FAIL 102
3 Set2 FAIL 101
4 Set1 FAIL 101
5 Set1 PASS 104
$set = $row['Set'];
What I am trying to achieve is , foreach of these Set, store its associated Result and Case in an array.

$arr = array();
foreach ($records as $key => $value)
{
$arr[$key]['Result'] = $value['Result'];
$arr[$key]['Case'] = $value['Case'];
}
echo '<pre>';
print_r($arr);
echo '</pre>';
In light of your comment:
foreach ($records as $key => $value)
{
$arr[$key][$value['Result']] = $value['Case'];
}
In light of your most recent comment:
foreach ($records as $key => $value)
{
$arr[$value['Set']][$value['Result']] = $value['Case'];
}

After reading comments I thought I'd take a crack at it.
First of all: What you asking for will not work unless you are going to check for duplicate {result} keys in $array[Set2][{result}] and lowercase them if there are duplicates as in your comment, which I don't know why you'd do. It would be confusing and strikes me as nonsensical. To wit:
$arr[Set2][FAIL] vs. $arr[Set2][fail]
If you do it as shown above [in Alix Axel's third code block], you'll do:
$arr[Set2][FAIL] = 102 then overwrite that array index value with $arr[Set2][FAIL] = 101, causing you to lose data.
To put it another way, you are using a combination of the "set" and "result" as a "combined key" so to speak, which you CANNOT DO as the combinations are not unique (Set2 FAIL, Set2 FAIL). I know it's an annoying answer, but you should take a look at what you are doing and why, as I have a hunch you are going about it the wrong way. You probably want an array like:
Array
(
[Set1] => Array
(
[101] => 'FAIL'
[102] => 'PASS'
)
[Set2] => Array
(
[101] => 'FAIL'
[102] => 'FAIL'
)
)
or something, but even then it won't work as you have some Set/Case pairs both passing and failing. Because of this, the only thing you can do here is use the "id" as an index:
Array
(
[1] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '101'
)
[2] => Array
(
[Set] => 'Set1'
[Result] => 'FAIL'
[Case] => '101'
)
)
But I can't even tell you how to do that, cuz you haven't told us how your query results array is structured in the first place!! So step 1) Please print_r or var_dump the query results.

// assuming
$myArray = array();
$result = mysql_query("SELECT * FROM table ORDER BY Set ASC");
while ($rows = mysql_fetch_assoc($result)) {
for ($i = 0; $i <= count($rows); $i++) {
$myArray[$rows['ID']]['Set'] = $rows['Set'];
$myArray[$rows['ID']]['Result'] = $rows['Result'];
$myArray[$rows['ID']]['Case'] = $rows['Case'];
}
// output
Array
(
[1] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '101'
)
[2] => Array
(
[Set] => 'Set1'
[Result] => 'FAIL'
[Case] => '101'
)
[3] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '104'
)
[4] => Array
(
[Set] => 'Set2'
[Result] => 'FAIL'
[Case] => '102'
)
[5] => Array
(
[Set] => 'Set2'
[Result] => 'FAIL'
[Case] => '101'
)
)

Related

Cycling as an array returned by the method fetch_assoc () PHP & Mysqli

I ask those who have a bit of kindness to help me in this thing, I would like to cycle this dynamic array returned from a store procedure mysql, since it is dynamic and will never know the index I have no idea how to do, I know only the array with two key is the title that will give the list and arrays with [NomeProdotto] etc .. will list items.
This is Array to loop:
Array
(
[0] => Array
(
[NomeMenu] => Pizze
[IDMenu] => 1
)
[1] => Array
(
[IDMenu] => 1
[IDProdotto] => 2
[NomeProdotto] => Eurobar
[PrezzoProdotto] => 5.00
[IngredientiProdotto] => Pomodoro, Mozzarella, Patate bollite, Prosciutto
)
[2] => Array
(
[IDMenu] => 1
[IDProdotto] => 4
[NomeProdotto] => Parripusu
[PrezzoProdotto] => 6.00
[IngredientiProdotto] => Pomodoro, Mozzarella, Salsiccia, Funghi, Origano
)
[3] => Array
(
[IDMenu] => 1
[IDProdotto] => 5
[NomeProdotto] => U Chianu a Muccusa
[PrezzoProdotto] => 4.00
[IngredientiProdotto] => Pomodoro, Mozzarella, Origano
)
[4] => Array
(
[IDMenu] => 1
[IDProdotto] => 6
[NomeProdotto] => Vaddruni
[PrezzoProdotto] => 5.00
[IngredientiProdotto] => Pomodoro, Mozzarella, Piselli, Uovo, Prosciutto cotto, Funghi, Origano
)
[5] => Array
(
[NomeMenu] => Supplementi
[IDMenu] => 3
)
[6] => Array
(
[IDMenu] => 3
[IDProdotto] => 8
[NomeProdotto] => Prosciutto
[PrezzoProdotto] => 1.00
[IngredientiProdotto] =>
)
)
I solved this way
$i = 0;
$menus = array();
if ($mysqli->multi_query("CALL getMenuLocale(" . $_REQUEST['locale'] . ")")) {
while ($mysqli->more_results()) {
$mysqli->next_result();
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_all(MYSQL_ASSOC)) {
$menus[$i] = $row;
}
$result->free();
}
$i++;
}
}
but I get an error as soon as published on Godaddy :
Fatal error: Call to undefined method mysqli_result::fetch_all() on line 758
I read that the solution might be to use fetch_assoc () instead of fetch_all (), and it returns the array mentioned above, how can I cycle through php
First of all, don't use fetch_all. As the name implies, it returns the complete dataset. So use fetch_assoc, this will return a row of the result in the form of an array.
Like this:
while ($row = $result->fetch_assoc()) {
//
}
This leaves you with $row being the array with the irregular number of elements. You can also iterate through this array. So the code would look like:
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
//$key is 'IDMenu' for example
//$value is '1' for example
//you can then use these variables
}
}
This way it doesn't matter if you have 2 of 5 elements in the array, you can still use what values you have.
I also see you are trying to add the $row array to another $menus array? What is the purpose of this? Because you are essentially recreating the query result this way...
All things aside, perhaps it is a good idea to look at some tutorials if you have a hard time with working with arrays ;)

JSON PHP Decode

i have an extern JSON File and no problems to get Airline, Price, etc..
But how can i get [ACE] ?
[success] => 1
[data] => Array
(
[ACE] => Array
(
[0] => Array
(
[price] => 477
[airline] => AB
[flight_number] => 2434
[departure_at] => 2014-08-09T12:30:00Z
[return_at] => 2014-08-24T08:35:00Z
[expires_at] => 2014-04-03T22:46:17Z
)
)
$foo = $json['data']['ACE']; should do it.
Unless you want to get the key from the $data array, in which case:
foreach ($json['data'] as $k=>$v) {
$foo = $k; // this is 'ACE'.
break;
}
Edited as per comment.
['ACE'] is an array?
Your getting the data from it starting with the first - [0]
Then the ['price'] of the first one?

php array merge with value of parent array

I need to merge an array with value of parent array.
$testArr=unserialize('a:6:{s:5:"queue";a:2:{i:6;s:1:"5";i:5;s:1:"2";}s:3:"sum";a:2:{i:6;s:3:"765";i:5;s:3:"2.1";}s:7:"sumAccD";a:2:{i:6;s:3:"543";i:5;s:3:"3.1";}s:7:"sumAccC";a:2:{i:6;s:2:"54";i:5;s:3:"3.3";}s:7:"comment";a:2:{i:6;s:12:"test comment";i:5;s:6:"111222";}s:3:"yt0";s:0:"";}');
$ret = array();
foreach ($testArr as $pkey => $pval) {
if (is_array($pval)) {
foreach ($pval as $pvkey => $pvval) {
$ret[$pvkey] = array($pkey => $pvval);
}
}
}
echo '<pre>', print_r($ret), '</pre>';
In this case it prints out
Array
(
[6] => Array
(
[comment] => test comment
)
[5] => Array
(
[comment] => 111222
)
)
1
Unfortunally it print out only comment. I need to add other rows: queue,sum,sumAccD,sumAccC. Array must look like this:
Array
(
[6] => Array
(
[queue] => 5
[sum] => ''
....
[comment] => test comment
)
[5] => Array
(
[queue] => 2
[sum] => 2.1
....
[comment] => 111222
)
)
1
Please help merge them.
Thanks.
Look at this line:
$ret[$pvkey] = array($pkey => $pvval);
You're assigning the key to a new array every time, overwriting what was previously there.
In your case, 'comment' is the last key that is processed, so that's going to be the only key in the final array.
Instead of this, you could define a new array only once outside the inner for, like this:
$ret[$pvkey] = array();
And then assign your values to that array in the inner for loop as you would normally do (so no more creating arrays there!)
Problem solved by replacing
$ret[$pvkey] = array($pkey => $pvval);
with
$ret[$pvkey][$pkey] = $pvval;

Compare two multi-multi-dimensionals-arrays

I got the following array (shortened ... multiple dot mean that they are data there, both array start at entry 1 until the end.
Array
(
[12U_S_136_15_29_141] => Array
(
.....
[35] => Array
(
[stop_sequence] => 35
[stop_id] => 1601394
)
.....
[46] => Array
(
[stop_sequence] => 46
[stop_id] => 122052
)
[47] => Array
(
[stop_sequence] => 47
[stop_id] => 136208
)
[48] => Array
(
[stop_sequence] => 48
[stop_id] => 128163
)
)
[12U_S_141_57_6_141] => Array
(
[1] => Array
(
[stop_sequence] => 1
[stop_id] => 1601394
)
.....
[12] => Array
(
[stop_sequence] => 12
[stop_id] => 122052
)
[13] => Array
(
[stop_sequence] => 13
[stop_id] => 136208
)
[14] => Array
(
[stop_sequence] => 14
[stop_id] => 128163
)
)
)
As you can see, both array end are equal... 35 = 1, 46 = 12, ..., 48 = 14. By equal, I mean the same stop_id but will always be diffrent for stop_sequence and of course the array entry number.
I want to know how I can compare the entire array against the other so I can know if, here let's say, the second array match the first one at 100% (except we don't look for stop_sequence so this can be different. So in this case, both will be mark as "equal", but if let's say the last entry had a different stop_id (entry 48 would be != from the entry 14), the array will be mark as "not equal".
Anyone have a path to lead me ? I keep thinking but do not know how. I've tryed array_compare but this leaded to nothing :\
Thanks
EDIT
Nothing can be change to the database. Also, the array need to be created this way (with weird text, ie 12U_S_136_15_29_141). I can do whatever in PHP.
fetch the stop_id in to new array:s and compare thous
here is a useful function
function sub_array($array, $sub_key)
{
$new_array = array();
if(is_array($array) AND $array)
{
foreach($array as $key => $data)
{
$new_array[$key] = $data[$sub_key];
}
}
return $new_array;
}
then just compare the arrays
if(sub_array($a['12U_S_136_15_29_141'], 'stop_id') == sub_array($a['12U_S_141_57_6_141'], 'stop_id'))
or if you know what stop_id that matches:
$first_stop_id_list = sub_array($a['12U_S_136_15_29_141'], 'stop_id');
$secound_stop_id_list = sub_array($a['12U_S_141_57_6_141'], 'stop_id');
$matches = array_intersect($first_stop_id_list, $secound_stop_id_list);
if(!$matches)
echo "No stop_id matches";
else if($first_stop_id_list = $secound_stop_id_list)
echo "all element was in both";
else if($matches == $first_stop_id_list)
echo "all element in first was in secound";
else if($matches == $secound_stop_id_list)
echo "all element in secound was in first";

Problems with array when getting data from a database

I'm trying to get some data from my database, and then pass it into an array for later use. I am using MySQLi for my driver.
Here's my code:
// Build a query to get skins from the database
$stmt = $mysqli->prepare('SELECT id, name, description, author, timestamp, url, preview_filename FROM `skins` LIMIT 0, 5');
$stmt->execute();
$stmt->bind_result($result['id'], $result['name'], $result['desc'], $result['auth'], $result['time'], $result['url'], $result['preview']);
// The skins array holds all the skins on the current page, to be passed to index.html
$skins = array();
$i = 0;
while($stmt->fetch())
{
$skins[$i] = $result;
$i++;
}
print_r($skins);
The problem is, when this executes, the $skins array contains the last result row from the query. This is the print_r of $skins:
Array
(
[0] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[1] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[2] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
)
As you can see, the last result from the query is populating all of the array entries for some reason.
Can anyone explain this behaviour and tell me what I'm doing wrong? Thanks. :)
EDIT: Here's the solution:
while($stmt->fetch())
{
foreach($result as $key=>$value)
{
$tmp[$key] = $value;
}
$skins[$i] = $tmp;
$i++;
}
Quoting the (now) first note on the mysqli::fetch manual page, emphasis mine :
the problem is that the $row returned is reference and not data.
So, when you write $array[] = $row, the $array will be filled up with the last element of the dataset.

Categories