My Question is, i got an array with mix keys (i mean numeric and string);
This is my code sample,
Array
(
[_id] => 1, [month] => 052014, [studId] => STU140528155358,
[1] => 'p', [2] => , [3] => ,[4] => ,[12] => 'a'
)
Now I try to
print_r($array[3]);
It gives error undefined offset 3
SAMPLE:-
Run This:
$a = array("name"=>"Nishchit",[1]=>"Dhanani");
print_r($a[1]);
This will work-
$a = array("name"=>"Nishchit",1=>"Dhanani");
print_r($a[1]);
You have done a mistake of putting [1] instead of just 1 as key.
And if you want a nested array, you can do this -
$a = array("name"=>"Nishchit",[1=>"Dhanani"]);
print_r($a[0]);
print_r($a[0][1]);
$a = array(
'_id' => 1,
'month' => 052014,
'studId' => STU140528155358,
1 => 'p',
12 => 'a'
);
echo "<pre>";
print_r($a[1]);
Output -
p
$b = array(
'_id' => 1,
'month' => 052014,
'studId' => STU140528155358,
1 => 'p',
2 => '',
12 => 'a'
);
echo "<pre>";
print_r($b[12]);
Output -
a
Related
I want the alphanumeric array below to return the max element depending on the value in the array which is h5-19: I tried using max($array), but that returns h5-9.
Array
(
[3] => h5-1
[4] => h5-2
[2] => h5-3
[1] => h5-4
[0] => h5-6
[5] => h5-7
[6] => h5-8
[7] => h5-9
[8] => h5-10
[9] => h5-11
[10] => h5-13
[11] => h5-15
[12] => h5-19
)
Thanks in advance.
You can use usort() sort array based on second digit of items. In sort function use explode() to get target digit.
$originArr = [
"h5-01",
"h6-1",
"h5-2",
"h5-3",
"h5-7",
"h5-9",
"h5-11",
"h5-15",
"h5-19"
];
// Copy array to keep order of origin array
$arr = $originArr;
usort($arr, function($a, $b){
return explode('-', $b)[1] - explode('-', $a)[1];
});
echo $arr[0];
// h5-19
Check result in demo
<?php
$set = array();
$data = array(
'3' => 'h5-1',
'4' => 'h5-2',
'2' => 'h5-3',
'1' => 'h5-4',
'0' => 'h5-6',
'5' => 'h5-7',
'6' => 'h5-8',
'7' => 'h5-9',
'8' => 'h5-10',
'9' => 'h5-11',
'10' => 'h5-13',
'11' => 'h5-15',
'12' => 'h5-19',
'789' => 'h1-8',
'123' => 'p-78',
'3000' => 'p-8',
);
foreach($data as $each_element){
$each_val = explode("-",$each_element);
if(!isset($set[$each_val[0]])){
$set[$each_val[0]] = intval($each_val[1]);
}
$set[$each_val[0]] = max(intval($each_val[1]),$set[$each_val[0]]);
}
print_r($set);
Output:
Array
(
[h5] => 19
[h1] => 8
[p] => 78
)
Algorithm:
Create a set of tags which will be an associative array where key will be the tag name and it's value will be the max value of that tag present in your array.
If the key is already set, get the max between the value in the key(tag) and the new value which also belongs to the same tag. This way you have max for each tag.
This sorts the data use rsort() and SORT_NATURAL and then echos the first item...
$data = Array
(
"3" => "h5-1",
"4" => "h5-2",
"2" => "h5-3",
"1" => "h5-4",
"11" => "h5-15",
"12" => "h5-19"
);
rsort($data, SORT_NATURAL);
echo $data[0];
which gives...
h5-19
Array
(
['data'] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
['id'] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
This data ($form_data) coming from form. How to accessing this array? I cannot access with following:
$data= $form_data['data'][0]; or
$id = $form_data['id'][0];
I just accessing with array_values() function and following:
$data= $form_data[0][0]; or
$id = $form_data[0][0];
But i dont want use array_values() function. Why I cant access my array natural way?
This works fine man, make sure you are building your array correctly. This code works flawlessly. There's not much information on how you built the array, so I hope this model helps you.
<?php
$array = array(
'data' => array
(
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd'
),
'id' => array
(
0 => 5,
1 => 6,
2 => 7,
3 => 8
)
);
Now you can call back on your array using your preferred method:
$a = $array['data'][0];
$b = $array['data'][1];
$c = $array['data'][2];
echo $a . $b . $c;
// outputs 'abc'
Also call the id:
$fiv = $array['id'][0];
$six = $array['id'][1];
$sev = $array['id'][2];
echo $fiv . $six . $sev;
// outputs '567'
I've an array.
Array
(
[initial] => MSS
[hour] => 5.2
[row_checker_1] => 1
[project_name_1] => KGD001
[project_shortcode_1] => KGD001
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
[remarks_1] => on going
[task_id] => 76
[row_checker_2] => 2
[project_name_2] => DG001
[project_shortcode_2] => DG001
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
[remarks_2] => on going
)
Now I want to split all element upper range key is "project_shortcode_1" and lower range key is remarks_1.
So, new array should look like:
array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
Use array_filter with flag ARRAY_FILTER_USE_KEY for using the array keys, and do the comparison with the logic needed to get the desired keys. It works from PHP 5.6.
$arr = array ( "initial" => "MSS",
"hour" => 5.2,
"row_checker_1" => 1,
"project_name_1" => "KGD001",
"project_shortcode_1" => "KGD001",
"5_1" => 23,
"6_1" => 3.3,
"4_1" => 23.2,
"remarks_1" => "on going",
"task_id" => 76,
"row_checker_2" => 2,
"project_name_2" => "DG001",
"project_shortcode_2" => "DG001",
"5_2" => 1.1,
"6_2" => 2.2,
"4_2" => 3.1,
"remarks_2" => "on going",
);
// PHP > 5.6
$result = array_filter($arr, function($k){
$var = explode('_', $k);
return is_numeric($var[0]) && $var[1]==1;
}, ARRAY_FILTER_USE_KEY);
If what you need is a multidimensional array with all the ranges NUMBER_N, then use something like this (extended from Dmitriy Demir answer):
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
function splitRange($a){
$newArray = array();
foreach ($a as $k => $v) {
$rightFormat = preg_match('/^\d+_(\d+)$/', $k, $index);
if ($rightFormat)
$newArray[$index[1]][$k] = $v;
}
return $newArray;
}
print_r(splitRange($myArray));
The result will be something like:
Array
(
[1] => Array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
[2] => Array
(
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
)
)
being N from NUMBER_N the index of the array.
Since you mentioned in the comments that you'd prefer to get all values that are in format NUMBER_1 I think you'd need to loop through your array and check the value names with regex, then add the values to a new array if they meet the criteria. Here's how I would do this:
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2a' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
$newArray = array();
foreach ($myArray as $k => $v) {
$rightFormat = preg_match('/^\d+_\d+$/', $k);
if ($rightFormat)
$newArray[$k] = $v;
}
print_r($newArray);
The result of print_r in that case would be:
Array ( [5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] =>
2.2 [4_2] => 3.1 )
If the number after the underscore should always be 1 then change the regex from /^\d+_\d+$/ to /^\d+_1$/.
You can play around and see how regex works here.
PS: I've set all values to strings out of convenience. Feel free to modify that.
A regex-based solution seems fitting for this question.
preg_grep() is a function designed to apply a regex filter upon each value in an array. I little more tweaking is necessary for this case because the keys must be filtered instead.
The One-liner:
$output=array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))));
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
Here is the step-by-step array manipulation...
array_keys($input); // create array with input keys as values
/* array (
0 => 'initial',
1 => 'hour',
2 => 'row_checker_1',
3 => 'project_name_1',
4 => 'project_shortcode_1',
5 => '5_1',
6 => '6_1',
7 => '4_1',
8 => 'remarks_1',
9 => 'task_id',
10 => 'row_checker_2',
11 => 'project_name_2',
12 => 'project_shortcode_2',
13 => '5_2',
14 => '6_2',
15 => '4_2',
16 => 'remarks_2',
) */
preg_grep("/^\d+_1$/",array_keys($input)); // filter the input array using regex pattern
/* array (
5 => '5_1',
6 => '6_1',
7 => '4_1',
) */
array_flip(preg_grep("/^\d+_1$/",array_keys($input))); // flip the filtered array
/* array (
'5_1' => 5,
'6_1' => 6,
'4_1' => 7,
)*/
array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))); // filter input by comparing keys against filtered array
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
In the following array, which I have stored in $_SESSION['doc_brick_array'], I am trying to find the element with brick0. I want to delete this element, and then re-index the outer array. How can I do this?
Array
(
[0] => Array
(
[brick0] => Array
(
[city_name] => Lahore
[clinic_name] => shifa hospital
[attendant_name] => ali
[drd1_cell1] => 03017666454
[mbv] => 666
[brick_name] => LHR-0002
[clinic_address] => i-8 markaz
[drd1_phone] => 9798797
[drd1_cell2] => 04037777888
[drd1_email] => abc#yahoo.com
[visit_time] => m
)
)
[1] => Array
(
[brick1] => Array
(
[city_name] => Rawalpindi
[clinic_name] => aljanat hospital
[attendant_name] => kanzal
[drd1_cell1] => 03014544567
[mbv] => 6000
[brick_name] =>
[clinic_address] => i-9 markaz
[drd1_phone] => 07337837
[drd1_cell2] => 03017767575
[drd1_email] => abcd#yahoo.com
[visit_time] => m
)
)
)
The code I have tried
for($g=0; $g<=count($_SESSION['doc_brick_array']); $g++){
if (($key = array_search($brick_code, $_SESSION['doc_brick_array'][$g])) !== false) {
unset($_SESSION['doc_brick_array'][$key]);
$_SESSION['doc_brick_array'] = array_values($_SESSION['doc_brick_array']);
}
}
I think what you are after here is array_values() which, from the PHP.net site
array_values() returns all the values from the array and indexes the
array numerically.
As an example:
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will produce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/
$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
You can use array_filter to remove any elements from $_SESSION['doc_brick_array'] that have a brick0 key.
$x = array_filter($_SESSION['doc_brick_array']), function($y) {
return !isset($y['brick0']);
});
Then use array_values to reindex the result.
$_SESSION['doc_brick_array']) = array_values($x);
I have 1 array that has the right values that I need but it is out of order. I then have another array with the same keys and it is in the right order but the values are not what I need.
Here is my first array with the correct values but is out of order:
Array
(
[countTotal] => 7268
[zip] =>
[yearName] =>
[countZipRadius] =>
[Acura] => 1334
[Cadillac] => 511
[Ford] => 5423
)
Here is my second array with the right order but the wrong values:
Array
(
[countZipRadius] => 0
[zip] => 1
[yearName] => 2
[Acura] => 3
[Cadillac] => 4
[Ford] => 5
[countTotal] => 6
)
I am trying to figure out a way to create a new array with the right values from array 1 but that is in the order of array 2.
I have been playing with it for awhile and cannot seem to get it.
Any help would be great.
Thanks!
$c = array();
foreach (array_keys($b) as $k) {
$c[k] = $a[k];
}
You could use php's array_multisort function:
$original = array(
'countTotal' => 7268,
'zip' => '',
'yearName' => '',
'countZipRadius' => '',
'Acura' => 1334,
'Cadillac' => 511,
'Ford' => 5423,
);
$right = array(
'countZipRadius' => 0,
'zip' => 1,
'yearName' => 2,
'Acura' => 3,
'Cadilac' => 4,
'Ford' => 5,
'countTotal' => 6
);
//make sure both arrays are in the same order
ksort($original);
ksort($right);
array_multisort($right, $original);
print_r($original);
When you give it two arrays with the same number of elements it sorts both arrays, based on the order of the first array - in this case the 0, 1, 2, 3, etc. values in $right
Create a New Array (Array C)
Use a FOR loop to go through Array B
For each value in Array B, get the value with the same key from Array A and set Array C append those values to Array C. This will put them in the correct order in C.
Using scones' method:
$original = array(
'countTotal' => 7268,
'zip' => '',
'yearName' => '',
'countZipRadius' => '',
'Acura' => 1334,
'Cadillac' => 511,
'Ford' => 5423,
);
$right = array(
'countZipRadius' => 0,
'zip' => 1,
'yearName' => 2,
'Acura' => 3,
'Cadilac' => 4,
'Ford' => 5,
'countTotal' => 6
);
foreach ($right as $key => $value) {
$new[$key] = $original[$key];
}
print_r($new);
$array = array('a' => 100, 'b' => '5');
$newArray = array_combine(array_keys($array), range(0, count($array) - 1));
var_dump($newArray);