I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.
Related
How to extract only value from the below array
Array ( [COUNT(department_name)] => 3 )
this is a response from a db query the actual value came like this
Array ( [0] => Array ( [COUNT(department_name)] => 3 ) )
if we select the 0th index $department[0] it is giving this
Array ( [COUNT(department_name)] => 3 )
but now i need only the value from this i.e 3 how can i extract the value from this.
and i tried like this $department[0]['department_name'] and $department[0]['COUNT(department_name)'] but no result.
You can use this simply and it's working -
$department = var_export($department,true);
print_R($department[0]['COUNT(department_name)'])
var_export helps you get the structured information of a variable.
For more see this -
var_export
NOTE - I will suggest you to to get a more readable key from database.
I have an associative array in PHP. i want to change position of an array index and its value.
Array
(
[savedRows] => 1
[errors] => Array
(
[0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4"
)
[success] => successfully saved.
)
to like this
Array
(
[savedRows] => 1
[success] => successfully saved.
[errors] => Array
(
[0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4"
)
)
i want to change ["errors"] index position from second to last and [success] index position at second when ever this array build. This is a dynamic array not a static it builds when a function call on function return i am getting this array.
You can use array functions, but by far the easiest way to change it would be:
$newRow = ['savedRows' => $oldRow['savedRows'],
'success' => $oldRow['success'],
'errors' => $oldRow['errors']];
But it is an associative array, not a numeric array, so order should not be that important.
You need not make this too complicated or use any fancy functions. Just follow a few simple steps.
Store the errors sub array in another variable $errorField.
Unset the array index "errors"
Append this $errorField to a new key "errors".
$errorField = $array['errors'];
unset($array['errors']);
$array['errors'] = $errorField;
Why does the order in the array matter?
If you really need that visually, you should initialize your array before you use it and overwrite the values when you fill it:
$arr = [
'savedRows' => 0,
'success' => '',
'errors' => [],
]
// the rest of your code
Note that the order should not matter, if it does, you have another problem that you should fix.
I have an array that looks like this
Array
(
[0] => Array
(
[Title] => The Title
[Price] => 700
[Quantity] => 2
)
)
Say I wanted to change the Quantity to 5 how would I do that if the array were stored in the variable $ItemArray?
Try $itemArray[0]['Quantity'] = 5;.
Basically, you have an array, $itemArray, which contains an associative array. To access that inside array, you simply use standard PHP array syntax: $itemArray[0].
Then, you need the Quantity field of that inner array. Using the nested array syntax, you append ['Quantity'] to the end of our previous statement, resulting in: $itemArray[0]['Quantity'].
At this point, you have the field you want, and you can use the normal = to set the field value.
$itemArray[0]['Quantity'] = 5;
thats very simple, try
$itemArray[0]["Quantity"] = 5;
What we're doing here is accessing the first index within $itemArray which is 0; 0 contains an array, so we now specify which part of 0 we want to access: Like this basically:
$array[index][innerarrayindex]
I'm a bit struggling with the associative arrays in associative arrays. Point is that I always have to drill deeper in an array and I just don't get this right.
$array['sections']['items'][] = array (
'ident' => $item->attributes()->ident,
'type' => $questionType,
'title' => $item->attributes()->title,
'objective' => (string) $item->objectives->material->mattext,
'question' => (string) $item->presentation->material->mattext,
'possibilities' => array (
// is this even neccesary to tell an empty array will come here??
//(string) $item->presentation->response_lid->render_choice->flow_label->response_label->attributes()->ident => (string) $item->presentation->response_lid->render_choice->flow_label->response_label->material->mattext
)
);
foreach ($item->presentation->response_lid->render_choice->children() as $flow_label) {
$array['sections']['items']['possibilities'][] = array (
(string) $flow_label->response_label->attributes()->ident => (string) $flow_label->response_label->material->mattext
);
}
So 'possibilities' => array() contains an array and if I put a value in it like the comment illustrates I get what I need. But an array contains multiple values so I am trying to put multiple values on the position $array['sections']['items']['possibilities'][]
But this outputs that the values are stores on a different level.
...
[items] => Array
(
[0] => Array
(
[ident] => SimpleXMLElement Object
(
[0] => QTIEDIT:SCQ:1000015312
)
[type] => SCQ
...
[possibilities] => Array
(
)
)
[possibilities] => Array
(
[0] => Array
(
[1000015317] => 500 bytes
)
[1] => Array
...
What am trying to accomplish is with my foreach code above is the first [possibilities] => Array is containing the information of the second. And of course that the second will disappear.
Your $array['sections']['items'] is an array of items, so you need to specify which item to add the possibilities to:
$array['sections']['items'][$i]['possibilities'][]
Where $i is a counter in your loop.
Right now you are appending the Arrays to [items]. But you want to append them to a child element of [items]:
You do:
$array['sections']['items']['possibilities'][] = ...
But it should be something like:
$array['sections']['items'][0]['possibilities'][] = ...
$array['sections']['items'] is an array of items, and as per the way you populate the possibilities key, each item will have it's own possibilities. So, to access the possibilities of the item that is being looped over, you need to specify which one from $array['sections']['items'] by passing the index as explained in the first answer.
OR
To make things simpler, you can try
Save the item array (RHS of the first =) to a separate variable instead of defining and appending to the main array at the same time.
Set the possibilities of that variable.
Append that variable to the main $array['sections']['items']
I have:
array[{IsChecked: true, SEC: 0, STP: 0},
{IsChecked: ture ,SEC: 0, STP: 1},
{IsChecked: false, SEC: 1 ,STP: 0}]
How to get each SEC where IsCheked value is true?
I have some data for my sites statistics in a SQL DB:
date: visits: pageviews:
12-12-12 34 21
12-12-13 31 22
12-12-14 33 2445
12-12-15 35 2422
12-12-16 36 232
//ect ect
I'm trying to create a Multidimensional array that contains all the dates info from the DB and where the date will be the key (selector,name of the array inside the multi array), so as the end result, I should be able to just do this:
print_r $my_multi_array[12-05-12];
And I should see the statistics for that date on the screen.
Now I know how to do all the loops and stuff, and I even have a good idea about how to do multidimensional arrays, it's just that I think I'm doing something wrong:
//first things first, define the array:
$my_multi_array=array();
//then, in a loop, append to the array:
$my_multi_array[]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));
Now when I print_r that array, everything looks good:
Array ( [0] => Array ( [11-12-24] => Array ( [visits] => 1 [pageviews] => 0) ) [1] => Array ( [11-12-25] => Array ( [visits] => 1 [pageviews] => 0) ) [2] => Array ( [11-12-26] => Array ( [visits] => 1 [pageviews] => 0)))1
Notice the 1 at the end ^^. That seemed to be in the result (not a typo).
Now when I try printing a certain array out (using the date as the key):
print_r $my_multi_array['11-12-24'];
I get:
1
So then i try:
print_r $my_multi_array[2];
and that works fine.
For some reason, it wont let me select an array from $my_multi_array using the date as the key.
Any ideas on how to fix this?
thanks
Everything is correct, because you don't have array('key' => 'value') style array, instead of that you've got array( [0] => array( 'key' => 'value' ) ) that's why you're getting correct result on accessing numeric key of the array.
You have to put the date as the array key, like so:
$my_multi_array[$date]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));
Notice the $my_multi_array[$date].
By doing $my_multi_array[] = ... you are just creating a new numerical index on the array with the content on the right side. That's why when you access the array with the numerical index, like $my_multi_array[2], it works.
On the other hand, by doing $my_multi_array[$date]you are treating the array like an hash table, where you associate a key (in this case a string containing a date) with a value.