When I do:
print_r($_COOKIE[#PATH]);
It returns a nice array:
Array ( ['threads'] => Array ( [12] => Array ( [50] => 1317830223 [1]
=> 1317785487 [14] => 1317497737 [7] => 1317488004 [9] => 1317485889
[6] => 1317294825 [5] => 1317289974 [4] => 1317288063 ) ) )
But when I do:
print_r($_COOKIE[#PATH]['threads']);
It doesn't print anything... var_dump is also returning NULL.
Whats wrong with that? First print is saying that there is an array like this, but when I try to catch it, script returning null.
Judging by the looks of your array output, your cookie array contains a key named 'threads', not threads. Those quotes are part of the key name, so somewhere you are adding extraneous quotes to the key.
Try print_r($_COOKIE[#PATH]["'threads'"]); to see what I mean.
Related
I have a multi-dimensional array and php seems to be returning an array instead of a value when I attempt to access the values directly. What am I doing to cause this?
The array looks like (via print_r):
Array (
[12] => Array ( [2016] => 93083.00 [2015] => 85367.00 [2014] => 69726.00 )
[11] => Array ( [2016] => 66730.00 [2015] => 65548.00 [2014] => 77936.00 )
[10] => Array ( [2016] => 84602.00 [2015] => 112070.00 [2014] => 102104.00 )
)
I'm trying to access values using $arrayname[12][2016] but it is returning Array[2016] instead of 93083.
Is this a simple syntax mistake? Or am I missing part of the concept here? I've been trying to work this problem for hours so maybe I'm missing a simple explanation.
EDIT: the syntax above is actually correct, the issue was in the data entry: I was trying to access a key that didn't exist. I tried to delete the post, but can't since it has been answered.
$arrayname[12] = [2016=>93083.00, 2015=> 85367.00 ]
...
...
echo $arrayname[12][2015] ; // prints 85367
i think your array has one more level. try $arrayname[12][2016][2016] .
I am making use of two arrays in one foreach loop.
Here's the code snippet that i have written for two input file type arrays.
$file[]= $_FILES['f_name']['name'];
$tmp_name[]=$_FILES['f_name']['tmp_name'];
foreach (array_combine($file, $tmp_name) as $code => $name) {
print_r($code);
print_r($name);
}
The resultant value i get on printing the array is this:
ArrayArray ( [0] => C:\xampp\tmp\phpC24D.tmp [1] => C:\xampp\tmp\phpC24E.tmp [2] => C:\xampp\tmp\phpC25F.tmp [3] => C:\xampp\tmp\phpC260.tmp [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
It prints only one array, doesn't prints the other array.
How can I get it printed?
thanks in advance.
Actually it is printing 2 arrays, just the 1st one contains nothing.
ArrayArray (
I prefer to use var_dump compared to print_r as it gives you more details for debugging.
As you are combining the 2 arrays into one and they only have 1 index there is nothing in the $code variable, just $name - unless you add array indexes.
One more beginner question:
how can I turn an array (actually it's just a text) like that:
Array
(
[0] => Array
(
[0] => nsor#cg.ukrtel.net
[1] => p2007#rambler.ru
[2] => pan20072009#yandex.ru
[3] => gf#ukr.net
[4] => tmkp#ma.odessa.ua
[5] => export#soh.by
[6] => advert#soh.by
)
)
into a .csv file by means of PHP? This file should be without things like this "[0] =>", only addresses.
Assuming your array is in a variable called $arr, you could use
echo implode("\r\n",$arr);
And you might want to prefix it with
header('Content-type: text-csv; charset="UTF-8"');
or similar.
Look at the first example at http://php.net/manual/en/function.fputcsv.php
Use your array instead of the array in the example.
I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}
Not sure how to go about this...
But, I have two arrays, one with updated information, another with outdated information... There are a lot more elements in the second array, but I'm looking to "update" the outdated one with the updated information.
Here's what the arrays look like:
//Outdated
Array (
[0] => Array
(
[anum] => 3236468462
[cid] => 4899097762
[mid] => 1104881401
[na_title] =>
[na_fname] => JOHN
[m_initial] =>
[na_lname] => DOE
[na_suffix] =>
[na_addr1] => 1234 SAMPLE AVENUE
[na_addr2] =>
[na_city] => NORWALK
[state] => OH
[zip] =>
[zip_plus_4] =>
[route] => R002
[dma_code] => 510334
)
)
//Updated
Array (
[1] => Array
(
[0] => YUD990
[1] => 98
[2] => 1234 Sample Avenue
[3] =>
[4] => Norwalk
[5] => OH
[6] => 44857-9215
[7] => 3236468462
)
)
To clarify, I want to:
(1) Match up the value for [7] from the updated array with the value for [anum] in the outdated array, and then update [na_addr1], [na_addr2], [na_city], [state], [zip], [zip_plus_4] in the outdated array with the values for [2],[3],[4],[5],[6] (I know I'll need to split the updated [6] in order to get it to map corrected to the outdated)
Feel like I'm making this very confusing... sorry about that...
Your updated array needs to have matching keys, otherwise there's no way to know how the values should replace the old ones. Then the standard array merge works.
$new = array_merge($outdated, $updated);
Assuming that the structure of the update array will never change, you could just use the code below. (I'm assuming that 'zip' is the first 5 digits and zip_plus_4 is the last 4 digits, but I'm not clear exactly what they're supposed to be.)
$old_array['anum'] = $new_array[7];
$old_array['na_addr1'] = $new_array[2];
$old_array['na_addr2'] = $new_array[2];
$old_array['na_city'] = $new_array[3];
$old_array['state'] = $new_array[4];
$zip_code = explode('-', $new_array[6]);
$old_array['zip'] = $zip_code[0];
$old_array['zip_plus_4'] = $zip_code[1];
I'm not sure why the second array doesn't use its own set of matching keys. It would be more readable and help keeps things consistent. For example, if you end up adding another field, some of the elements could be offset by one, which would just cause headaches. But if the arrays used the same keys, you could use the code below and everything would be fine.
$old_array['anum'] = $new_array['anum'];
$old_array['na_addr1'] = $new_array['na_addr1'];
(etc)