No of entries of array in multidimensional array - php

I am new to php. Please let me know how do I count number of array entry in under array 80cb936e55e5225cd2af.
array
'status' => int 1
'msg' => string '2 out of 1 Transactions Fetched Successfully' (length=44)
'transaction_details' =>
array
'80cb936e55e5225cd2af' =>
array
0 =>
array
...
1 =>
array
...

Assuming your array variable is $arr : ...
$count = count($arr['transaction_details']['80cb936e55e5225cd2af']);
(But this will only count the indexes/integers of that sub-array, not the values inside them!)

You can use PHP's count() like this
$noOfEntries = count($array['transaction_details']['80cb936e55e5225cd2af']);
This will get you the number of arrays inside 80cb936e55e5225cd2af (not the overall amount of elements).

Related

extracting array from bigger associative array in php

I have stored many arrays in a single associative array and assigned key values simple number counting.
How can I extract one of those array from the associative array?
my array:
$arr = array(
1 => array("ask","bat","cod","dig","egg","fur","gap","hay","ice","jar","kin","lee"),
2 => array("add","big","cap","day","eye","fat","gel","hop","ink","jog","key","law"),
3 => array("axe","bin","cel","don","eat","fig","gig","hut","ion","jin","kid","lip")
);
I want to store the array indexed say 2 in $arr_chosen, what will be the syntax to do so?
$arr = array(
1 => array("ask","bat","cod","dig","egg","fur","gap","hay","ice","jar","kin","lee"),
2 => array("add","big","cap","day","eye","fat","gel","hop","ink","jog","key","law"),
3 => array("axe","bin","cel","don","eat","fig","gig","hut","ion","jin","kid","lip")
);
$arr_chosen = $arr[2];
You can extract an array by doing
$arr_chosen = $arr[2]

Reorder array elements

I have this array of arrays
array (size=2)
'login' =>
array (size=3)
20 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
5 =>
array (size=1)
0 =>
object(Closure)[5]
I'm trying to reorder the position of the keys in the second level arrays (where it says 20, 10, 5) so the result should be
array (size=2)
'login' =>
array (size=3)
5 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
20 =>
array (size=1)
0 =>
object(Closure)[5]
The problem is that I can't figure out how to do that so please help
You need to do
ksort($array['login']);
This will sort the array keys so they are ascending. If you wanted them descending you would do
krsort($array['login']);
PHP has lots of handy functions for sorting arrays
You could have a look at Array Sorting and find out, that 'ksort' is your choice ;-)
Try PHPs ksort function on your second level array: http://php.net/ksort
It will sort your array by key while maintaining the key-data-associations
PHP has a variety of array sorting functions.
In your case, the most appropriate would seem to be ksort(), which is summarised thus:
Sorts an array by key, maintaining key to data correlations.
As pointed out in the comments, your particular example is already in the reverse of the required order; if this can be guaranteed, you could also use array_reverse(), being sure to pass the optional $preserve_keys parameter as true.
Specifically, you want to re-order the sub-array with the key 'login'; assuming your overall array is called $data, you would want to write this:
ksort($data['login']);
// note no need for an assignment, as PHP's sort functions act in-place
or this:
$data['login'] = array_reverse($data['login'], true);
// assign back to the original variable, as array_reverse() returns a new array

Array in array in array

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?

Using array values as another array keys

Good day everyone.
I have an regular array (this is the print_r result, the array can have from 1 to n positions):
Array
(
[1] => value1
[2] => value2
[3] => value3
)
I have another array defined elsewhere as:
$array_def['value1']['value2']['value3'] = array(
'fl' => 'field1',
'f2' => 'field2',
);
Using the first array result, how can i check if $array_def exists? In other words, i need to use a flat array values to check if a multidimensional array correspondence exists; keep in mind that the values can repeat in the first array, therefore flipping values with keys it's not an option as it will collide and remove duplicated values.
Thanks in advance.
You can do it this way:
$a = array(1=>'value1', 2=>'value2', 3=>'value3');
$array_def[$a[1]][$a[2]][$a[3]] = array(
'fl' => 'field1',
'f2' => 'field2',
);
I don't think there's any shortcut or special built-in function to do this.
Found the perfect function for you. returns not only exists, but position within a multi-dimensional array..
http://www.php.net/manual/en/function.array-search.php#47116
dated: 03-Nov-2004 11:13
too much to copy/paste
you can then loop over your flat array and foreach:
multi_array_search($search_value, $the_array)

How to rearrange the array element from 0th index in the sequence to end?

How to rearrange the array element from 0th index in the sequence to end in PHP?
UPDATED
I have an array:
$input =array{
2 => '13234390',
4 => '12345290',
5 => '21322210'
}
now I want this array to be rearranged as
$input =array{
0 => '13234390',
1 => '12345290',
2 => '21322210'
}
With
array_values() returns all the values from the input array and indexes numerically the array.
Example:
$array = array_values($input);

Categories