Sorting data in an array by keys - php

Good day.
We have array:
array(3) {
[1]=>
array(9) {
[1]=>
string(12) "aaandroid.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(5) "Test2"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
[2]=>
array(9) {
[1]=>
string(7) "aaga.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test1"
[8]=>
string(10) "2008-02-21"
[9]=>
string(10) "2013-04-29"
}
[3]=>
array(9) {
[1]=>
string(10) "aatrakc.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test3"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
Tell me please how sort data in array with key?
For example i would like get array where data sorting on element 7, ie. in result i would like get array:
array(3) {
[1]=>
array(9) {
[1]=>
string(7) "aaga.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test1"
[8]=>
string(10) "2008-02-21"
[9]=>
string(10) "2013-04-29"
}
[1]=>
string(12) "aaandroid.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(5) "Test2"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
[3]=>
array(9) {
[1]=>
string(10) "aatrakc.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test3"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
Tell me please it really an how make it?

Check the PHP usort function: http://www.php.net/manual/en/function.usort.php.
It provides (in place) sorting based on a callback, which you can create.
Example:
usort($myArray, function ($a, $b) {
return strcmp($a[7], $b[7]);
});

Continuing Hidde's answer, if you want to reverse the order of the sort you would still use usort(), but you reverse the order of the parameters in the call to strcmp(). This will reverse which of the two arrays usort() sees as the larger value.
usort($myArray, function ($a, $b) {
return strcmp($b[7], $a[7]);
});
This uses an anonymous function, so it will only work on Php 5.3 or later. If you have to work on 5.2 define a function to use as the callback.
function mySortFunction($a, $b) {
return strcmp($b[7], $a[7]);
}
usort($myArray, 'mySortFunction');
See:
http://php.net/manual/en/function.usort.php
http://php.net/manual/en/function.strcmp.php

Related

Count values in specific position in multidimensional array

I have a multidimensional array that looks like this:
array(6) {
[0]=>
array(6) {
[0]=>
string(5) "email"
[1]=>
string(5) "vote1"
[2]=>
string(5) "vote2"
[3]=>
string(5) "vote3"
[4]=>
string(5) "vote4"
[5]=>
string(5) "vote5"
}
[1]=>
array(6) {
[0]=>
string(5) "a#a.a"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[2]=>
array(6) {
[0]=>
string(5) "b#b.b"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[3]=>
array(6) {
[0]=>
string(5) "c#c.c"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[4]=>
array(6) {
[0]=>
string(5) "d#d.d"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[5]=>
array(6) {
[0]=>
string(5) "e#e.e"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
}
I want to count how many times each value occurs at position [1] of each array.
I have tried many things including array_count_values(), but this doesn't allow the option to count occurrences in just a certain position of each array. I'm not quite sure how to go about this issue because the array is multidimensional and I have never had to deal with just counting the values at a specific position.
I apologize if the answer to this is blatantly obvious, I've been working on this issue for a few hours but to no avail, but I also am a beginner when it comes to arrays, especially multidimensional. All ideas are welcome :)
What you can do is, you can loop through the array and put the values of [1] into another array, and apply array_count_values on the resultant array.
$a = array();
foreach ($arr as $ar) {
$a[] = $ar[1];
}
print_r(array_count_values($a));
This will give you:
vote1: 1
A: 5
If the above is what you are looking for? If you want a shorter version, you can also use array_column.
use array_column($array, 1) to get position[1] elements as array; Then use your array_count_values() to count them.
<?php
$array = [[1,2,3],[2,3,4]];
var_dump(array_count_values ( array_column($array,1) ));

PHP session saving array with POST values

First sorry for my English.
I have a weird problem using the php $_SESSION Object.
I spent 2 days without find the solution.
I am triying to save a multidimensional array that stores some post values.
I create an array with this values and then i create a multidimensional array containing this arrays.
Page 1 (setvalues in multi array ans save in session)-> GET -> Page 2 read the session.
My code Page 1:
First, save the post values into a object property and validate them.
public function validateData(){
$this->nombres=$_POST["nombre"];
$this->imp_nombres=$_POST["imp_nombre"];
$this->numeros=$_POST["numero"];
$this->imp_numeros=$_POST["imp_numero"];
$this->tallas=$_POST["talla"];
$this->cantidades=$_POST["cantidad"];
$this->productos=$_POST["products"];
$this->equipos=$_POST["equipo"];
if(WSI_Funtions::compareSizes($this->nombres,$this->imp_nombres,$this->numeros,$this->imp_numeros,$this->tallas,$this->cantidades,$this->productos,$this->equipos))
{
$this->isValidModel=true;
$this->saveProductsValues();
}
else{
$this->isValidModel=false;
$this->errorMessage="Los datos no son correctos. Los parametros no coinciden";
}
}
If data is ok, I save those values:
public function saveProductsValues()
{
$this->productsValues=array();
$this->productsValues["names"]=$this->nombres;
$this->productsValues["imp_nombres"]=$this->imp_nombres;
$this->productsValues["numeros"]=$this->numeros;
$this->productsValues["imp_numeros"]=$this->imp_numeros;
$this->productsValues["tallas"]=$this->tallas;
$this->productsValues["cantidades"]=$this->cantidades;
$this->productsValues["productos"]=$this->productos;
$this->productsValues["equipos"]=$this->equipos;
}
Then I save it in a session :
public function saveSessionValues()
{
$_SESSION['customer'] = $this->customerObject;
$_SESSION['productsValues'] =$this->productsValues;
echo var_dump($_SESSION['productsValues']);
}
The saveSessionValues echo print this:
array(8) { ["names"]=> array(12) { [0]=> string(0) "" [1]=> string(12) "ÁNGEL HDEZ." [2]=> string(11) "VUJASINOVIC" [3]=> string(4) "ABIA" [4]=> string(10) "MUTAKABBIR" [5]=> string(8) "PETROVIC" [6]=> string(5) "DOBOS" [7]=> string(4) "HOMS" [8]=> string(6) "CASTRO" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["imp_nombres"]=> array(12) { [0]=> string(0) "" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["numeros"]=> array(12) { [0]=> string(1) "7" [1]=> string(1) "8" [2]=> string(1) "9" [3]=> string(0) "" [4]=> string(2) "11" [5]=> string(2) "12" [6]=> string(2) "18" [7]=> string(2) "19" [8]=> string(2) "22" [9]=> string(1) "5" [10]=> string(2) "33" [11]=> string(0) "" } ["imp_numeros"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(0) "" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(0) "" } ["tallas"]=> array(12) { [0]=> string(4) "XXXL" [1]=> string(3) "XXL" [2]=> string(2) "XL" [3]=> string(3) "XXL" [4]=> string(3) "XXL" [5]=> string(3) "XXL" [6]=> string(4) "XXXL" [7]=> string(3) "XXL" [8]=> string(2) "XL" [9]=> string(4) "XXXL" [10]=> string(2) "XL" [11]=> string(3) "XXL" } ["cantidades"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(3) "145" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(1) "1" } ["productos"]=> array(12) { [0]=> string(3) "109" [1]=> string(3) "109" [2]=> string(3) "109" [3]=> string(3) "109" [4]=> string(3) "109" [5]=> string(3) "109" [6]=> string(3) "109" [7]=> string(3) "109" [8]=> string(3) "109" [9]=> string(3) "109" [10]=> string(3) "109" [11]=> string(3) "109" } ["equipos"]=> array(12) { [0]=> string(7) "LEB ORO" [1]=> string(7) "LEB ORO" [2]=> string(7) "LEB ORO" [3]=> string(7) "LEB ORO" [4]=> string(7) "LEB ORO" [5]=> string(7) "LEB ORO" [6]=> string(12) "ES TALLA 4XL" [7]=> string(7) "LEB ORO" [8]=> string(7) "LEB ORO" [9]=> string(12) "ES TALLA 4XL" [10]=> string(7) "LEB ORO" [11]=> string(7) "LEB ORO" } }
My code Page 2 (php tags ommitted):
if (!isset($_SESSION)) { session_start(); }
echo var_dump($_SESSION['productsValues']);
This echo print the next value:
array(8) { ["names"]=> NULL ["imp_nombres"]=> NULL ["numeros"]=> NULL ["imp_numeros"]=> NULL ["tallas"]=> NULL ["cantidades"]=> NULL ["productos"]=> NULL ["equipos"]=> NULL }
The first level array exists, because the subarrays keys are printed, but all the second level arrays are NULL..
Maybe is for use the $_POST value??
I had try to encode the values of the $_POST object, saving only a json string instead of saving a object with the same result, the first nodes in the JSON are the keys of the arrays but the values are "NULL"
Any help please??
Thanks!!
You are over writing the values. Try with -
$_SESSION['productsValues'][] = $this->productsValues;

Reading PHP array

I thought to access the value of '1' here, I would need to the following
$data['category'][0];
["category"]=> array(1) { [0]=> string(1) "1" }
But it doesn't return anything. How would I access the string(1) section to get the value 1?
Here is the full var dump of the variable $data array
array(18) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_id_48"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_ft_48"]=> string(4) "none" ["revision_post"]=> array(26) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["author_id"]=> string(1) "1" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["title"]=> string(3) "asd" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["category"]=> array(1) { [0]=> string(1) "1" } ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_id_48_hidden_file"]=> string(0) "" ["field_id_48_hidden_dir"]=> string(1) "1" ["field_id_48"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_ft_48"]=> string(4) "none" ["allow_comments"]=> string(1) "y" ["entry_date"]=> string(10) "1380114180" ["status"]=> string(4) "open" ["previous_status"]=> bool(false) } }
To access that data you have to use $data['revision_post']['category'][0] because category array is in revision_post array.
The value of $data['category'][0] is an array. So you would access like this:
$data['category'][0] = array('1');
echo $data['category'][0][0];
Try it for yourself by copying and pasting here http://writecodeonline.com/php/

how do you insert associative array to an oracle table in php

I have this array called lines. When I do var_dump($lines), I get this result back:
array(3) {
[0]=>
array(21) {
[0]=>
string(1) "3"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49992904"
[4]=>
string(6) "283699"
[5]=>
string(1) "9"
[6]=>
string(3) "610"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1142"
[12]=>
string(5) "61219"
[13]=>
string(5) "21873"
[14]=>
string(1) "6"
[15]=>
string(1) "2"
[16]=>
string(2) "91"
[17]=>
string(1) "1"
[18]=>
string(4) "1.90"
[19]=>
string(4) "11.9"
[20]=>
string(8) "15:03:46"
}
[1]=>
array(21) {
[0]=>
string(1) "3"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49986779"
[4]=>
string(6) "291340"
[5]=>
string(2) "11"
[6]=>
string(3) "667"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1286"
[12]=>
string(5) "34464"
[13]=>
string(5) "21778"
[14]=>
string(1) "4"
[15]=>
string(1) "2"
[16]=>
string(2) "93"
[17]=>
string(1) "1"
[18]=>
string(4) "1.28"
[19]=>
string(3) "8.0"
[20]=>
string(8) "15:04:16"
}
[2]=>
array(21) {
[0]=>
string(1) "6"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49986826"
[4]=>
string(6) "292415"
[5]=>
string(2) "10"
[6]=>
string(3) "622"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1226"
[12]=>
string(5) "31334"
[13]=>
string(5) "20273"
[14]=>
string(1) "4"
[15]=>
string(1) "1"
[16]=>
string(2) "94"
[17]=>
string(1) "1"
[18]=>
string(4) "1.21"
[19]=>
string(3) "7.5"
[20]=>
string(8) "15:04:46"
}
}
I need to append today's date to each of the 20th elements in each array (need to make it date and time)
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$today=$now->format('Y-m-d');
I tried this:
foreach ($lines as $key => $value){
$lines[$key][20]=$today . ' ' . $lines[$key][20];
echo $lines[$key][20];
}
looks like it worked.
2 . I need to insert this into an oracle db. I was curious to see if there is already a method or function to insert each array within lines array into the table.
In order to serialize an array for insertion into a database, one would either use json_encode(), or serialize(); I personally like json_encode(), as it is easier to read and modify.
Both of these functions convert an array or the like into an encoded string, which you can then either insert into a column for text-based data.
To do the actual action of insertion.. that actually depends upon the version of the db you are using, because the following code only works for 11g, 10g, 9i and 8i.
Note the following isn't exactly real code, and you will have to adapt it to suit your needs, following the documentation.
<?php
$str = json_encode($lines); //convert to string. You should have properly escaped the data prior to this/ran it through some function to escape the data. We don't want injection attacks to occur
$conn = oci_connect('username', 'password', 'localhost'); //connect to database
$stmt = oci_parse($conn, "insert into tablename ('column')
values('$str')"); // construct statement
oci_execute($stmt, OCI_DEFAULT); // statement executed/data inserted
?>

I need to get NID from the array

array(1) {
[0] => object(stdClass)#53 (14)
{
["cart_item_id"]=> string(2) "64"
["cart_id"]=> string(1) "1"
["nid"]=> string(3) "204"
["qty"]=> string(1) "1"
"changed"]=> string(10) "1340948878"
["data"]=> array(3)
{
["shippable"]=> string(1) "1"
["restrict_qty"]=> string(1) "0"
["module"]=> string(10) "uc_product"
}
["title"]=> string(5) "songs"
["vid"]=> string(3) "204"
["cost"]=> string(9) "123.00000"
["price"]=> string(9) "123.00000"
["weight"]=> string(1) "0"
["weight_units"]=> string(2) "lb"
["module"]=> string(10) "uc_product"
["model"]=> string(1) "1"
}
}
Please help how do get only nid value in new variable.
get via
$output = $array[0]->nid
access object by ->
access array by [index]

Categories