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'
Related
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 want to loop through an array and get each element to pass as parameters to a constructor.
here is my example
[user_by_province] => Array
(
[label] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
[count] => Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
[4] => 7
[5] => 1
)
)
Here is the constructor:
$pc = new C_PhpChartX(array(array('a','2'),
array('b','1'),
array('c','1'),
array('d','1'),
array('e','7'),
array('f','1')));
so how can i do that with php thanks for any help, may we can do that with array_map or no? thanks for any help
As you suggested array_map with multiple arguments could be a good solution.
<?php
$userByProvince = array(
'label' => array('a', 'b', 'c', 'd', 'e', 'f'),
'count' => array('1', '2', '3', '4', '5', '6'),
);
function combine($arg1, $arg2)
{
return array($arg1, $arg2);
}
$arguments = array_map('combine', $userByProvince['label'], $userByProvince['count']);
$pc = new C_PhpChartX($arguments);
If you are using PHP 5.3 you can even substitute the function with a lambda expression to make the code more compact (see docs).
It's easier to use array_combine.
you can do something like this:
$my_array=array("user_by_province"=>array("label"=>array('a','b','c','d','e','f'),"count"=>array(2,1,1,1,7,1)));
$new_array=array();
for($i=0;$i< count($my_array['user_by_province']['label']);$i++){
$new_values=array();
array_push($new_values,$my_array['user_by_province']['label'][$i],$my_array['user_by_province']['count'][$i]);
array_push($new_array,$new_values);
}
supposed your given array is ordered and has the same amount of data, i.e. pairs:
loop over your data:
$input = array("user_by_province"
=> array("label" => array('a', 'b', 'c'),
"count" => array(1, 2, 3)));
$combined = array();
for($i = 0; $i < count($input['user_by_province']['label'] && $i < count($input['user_by_province']['count']; $i++)
{
$combined[] = array($input['user_by_province']['label'][$i], $input['user_by_province']['count'][$i]);
}
$pc = new C_PhpChartX($combined);
I walk around here with some hesitation, I have passed an array with sub elements (so to speak) and I need three random values but these are obtained without repeating.
The array is as follows:
Array
(
[0] => Array
(
[uid] => 1
[ticket_code] => 0oreb8yo
)
[1] => Array
(
[uid] => 1
[ticket_code] => 2oeii8hm
)
[2] => Array
(
[uid] => 1
[ticket_code] => m0dwtjiw
)
[3] => Array
(
[uid] => 1
[ticket_code] => q6c7cymb
)
[4] => Array
(
[uid] => 1
[ticket_code] => zyqhm5bj
)
[5] => Array
(
[uid] => 1
[ticket_code] => amdqzjpi
)
[6] => Array
(
[uid] => 2
[ticket_code] => tzql7l42
)
[7] => Array
(
[uid] => 2
[ticket_code] => gap0r6vf
)
[8] => Array
(
[uid] => 2
[ticket_code] => ypqum5yz
)
[9] => Array
(
[uid] => 4
[ticket_code] => smupluac
)
[10] => Array
(
[uid] => 4
[ticket_code] => 9d8jsha7
)
[11] => Array
(
[uid] => 5
[ticket_code] => 6hdnja42
)
)
And I need you to get 3 "ticket_code" but no right to repeat the "uid".
I've been on trying as follows, but also repeats the "uid".
$ticketsWinners = array();
for ($i=0; $i < 3; $i++) {
$aux = array_rand($allTickets);
$aux2 = $allTickets[$aux]['uid'];
$ticketsWinners[] = array(
'uid' => $aux2,
'ticket_code' => $allTickets[$aux]['ticket_code']
);
}
Any way to do it without repeats?
We thank you in advance if anyone knows of something ^^
Try something like:
$ticketsWinners = array();
while (sizeof($ticketsWinners) < 3) {
$aux = array_rand($allTickets);
// array_rand return array of keys so you need first value only
$uid = $allTickets[$aux[0]]['uid']
// add uid as a key so ass not tot check all $allTickets values
if (!isset($ticketsWinners[$uid]))
$ticketsWinners[$uid] = $allTickets[$aux[0]];
}
// if you need $allTickets back to numeric keys [0, 1, 2]
$allTickets = array_values($allTickets);
if you're afraid of infinite loops (that can take place really) then try this:
$ticketsWinners = array();
// shuffle array before checking
shuffle($allTickets);
foreach ($allTickets as $tick_data) {
$uid = $tick_data['uid'];
if (!isset($ticketsWinners[$uid]))
$ticketsWinners[$uid] = $tick_data;
if (sizeof($ticketsWinners) == 3)
break;
}
Here in worst case you check $allTickets array and get winners of size <= 3.
Try this:
$ticketsWinners = array();
$ticketUid = array();
for ($i=0; $i < 3; $i++) {
$aux = array_rand($allTickets);
$aux2 = $allTickets[$aux]['uid'];
if(! in_array($aux2, $ticketUid)) {
$ticketUid[$i] = $aux2;
$ticketsWinners[] = array(
'uid' => $aux2,
'ticket_code' => $allTickets[$aux]['ticket_code']
);
} else {
$i--;
}
}
this structure would be better ( added benefit of ticket numbers being unique )
$tickets = Array
(
'0oreb8yo' => 1,
'2oeii8hm' => 1,
'm0dwtjiw' => 1,
'q6c7cymb' => 1,
'zyqhm5bj' => 1,
'amdqzjpi' => 1,
'tzql7l42' => 2,
'gap0r6vf' => 2,
'ypqum5yz' => 2,
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
);
$winners = array();
$picks = 3;
for($i = 0; $i < $picks; $i++){
if(count($tickets) == 0 ){
break; //or error -- shouldn't need this unless picks exceed uids
}
$ticket = array_rand($tickets);
$winner = $tickets[$ticket];
$winners[] = $winner;
$tickets = array_filter($tickets, function($item) use ($winner){
return $winner != $item;
});
}
echo '<pre>';
var_export($winners);
outputs
array (
0 => 2,
1 => 1,
2 => 4,
)
array (
0 => 2,
1 => 1,
2 => 3,
)
array (
0 => 1,
1 => 3,
2 => 2,
)
unlike the while option, this will reduce the operations for each loop of the for loop by reducing the ticket array by the uid. It's also the only way to insure your not always pulling out a user with tickets, what if user 1 bought 90% of the tickets, you'd loop on him 90% of the time, in any case you have to reduce the ticket array by winners if they can win only once. In essence you remove each uid from the list when they win. You can also be sure that each ticket has the same chance to win ( as well as array_rand is random that is ) - they all have equal footing.
ticket array reduction
after loop1
array (
'tzql7l42' => 2,
'gap0r6vf' => 2,
'ypqum5yz' => 2,
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
)
after loop2
array (
'smupluac' => 3,
'9d8jsha7' => 4,
'6hdnja42' => 5,
)
after loop3
array (
'smupluac' => 3,
'6hdnja42' => 5,
)
winners
array (
0 => 1,
1 => 2,
2 => 4,
)
to return both the uid and wining ticket change
$winners[] = $winner;
to
$winners[$ticket] = $tickets[$ticket];
now winners will be, just like the input array
ticketnumber => uid
ticket is the key ( which is the ticket ) and winner is the value ( which is the uid )
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
I have 2 arrays:
$arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar'); and
$arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar');
I need to compare the 2 arrays and save the position of the matching elements to a 3rd array $arr3 = (3, 0, 2, 4, 5, 6); //expected result, displaying position of matching element of $arr1 in $arr2.
By 'matching' I mean all elements that are identical (ex. World), or partially the same (ex. Test & Tes) and also those elements that are alike but are in different case (ex. Foo & foo, Bar & bar).
I've tried a series of combinations and of various functions without success, using functions like array_intersect(), substr_compare(), array_filter() and more. I'm not asking for the exact solution, just something to get me on the right track because i'm going around in circles all afternoon.
This seemed to work for me, but I'm sure there are some edge cases I'm missing that you'd need to test for:
foreach( $arr1 as $i => $val1) {
$result = null;
// Search the second array for an exact match, if found
if( ($found = array_search( $val1, $arr2, true)) !== false) {
$result = $found;
} else {
// Otherwise, see if we can find a case-insensitive matching string where the element from $arr2 is at the 0th location in the one from $arr1
foreach( $arr2 as $j => $val2) {
if( stripos( $val1, $val2) === 0) {
$result = $j;
break;
}
}
}
$arr3[$i] = $result;
}
It produces your desired output array:
Array ( [0] => 3 [1] => 0 [2] => 2 [3] => 4 [4] => 5 [5] => 6 )
Try array_uintersect() with a callback function that implements your "matching" algorithm.
Looks like you need 2 foreach loops, and stripos (or mb_stripos for Unicode) for comparing.
I came up with this to account for duplicates. It returns both keys and both values so you can compare to see if it's working properly. To refine it, you can just comment out the lines setting the values you don't need. It matches all cases.
<?php
$arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar');
$arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar');
$matches = array();
//setup the var for the initial match
$x=0;
foreach($arr1 as $key=>$value){
$searchPhrase = '!'.$value.'!i';
//Setup the var for submatching (in case there is more than one)
$y=0;
foreach($arr2 as $key2=>$value2){
if(preg_match($searchPhrase,$value2)){
$matches[$x][$y]['key1']=$key;
$matches[$x][$y]['key2']=$key2;
$matches[$x][$y]['arr1']=$value;
$matches[$x][$y]['arr2']=$value2;
}
$y++;
}
unset($y);
$x++;
}
print_r($matches);
?>
Output looks like this:
Array
(
[1] => Array
(
[0] => Array
(
[key1] => 1
[key2] => 0
[arr1] => Hello
[arr2] => hello
)
)
[2] => Array
(
[2] => Array
(
[key1] => 2
[key2] => 2
[arr1] => World
[arr2] => World
)
)
[3] => Array
(
[4] => Array
(
[key1] => 3
[key2] => 4
[arr1] => Foo
[arr2] => foo
)
)
[4] => Array
(
[5] => Array
(
[key1] => 4
[key2] => 5
[arr1] => Bar1
[arr2] => BaR1
)
)
[5] => Array
(
[5] => Array
(
[key1] => 5
[key2] => 5
[arr1] => Bar
[arr2] => BaR1
)
[6] => Array
(
[key1] => 5
[key2] => 6
[arr1] => Bar
[arr2] => Bar
)
)
)