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";
Related
This is the first post from a PHP noob. My question relates to an HTML/PHP form which posts the following three types of data generated by a MySQL query:
ID (always posted via a hidden input field)
Mileage (only some entries may be completed, others might be left blank)
Vehicle Type (always posted via a hidden input field)
This data is all for one of three companies that are selected on the page preceding the data-posting page ie.
Step One: Choose company and year. Press submit.
Step Two: See results from MySQL query and enter in mileages where necessary. Press submit.
Step Three: (that's why I'm here)
I've stuffed this data into what I (probably mistakenly) believe is a multidimensional array called $idmilearray by using the following code:
if(isset($_POST['mileage'])) {
$mileagenumber = $_POST['mileage'];
}
if(isset($_POST['idnos'])) {
$idnumber = $_POST['idnos'];
}
if(isset($_POST['vehicle'])) {
$vehicletype = $_POST['vehicle'];
}
$idmilearray = array(
'ids' => $idnumber,
'mileage' => $mileagenumber,
'vtype' => $vehicletype
);
foreach($idmilearray as $inputs) {
$inputs = $idmilearray['ids'];
$inputs = $idmilearray['mileage'];
$inputs = $idmilearray['vtype'];
}
If I execute a print_r on the $idmilearray I get the following results:
Array
(
[ids] => Array
(
[0] => 35
[1] => 22
[2] => 32
[3] => 38
[4] => 36
[5] => 39
[6] => 16
[7] => 20
[8] => 48
[9] => 46
)
[mileage] => Array
(
[0] => 334
[1] => 56
[2] =>
[3] => 43
[4] =>
[5] =>
[6] =>
[7] =>
[8] => 11
[9] => 5
)
[vtype] => Array
(
[0] => 10T
[1] => 10T
[2] => 10T
[3] => Artic
[4] => 10T
[5] => Artic
[6] => Artic
[7] => 10T
[8] => Artic
[9] => 10T
)
What I would like to do is to create another array (the values of which I can use in later SQL queries) but only where the keys contain something in [ids][0], [mileage][0] and [vtype][0] and so on and so forth. I'd like to use [mileage] as the 'reference array'.
I've been playing around with array_intersect_key and array_diff_key but I've hit a big and rather a nasty wall. Can anyone help or give some pointers?
Thanks very much for your time.
Regards,
External.
You can use what you have to insert directly into mysql as follows:
foreach ( $idmilearray['mileage'] as $KEY => $VAL ) {
if ( empty($VAL) ) continue;
$query = "INSERT INTO `tableName` (id, mileage, vtype) VALUES ('{$idmilearray['ids'][$KEY]}', '$VAL', '{$idmilearray['vtype'][$KEY]}')";
}
Or if you really want to merge everything for further processing or whatever reason you can do it like so:
$NewArray = [];
foreach ( $idmilearray['mileage'] as $KEY => $VAL ) {
if ( empty($VAL) ) continue;
$NewArray[] = array('id' => $idmilearray['ids'][$KEY], 'mileage' => $VAL, 'vtype' => $idmilearray['vtype'][$KEY]);
}
Ofcourse there are many other possible ways to do this, but these are the easiest given what you currently have.
Just add another foreach, inside your existing foreach and fill your new array by checking the old array values with if (!empty($old_array)) {}
Example:
foreach($idmilearray as $inputs) {
$inputs = $idmilearray['ids'];
$inputs = $idmilearray['mileage']; // This is the one you want, right?
$inputs = $idmilearray['vtype'];
}
To do the job you need, it would be something like this:
$new_array = []; // Short array syntax (PHP 5.3+)
foreach($idmilearray as $inputs) {
$inputs = $idmilearray['ids'];
foreach($idmilearray['mileage'] as $mileage) {
if (!empty($mileage)) {
$new_array[] = $mileage;
}
}
$inputs = $idmilearray['vtype'];
}
Now you have new array ($new_array) filled only with the keys that links to existing values.
I hope it helps you.
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 ;)
After querying my data from database I have this type of array in $rown
Array
(
[0] => 60
[id] => 60
[1] => 78
[cholest] => 78
[2] => 2014-07-13
[Cdate] => 2014-07-13
)
Array
(
[0] => 61
[id] => 61
[1] => 0
[cholest] => 0
[2] => 2014-07-15
[Cdate] => 2014-07-15
)
My code
$resultn = mysql_query($sqln);
if(mysql_affected_rows() > 0)
{
while($rown = mysql_fetch_array($resultn))
{
$result_finalb = $rown["id"];
}
}
when I print $result_finalb then show only second array data, but I want to show both array data.
You are setting a single variable $result_finalb with a value.
If you want to create an array of values, using $result_finalb[] will create an array item with the value you assign.
The first time will be $result_finalb[0], second will be $result_finalb[1], and so on through however many iterations you go through.
You are missing square brackets. Use this:
$resultn = mysql_query($sqln);
if(mysql_affected_rows() > 0)
{
while($rown = mysql_fetch_array($resultn))
{
$result_finalb[] = $rown["id"];
}
}
I have an array of arrays like this:
$cart = Array (
[0] => Array ( [TypeFlag] => S [qty] => 2 [denom] => 50 [totalPrice] => 100 )
[1] => Array ( [TypeFlag] => V [qty] => 1 [denom] => 25 [totalPrice] => 25 )
[2] => Array ( [TypeFlag] => C [qty] => 1 [denom] => 25 [totalPrice] => 25 )
)
Is there any way, short of looping through all of them and checking one at a time, to determine if the TypeFlag value for any of them is S?
Try this:
foreach($cart as $key => $value) {
if ($value['TypeFlag'] == 'S') {
return $key;
}
}
This would return the key of the sub-array that has a TypeFlag value of S. However, this would stop after it finds the first sub-array that matches your search pattern. Not sure what your desired output is tho and how many results are expected. If you can provide more info, I can give you a more accurate example.
Given a function that returns the TypeFlag for each element of your array:
function get_type_flag($item) {
return $item["TypeFlag"];
}
You could apply that function to each element in the array:
$typeflags = array_map("get_type_flag", $cart);
and see if S is in that array:
if (in_array("S", $typeflags)) {
...
}
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'
)
)