When user submits form the data is stored in array & serialized & stored in the database, when user again submit another form then that data is stored in array & previous serialized array is fetched from database & is unserialized.
Now i want both these array to be multi dimensional, here is what I've tried
$post = array();
$post[] = $co_name = test_input($_POST['co_name1']);
this array is fetched from database
$db = unserialize($db);
$db[] = $post;
print_r($db);
After Printing this is what i get
Array
(
[0] => company_name
[1] => country
[2] => city
[3] => state
[4] => pincode
[5] => 2008
[6] => 01
[7] => 2008
[8] => Array
(
[0] => company_name
[1] => country
[2] => city
[3] => state
[4] => pincode
[5] => 2008
[6] => 01
[7] => 2008
)
)
Now my problem is second array is assigned to 8, how to perfectly create multi dimension array
My desired output is my array should like this
array(
0=>array(
0=>company_name
1=>country
),
1=>array(
0=>company_name
1=>location
)
)
The following will give you a numerically indexed array with your POST data as one value and DB data as another value of a new array. If you var_dump / print_r() this, the output will look similar to your desired output:
$newArray = array($post, $db);
However; Your desired output shows a reduced number of keys for each result:
company_name
country
...(or did you only write these to make it easier to read?)
If you do want just those two keys, consider using PHP's array_filter function which takes your combined array (above: $newArray) and a callback function as arguments. This allows you to manipulate any of the keys and values of the input array, to make the returned array look exactly as you like.
Just pass the info with the correct array number example
$post = array();
$post[0] = $co_name = test_input($_POST['co_name1']);
$post[1] = $co_name = test_input($_POST['co_name1']);
You won't get an answer without more clarification. This problem is likely due to faulty serialization.
How are you getting your data out of the db?
What does the data in the db look like?
Related
I am trying to save a Form Data in Mysql. But nested array is causing problem.
Array ( [name] => Custom Project
[number] => 08883
[key] => Array ( [0] => Server Cost
[1] => Domain Cost
[2] => Design Charges,
)
[value] => Array ( [0] => 098
[1] => 765
[2] => 787
)
)
I am using $this->db->insert('invoice',$array) and it is inserting data fine without the nested array.
So is there a way I can separate this nested array from above array and then save this nested array into another table having only two columns key and value
You could just use array_combine to create the key pair value:
function save_value($array)
{
$key_value = array_combine($array['key'], $array['value']);
$this->db->insert('table_name', $key_value);
}
I used the very simple approach, I pushed one array to another because the result was to be processed in Codeigniter insert method.
//Item description
$description=$_POST['key'];
//item amount
$amount=$_POST['value'];
$big_array=array();
for($i=0; $i<sizeof($description); $i++){
$small_array=array('description'=>$description[$i],'amount'=>$amount[$i]);
array_push($big_array,$small_array);
}
//adding data into model
$this->my_Model->InsertData($big_array);
I have an array that is put together. It's basically 24 data points across 7 stations. Each station has a data point each hour and a new sub0array is created for each data point. The problem with this, is I now want to split the data up based on station so I can sort it. When I go to do that array_filter doesn't work for me. I wanted to basically combine all of the values that have the same row 0 result in each sub-array. but keep each data point based on them. Row 0 is the station name. Here is an example of what the array looks like. There are 160 sub arrays. All I need to do is combine them together based on Station name. I was going to do this based on Array value, but the problem is if a data-point is missing, it breaks that logic.
Array
(
[0] => Array
(
[0] => STATIONONE
[1] => 02/22/15 04:00:00 PM
[2] => SW
[3] => Array
(
[0] => 4.51
)
[4] => MPH
[5] => Array
(
[0] => 16.1
)
[6] => MPH
)
[1] => Array
(
[0] => STATIONONE
[1] => 02/22/15 05:00:00 PM
[2] => S
[3] => Array
(
[0] => 2.7
)
[4] => MPH
[5] => Array
(
[0] => 9.61
)
[6] => MPH
)
And it just keeps repeating till I have 23 Values. Then the next station.
So 0-22 subarray value = station 1
23-45 subarray value = station 2
etc.. just stating to help show what I am trying to do.
It isn't entirely clear what you expect your output to be, but this might get you there:
$entriesByStation = []
foreach ($entries as $entry) {
$entriesByStation[$entry[0]] = $entry;
}
This will group all the the entries by station name in the $entriesByStation array
The accepted answer got me close into figuring this out. I had to do this by station though. So I just created 7 foreach statements and created an array for each station. This way I can also merge the array together if needed.
# Parse our All Data and set station
foreach ($array as $entry) {
if ($entry[0] === "STATIONONE"){
$S1pkwsite = ($entry[0]);
$S1pkwvalue = ($entry[5][0]);
$S1pkwdate = ($entry[1]);
$S1pkwunit = ($entry[6]);
$S1wspvalue = ($entry[3][0]);
$S1wspunit = ($entry[4]);
$S1wdrvalue = ($entry[2]);
$S1pkwdataarray[] = array('SiteName'=>$S1pkwsite,'DATE'=>$S1pkwdate,'WDR'=>$S1wdrvalue,'VALUE'=>$S1pkwvalue,'UNIT'=>$S1pkwunit);
}
}
array_push ($Stationone_data, $pkwdataarray);
$Stationone_data = array_shift($Stationone_data);
Hello guys I just need a little help here about unserializing MySQL values. What I want is after getting the serialized value from my database I will assign the unserialized value in a variables.
Here's my code
//THIS IS THE QUERY
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_order_data.data as data_serialize FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE type = 'I'";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = $row1['data_serialize'];
}
unserialize($var_data); //How can i get the selected value
print_r($var_data);
OUTPUT:
Array
(
[0] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
[1] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
[2] => a:2:{s:6:"points";i:294;s:4:"cost";i:294;}
[3] => a:2:{s:6:"points";d:107;s:4:"cost";d:107;}
[4] => a:2:{s:6:"points";i:163;s:4:"cost";i:163;}
[5] => a:2:{s:6:"points";i:322;s:4:"cost";i:322;}
[6] => a:2:{s:6:"points";i:289;s:4:"cost";i:289;}
[7] => a:2:{s:6:"points";i:9;s:4:"cost";i:9;}
[8] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[9] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[10] => a:2:{s:6:"points";i:301;s:4:"cost";i:301;}
[11] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[12] => a:2:{s:6:"points";i:490;s:4:"cost";i:490;}
[13] => a:2:{s:6:"points";i:103;s:4:"cost";i:103;}
For example I want to get the "points" and assign in a variable $points.
How can i do that? An also the cost. Ok that's all Thanks in advance.
In your loop you want to call that unserialize function in there:
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = unserialize($row1['data_serialize']);
}
Then you'll wind up with a multidimensional array. Right now you're winding up with an array of serialized values.
As far as I know, the only way to extract these values (which were serialize()d by PHP, not MYSQL) is to use PHP's unserialize() function (docs here)
I want to use jQuery UI Autocomplet to make a form field autocomplete grabbing the values from a database:
http://jqueryui.com/demos/autocomplete/#remote
I have copied the code accross, but the example (birds) is an associative array.
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus");
etc etc
I want to query my database taking two fields, id and author.
However the query returns an multidimensional array, where each returned row from the database is an array.
e.g. ( [0] => Array ( [ID] => 1 [Author] => Higgins ) ) ( [0] => Array ( [ID] => 2 [Author] => Darl) )( [0] => Array ( [ID] => 3 [Author] => Lewis) )
How can I return the query so it is in the format:
"1=>Higgins,
2=>Darl,
3=>etc etc,"
so that i can use the script?
Given that your database array is $dbresult, you can do it like this:
foreach($dbresult as &$arr) {
$completearray[$arr['ID']] = $arr['Author'];
}
var_dump($completearray);
above will output the following array:
1=>Higgins
2=>Darl
3=>Lewis
UPDATE: I've updated the code above so the resulting array is indexed by the ID field.
Firstly, YES, this is a duplicate issue that's been asked 100 times on here, and I've read through many of the posts, but the examples given aren't working 100% for me and I can't figure out where I'm going wrong.
I want to sort an array like this by the 'distance' key, and I want the sort to be "expanded" across the entire array so it reorders all keys in the array. In essence, just like you'd get if you sorted a column in a spreadsheet and expanded the sort across all columns.
Here's the sample array:
$locations = array (
[phone] => Array (
[0] => 555-123-1234
[1] => 555-456-4321
[2] => 555-789-1122
[3] => 555-321-1111 )
[address] => Array (
[0] => 123 Main Street BigCity, NY 12345
[1] => 456 Maple Street Somewhere, UT 87654
[2] => 999 1st Ave MyTown, VA 23456
[3] => 321 2nd Ave MyTown, VA 23456 )
[distance] => Array (
[0] => 24.56
[1] => 13.05
[2] => 23.99
[3] => 1.75 )
[name] => Array (
[0] => First Location
[1] => Second Location
[2] => Third Location
[3] => Fourth Location )
)
I'm trying to sort the array by 'distance', and I've tried using both the array_multisort() function and the usort() function ...but only "multisort" gives me nearly usable results by sorting the distances only and not sorting the entire array according to distance.
My function:
array_multisort($locations['distance'], SORT_NUMERIC);
I then run a "for" loop to traverse the array and spit out the values by key. The result is the distances are ordered but the Location names, addresses, etc. don't get reordered.
What am I missing here??
Thanks!!
Your array is arranged in columns, whereas array_multisort() when used in this way expects it to be arranged in rows.
It should be in the structure that you would get if you did:
$rows = array();
while ($row = $result->fetch_assoc()) $rows[] = $row;
...with a MySQLi result object.
You can reformat your existing array into this structure quite easily:
$locationsAsRows = array();
for ($i = 0; isset($locations['phone'][$i]); $i++) {
$locationsAsRows[$i] = array (
'phone' => $locations['phone'][$i],
'address' => $locations['address'][$i],
'distance' => $locations['distance'][$i],
'name' => $locations['name'][$i]
);
}
var_dump($locationsAsRows);
Now, when you do:
array_multisort($locations['distance'], SORT_NUMERIC, $locationsAsRows);
var_dump($locationsAsRows);
...you can see that the data has been sorted. Note that not only did I pass the column data you wanted to sort by, I also passed the whole "row formatted" $locationsAsRows array as the last argument. This is passed by reference, so $locationsAsRows itself will be the sorted data after you have called array_multisort().
Now let's say that two of the distance values are the same - we need to supply a second priority search column. So next, we could sort by address, and we can do that like so:
array_multisort($locations['distance'], SORT_NUMERIC, $locations['address'], SORT_ASC, $locationsAsRows);
See it working and have a play around with it...
array_multisort($locations['distance'],$locations['name'],$locations['address'],$locations['phone'],SORT_NUMERIC);
Maybe this will do the job?